首页 > Linux操作系统 > Linux操作系统 > linux下TCP编程(转)
|
编程要求: 客户端建立连接,服务器端接受连接并向客户端发送消息,客户端接收消息并将消息打印出来,最后服务器端向客户端发送exit,连接结束! 源代码: //服务器端:server.c #include #include #include #include #include #include #include #include #define PORT 5000 // The port which is communicate with server #define BACKLOG 10 #define LENGTH 512 // Buffer length int main () { int sockfd; // Socket file descriptor int nsockfd; // New Socket file descriptor int num; int sin_size; // to store struct size char sdbuf[LENGTH]; // Send buffer struct sockaddr_in addr_local; struct sockaddr_in addr_remote; char sendstr[16]= {"123456789 abcde"}; /* Get the Socket file descriptor */ if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) { printf ("ERROR: Failed to obtain Socket Despcritor.\n"); return (0); } else { printf ("OK: Obtain Socket Despcritor sucessfully.\n"); } /* Fill the local socket address struct */ addr_local.sin_family = AF_INET; // Protocol Family addr_local.sin_port = htons(PORT); // Port number addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct /* Blind a special Port */ if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 ) { printf ("ERROR: Failed to bind Port %d.\n",PORT); return (0); } else { printf("OK: Bind the Port %d sucessfully.\n",PORT); } /* Listen remote connect/calling */ if(listen(sockfd,BACKLOG) == -1) { printf ("ERROR: Failed to listen Port %d.\n", PORT); return (0); } else { printf ("OK: Listening the Port %d sucessfully.\n", PORT); } while(1) { sin_size = sizeof(struct sockaddr_in); /* Wait a connection, and obtain a new socket file despriptor for single connection */ if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1) { printf ("ERROR: Obtain new Socket Despcritor error.\n"); continue; } else { printf ("OK: Server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr)); } /* Child process */ if(!fork()) { printf("You can enter string, and press 'exit' to end the connect.\n"); while(strcmp(sdbuf,"exit") != 0) { scanf("%s", sdbuf); if((num = send(nsockfd, sdbuf, strlen(sdbuf), 0)) == -1) { printf("ERROR: Failed to sent string.\n"); close(nsockfd); exit(1); } printf("OK: Sent %d bytes sucessful, please enter again.\n", num); } } close(nsockfd); while(waitpid(-1, NULL, WNOHANG) > 0); } } //客户端:client.c #include #include #include #include #include #include #include #include #define PORT 5000 // The port which is communicate with server #define LENGTH 256 // Buffer length int main(int argc, char *argv[]) { int sockfd; // Socket file descriptor int num; // Counter of received bytes char revbuf[LENGTH]; // Receive buffer struct sockaddr_in remote_addr; // Host address information /* Check parameters number */ if (argc != 2) { printf ("Usage: client HOST IP (ex: ./client 192.168.0.94).\n"); return (0); } /* Get the Socket file descriptor */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { printf("ERROR: Failed to obtain Socket Descriptor!\n"); return (0); } /* Fill the socket address struct */ remote_addr.sin_family = AF_INET; // Protocol Family remote_addr.sin_port = htons(PORT); // Port number inet_pton(AF_INET, argv[1], &remote_addr.sin_addr); // Net Address bzero(&(remote_addr.sin_zero), 8); // Flush the rest of struct /* Try to connect the remote */ if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1) { printf ("ERROR: Failed to connect to the host!\n"); return (0); } else { printf ("OK: Have connected to the %s\n",argv[1]); } /* Try to connect the server */ while (strcmp(revbuf,"exit") != 0) // Check remoter command { bzero(revbuf,LENGTH); num = recv(sockfd, revbuf, LENGTH, 0); switch(num) { case -1: printf("ERROR: Receive string error!\n"); close(sockfd); return (0); case 0: close(sockfd); return(0); default: printf ("OK: Receviced numbytes = %d\n", num); break; } revbuf[num] = '\0'; printf ("OK: Receviced string is: %s\n", revbuf); } close (sockfd); return (0); } 结果: 1:[root@localhost server]# ./server OK: Obtain Socket Despcritor sucessfully. OK: Bind the Port 5000 sucessfully. OK: Listening the Port 5000 sucessfully. 2:[root@zylinux uclient]# ./client 192.168.0.94 OK: Have connected to the 192.168.0.94 [root@localhost server] OK: Server has got connect from 192.168.0.231. You can enter string, and press 'exit' to end the connect. 3:在PC终端输入 hello moon! 显示: OK: Sent 8 bytes sucessful, please enter again. OK: Sent 5 bytes sucessful, please enter again. 在客户端会显示 OK: Receviced numbytehello OK: Receviced numbytes = 5 OK: Receviced string is: moon! 4在PC终端输入 exit 显示 OK: Sent 4 bytes sucessful, please enter again. 客户端显示: OK: Receviced numbytes = 4 OK: Receviced string is: exit [root@zylinux uclient]# 即跳出了 |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13771794/viewspace-623137/ ,如需转载,请注明出处,否则将追究法律责任。