全部博文(346)
分类: C/C++
2011-05-31 15:27:43
/* Creates an IPv6 TCP client on Unix */ /* port number is passed in as an arg, server must be on local host */ #include#include #include #include #include #include #include /* for atoi */ char *msg = "Hello from the client"; void error(char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sock, n; unsigned short port; struct sockaddr_in6 server; char buffer[1200]; if (argc != 2) { printf("Usage: %s port\n", argv[0]); exit(1); } sock= socket(AF_INET6, SOCK_STREAM, 0); if (sock < 0) error("Opening socket"); memset(&server,0,sizeof(struct sockaddr_in6)); server.sin6_family = AF_INET6; port = (unsigned short)atoi(argv[1]); server.sin6_port = htons(port); if (inet_pton(AF_INET6,"0:0:0:0:0:0:0:1",&server.sin6_addr) <= 0) error("inet_pton"); if (connect(sock, (struct sockaddr *)&server, sizeof (server)) < 0) error("Connecting"); n = write(sock, msg, strlen(msg)); if (n < strlen(msg)) error("Writing to socket"); n = read(sock, buffer, 1024); if (n < 1) error("reading from socket"); buffer[n]='\0'; printf("The message from the server is %s\n",buffer); if (close(sock) < 0) error("closing"); printf("Client terminating\n"); return 0; }
/* IPv6server.c - creates an IPv6 TCP server on Unix To compile on solaris gcc ... -lnsl -lsocket -lpthread To compile on freebsd gcc ... -pthread */ #include#include #include #include #include #include #include #include #define BUFSIZE 1024 extern int errno; void error(char *msg) { perror(msg); exit(0); } int main(int argc, char *argv[]) { int sock, *newsock, len, fromlen, retval; unsigned short port; struct sockaddr_in6 server; /***/ struct sockaddr_in6 from; /***/ pthread_t tid; void *ConnectionThread(void *); //function prototype if (argc < 2) { fprintf(stderr,"usage %s portnumber\n",argv[0]); exit(0); } port = (unsigned short) atoi(argv[1]); sock=socket(AF_INET6, SOCK_STREAM, 0); /***/ if (sock < 0) error("Opening socket"); server.sin6_family=AF_INET6; /***/ server.sin6_addr =in6addr_any; /***/ server.sin6_port=htons(port); len=sizeof(server); if (bind(sock, (struct sockaddr *)&server, len) < 0) error("binding socket"); fromlen=sizeof(from); if (listen(sock,5) < 0) error("listening");; while (1) { newsock = (int *)malloc(sizeof (int)); *newsock=accept(sock, (struct sockaddr *)&from, &fromlen); if (*newsock < 0) error("Accepting"); printf("Connection accepted\n"); retval = pthread_create(&tid, NULL, ConnectionThread, (void *)newsock); if (retval != 0) { error("Error, could not create thread"); } } return 0; // we never get here } // OVER FOR CONNECTIONTHREAD void *ConnectionThread(void *arg) { int sock, n, len; char buffer[BUFSIZE]; char *msg = "Got your message"; sock = *(int *)arg; len = strlen(msg); n = read(sock,buffer,BUFSIZE-1); while (n > 0) { buffer[n]='\0'; printf("Message is %s\n",buffer); n = write(sock,msg,len); if (n < len) error("Error writing"); n = read(sock,buffer,BUFSIZE-1); if (n < 0) error("Error reading"); } if (close(sock) < 0) error("closing"); pthread_exit(NULL); return NULL; }