File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / example / recvfile.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu Aug 18 09:06:31 2016 UTC (7 years, 10 months ago) by misho
Branches: MAIN
CVS tags: io7_4, IO7_3, HEAD
version 7.3

    1: #include <stdio.h>
    2: #include <unistd.h>
    3: #include <sys/types.h>
    4: #include <sys/socket.h>
    5: #include <netinet/in.h>
    6: #include <arpa/inet.h>
    7: #include <aitio.h>
    8: 
    9: 
   10: int
   11: main(int argc, char **argv)
   12: {
   13: 	int s, c, sl;
   14: 	struct sockaddr sa;
   15: 	struct sockaddr_in sin;
   16: 
   17: 	if (argc < 3)
   18: 		return 1;
   19: 
   20: #ifndef __linux__
   21: 	sa.sa_len = sl = sizeof sa;
   22: 	sin.sin_len = sizeof sin;
   23: #endif
   24: 	sin.sin_family = AF_INET;
   25: 	sin.sin_port = htons(5000);
   26: 	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
   27: 
   28: 	s = socket(AF_INET, SOCK_STREAM, 0);
   29: 	if (s == -1) {
   30: 		perror("socket");
   31: 		return 2;
   32: 	}
   33: 	if (bind(s, (struct sockaddr*) &sin, sizeof sin) == -1) {
   34: 		perror("bind");
   35: 		close(s);
   36: 		return 3;
   37: 	}
   38: 	if (listen(s, 5) == -1) {
   39: 		perror("listen");
   40: 		close(s);
   41: 		return 4;
   42: 	}
   43: 
   44: 	if ((c = accept(s, &sa, (socklen_t*) &sl)) != -1) {
   45: 		sl = ioRecvFile(c, argv[1], strtol(argv[2], NULL, 0), 0644, 
   46: 				argv[3] ? strtol(argv[3], NULL, 0) : 0);
   47: 		printf("received %d bytes\n", sl);
   48: 		if (!sl)
   49: 			printf("#%d - %s\n", io_GetErrno(), io_GetError());
   50: 		close(c);
   51: 	}
   52: 
   53: 	close(s);
   54: 	return 0;
   55: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>