Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
/* A very simple TCP serial server that implements a trivial protocol. The point of this example is to see how to paralellize it so that it can serve more than one client at a time. */ #include // for fdopen(), fprintf(), fflush(), fgetc() #include // for atoi(), NULL, and exit() #include // for memset(), bzero(), bcopy(), and strerror() #include // for errno #include // for close() #include // for socket(), bind(), listen(), accept(), setsockopt() #include // for sockaddr_in, htons(), htonl(), inet_ntoa() #include // for gethostbyaddr(), hostent, hstrerror(), and h_errno // prototypes for two error handling functions (defined below) void unix_error(char *msg); void dns_error(char *msg); #define LISTENQUEUE 8 // second argument to listen() int main(int argc, char **argv) { int socket_fd; // listening socket file descriptor int connected_fd; // connected socket file descriptor int optionvalue = 1; // used in setsockopt() struct sockaddr_in localAddr; // server's address structure struct sockaddr_in clientAddr; // clients's address structure struct hostent *hp; // used to get client's IP address int clientLength; // needed for accept() char *h_addressp; FILE *connected_fpout; // connected socket file pointer (i.e., stream) FILE *connected_fpin; // connected socket file pointer if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(0); } // Create a reliable, stream socket using TCP. if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) unix_error("socket() error"); // Eliminate "Address already in use" error from bind(). if ( setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, (const void*)&optionvalue, sizeof(int)) < 0 ) unix_error("setsockopt() error"); // Construct a local address structure. memset(&localAddr, 0, sizeof(localAddr)); // zero out the address structure localAddr.sin_family = AF_INET; // Internet address family localAddr.sin_addr.s_addr = htonl(INADDR_ANY); // any incoming interface localAddr.sin_port = htons( atoi(argv[1]) ); // fill in the port number // Bind to the local address if (bind(socket_fd, (struct sockaddr*)&localAddr, sizeof(localAddr)) < 0) unix_error("bind() error"); // Make it a listening socket ready to accept connection requests if (listen(socket_fd, LISTENQUEUE) < 0) unix_error("listen() error"); while(1) { clientLength = sizeof(clientAddr); // needed by accept() // Wait for a connection from a client. connected_fd = accept(socket_fd, (struct sockaddr*)&clientAddr, &clientLength); if (connected_fd < 0) unix_error("accept() error"); // Determine the domain name and IP address of the client. hp = gethostbyaddr( (const char*)&clientAddr.sin_addr.s_addr, sizeof(clientAddr.sin_addr.s_addr), AF_INET ); if ( hp == NULL ) dns_error("gethostbyaddr() error"); h_addressp = inet_ntoa(clientAddr.sin_addr); fprintf(stderr, "server connected to %s (%s)\n", hp->h_name, h_addressp); // Convert the connected socket file descriptor to file pointers // (i.e., streams) and use C Standard Library I/O functions. // (But see the textbook CS:APP, pages 796-797.) if ( (connected_fpout = fdopen(connected_fd, "w")) == NULL ) unix_error("fdopen() error" ); if ( (connected_fpin = fdopen(connected_fd, "r")) == NULL ) unix_error("fdopen() error" ); // Send a welcome message to the client. fprintf(connected_fpout, "Welcome to the world's dumbest Internet server.\n"); fflush(connected_fpout); // Now block the server on I/O from the client. This prevents // the server from serving any other clients until the current // client sends at least one character. char c = fgetc(connected_fpin); fprintf(stdout, "Got %c from client.\n", c); fflush(stdout); // Close the "connected" socket. if (close(connected_fd) < 0) unix_error("close() error"); } exit(0); }//main /* Below are two error handling functions. They provide reasonably meaningful error messages. */ void unix_error(char *msg) { fprintf(stderr, "%s: %s (errno = %d)\n", msg, strerror(errno), errno); exit(1); } void dns_error(char *msg) { fprintf(stderr, "%s: %s (h_errno = %d)\n", msg, hstrerror(h_errno), h_errno); exit(1); }