实验一 TCP数据发送与接收
【实验目的】
1、熟练掌握套接字函数的使用方法。
2、应用套接字函数完成基本TCP通讯,实现服务器与客户端的信息交互。
【实验学时】
4学时
【实验内容】
实现一个服务器与一个客户之间通讯。具体功能如下:
(1)服务器端:
服务器端等待客户的连接,一旦连接成功,则显示客户的IP地址、端口号;
循环接收客户发来的信息并在终端上显示,同时在信息前加入序号并返回给客户端;当从客户接收到bye后不再发送给各户并退出程序。
(2)客户端:
根据用户从终端输入的服务器IP地址及端口号连接到相应的服务器;
连接成功后,循环从终端输入信息,并将信息发送给服务器,再从服务器接收信息,并显示在终端上。
当从终端输入bye并发送给服务器后,程序退出。
程序实现:
服务器端:
1 /* TcpServer.c 2 copyright@msxh 2015/09/21 3 */ 4 #include5 #include <string.h> 6 #include 7 #include in.h> 8 #include 9 10 int main(){ 11 12 struct sockaddr_in server; 13 struct sockaddr_in client; 14 int listenfd,connetfd; 15 char ip[20]; 16 int port; 17 int addrlen; 18 char rebuf[100]; 19 char wrbuf[100]; 20 char tmp[100]; 21 int revlen; 22 /*---------------------socket-------------------*/ 23 if((listenfd = socket(AF_INET,SOCK_STREAM,0))== -1){ 24 perror("socket() error\n"); 25 exit(1); 26 } 27 28 /*----------------------IO-----------------------*/ 29 printf("Please input the ip:\n"); 30 scanf("%s",ip); 31 printf("Please input the port:\n"); 32 scanf("%d",&port); 33 34 /*---------------------bind----------------------*/ 35 bzero(&server,sizeof(server)); 36 server.sin_family = AF_INET; 37 server.sin_port = htons(port); 38 server.sin_addr.s_addr = inet_addr(ip); 39 if(bind(listenfd,(struct sockaddr *)&server,sizeof(server))== -1){ 40 perror("bind() error\n"); 41 exit(1); 42 } 43 44 /*----------------------listen-------------------*/ 45 if (listen(listenfd,5)== -1){ 46 perror("listen() error\n"); 47 exit(1); 48 } 49 50 /*----------------------accept------------------*/ 51 addrlen = sizeof(client); 52 if((connetfd = accept(listenfd,(struct sockaddr *)&client,&addrlen))== -1){ 53 perror("accept() error\n"); 54 exit(1); 55 } 56 /*---------------------show client---------------*/ 57 printf("connect successful!\n"); 58 printf("the client ip is %s,port is %d\n",inet_ntoa(client.sin_addr),ntohs(port)); 59 60 /*----------------------read and write----------*/ 61 int serial = 0; 62 while(1){ 63 bzero(rebuf,sizeof(rebuf)); 64 revlen = read(connetfd,rebuf,sizeof(rebuf)); 65 if((memcmp("bye",rebuf,3))== 0){ 66 printf("Bye-bye then close the connect...\n"); 67 break; 68 } 69 bzero(wrbuf,sizeof(wrbuf)); 70 bzero(tmp,sizeof(tmp)); 71 sprintf(tmp,"%d",serial); 72 strcat(tmp,rebuf); 73 bcopy(tmp,wrbuf,strlen(tmp)); 74 write(connetfd,wrbuf,sizeof(wrbuf)); 75 rebuf[revlen] = '\0'; 76 printf("the info from client is:%s\n",rebuf); 77 serial++; 78 } 79 80 /*----------------------close-------------------*/ 81 close(connetfd); 82 close(listenfd); 83 84 return 0; 85 }
客户端实现:
/* TcpClient.c copyright@msxh 2015/09/21 */ #include#include <string.h> #include #include in.h> #include int main(){ int sockfd; char wrbuf[100]; char ip[20]; int port; int revlen; char rebuf[100]; struct sockaddr_in server; /*---------------------socket---------------------*/ if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1){ perror("socket error\n"); exit(1); } /*---------------------connect--------------------*/ printf("Please input the ip:\n"); scanf("%s",ip); printf("Please input the port:\n"); scanf("%d",&port); bzero(&server,sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(port); inet_aton(ip,&server.sin_addr); if(connect(sockfd,(struct sockaddr *)&server,sizeof(server))== -1){ perror("connect() error\n"); exit(1); } /*-----------------------read and write------------------*/ while(1){ bzero(wrbuf,sizeof(wrbuf)); bzero(rebuf,sizeof(rebuf)); printf("Please input the info:\n"); scanf("%s",wrbuf); if((memcmp("bye",wrbuf,3))== 0){ write(sockfd,wrbuf,strlen(wrbuf)); printf("Bye-bye then close the connect...\n"); break; } //printf("%s\n",wrbuf); write(sockfd,wrbuf,strlen(wrbuf)); revlen = read(sockfd,rebuf,sizeof(rebuf)); rebuf[revlen] = '\0'; printf("The info from server is: %s\n",rebuf); } /*------------------------close--------------------------*/ close(sockfd); return 0; }
makefile文件:
main:tcpserver.c tcpclient.c gcc -o tcpserver tcpserver.c gcc -o tcpclient tcpclient.c
学习Linux、Unix网络编程时写的第一个程序。。。