网络编程udp入门
老师布置的作业
echo4_server.c
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<unistd.h> 5 #include<arpa/inet.h> 6 #include<sys/socket.h> 7 8 #define BUF_SIZE 10 //一次最大传输大小 9 void error_handling(char *message); //异常 10 11 int main(int argc,char** argv) 12 { 13 int serv_sock,n; 14 char message[BUF_SIZE*8+3],message1[BUF_SIZE+3]; 15 int str_len; 16 socklen_t clnt_adr_sz; 17 struct sockaddr_in serv_adr,clnt_adr; 18 if(argc!=2){ 19 printf("Usage : %s <port>\n",argv[0]); 20 exit(1); 21 } 22 serv_sock=socket(PF_INET,SOCK_DGRAM,0); 23 if(serv_sock == -1){ 24 error_handling("UDP socket creattion error"); 25 } 26 27 memset(&serv_adr,0,sizeof(serv_adr)); 28 serv_adr.sin_family=AF_INET; 29 serv_adr.sin_addr.s_addr=htonl(INADDR_ANY); 30 serv_adr.sin_port=htons(atoi(argv[1])); 31 32 if(bind(serv_sock,(struct sockaddr*)&serv_adr,sizeof(serv_adr))==-1) 33 error_handling("bind() error"); 34 35 while(1){ 36 clnt_adr_sz=sizeof(clnt_adr); 37 str_len=recvfrom(serv_sock,message,BUF_SIZE+3,0,(struct sockaddr*)&clnt_adr,&clnt_adr_sz); 38 message[str_len]=0; 39 if(message[0]==0){ 40 n=message[1]; 41 message[0]=message[1]=message[2]='1'; 42 message[3]=0; 43 for(int i=0;i<n;i++){ 44 str_len=recvfrom(serv_sock,&message1,BUF_SIZE*2,0,(struct sockaddr*)&clnt_adr,&clnt_adr_sz); 45 message1[str_len]=0; 46 strcat(message,&message1[3]); 47 } 48 } 49 if(strlen(&message[3])-1<=BUF_SIZE){ 50 message[0]=1; 51 message[2]=strlen(&message[3]); 52 sendto(serv_sock,message,strlen(&message[3])+3,0,(struct sockaddr*)&clnt_adr,clnt_adr_sz); 53 } 54 else{ 55 message[0]=0; 56 message[1]=strlen(&message[3])/BUF_SIZE; 57 if(strlen(&message[3])%BUF_SIZE!=0) 58 message[1]+=1; 59 sendto(serv_sock,message,2,0,(struct sockaddr*)&clnt_adr,clnt_adr_sz); 60 for(int i=0;i*BUF_SIZE<strlen(&message[3]);i++){ 61 message[i*BUF_SIZE]=2; 62 message[i*BUF_SIZE+1]=i+1; 63 message[i*BUF_SIZE+2]=BUF_SIZE; 64 if((i+1)*BUF_SIZE>strlen(&message[3])) 65 message[i*BUF_SIZE+2]=strlen(&message[3])-i*BUF_SIZE; 66 sendto(serv_sock,&message[i*BUF_SIZE],message[i*BUF_SIZE+2]+3,0,(struct sockaddr*)&clnt_adr,clnt_adr_sz); 67 } 68 } 69 } 70 close(serv_sock); 71 return 0; 72 } 73 74 void error_handling(char *message) 75 { 76 fputs(message,stderr); 77 fputc('\n',stderr); 78 exit(1); 79 }
echo4_client.c
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<unistd.h> 5 #include<arpa/inet.h> 6 #include<sys/socket.h> 7 8 #define BUF_SIZE 10 9 10 void error_handling(char *message); 11 12 int main(int argc,char** argv) 13 { 14 int sock; 15 char message[BUF_SIZE*8+3],message1[BUF_SIZE+3]; 16 int str_len,n; 17 socklen_t adr_sz; 18 19 struct sockaddr_in serv_adr,from_adr; 20 if(argc!=3){ 21 printf("Usage : %s <IP> <port>\n",argv[0]); 22 exit(1); 23 } 24 25 sock=socket(PF_INET,SOCK_DGRAM,0); 26 if(sock==-1) 27 error_handling("sock() error"); 28 memset(&serv_adr,0,sizeof(serv_adr)); 29 serv_adr.sin_family=AF_INET; 30 serv_adr.sin_addr.s_addr=inet_addr(argv[1]); 31 serv_adr.sin_port=htons(atoi(argv[2])); 32 33 while(1){ 34 int m_len; 35 fputs("Insert message(q to quit): ",stdout); 36 fgets(&message[3],sizeof(message)-3,stdin); 37 if(!strcmp(&message[3],"q\n")||!strcmp(&message[3],"Q\n"))break; 38 if(strlen(&message[3])-1<=BUF_SIZE){ 39 message[0]=1; 40 message[2]=strlen(&message[3]); 41 sendto(sock,message,strlen(&message[3])+3,0,(struct sockaddr*)&serv_adr,sizeof(serv_adr)); 42 } 43 else{ 44 message[0]=0; 45 message[1]=strlen(&message[3])/BUF_SIZE; 46 if(strlen(&message[3])%BUF_SIZE!=0) 47 message[1]+=1; 48 sendto(sock,message,2,0,(struct sockaddr*)&serv_adr,sizeof(serv_adr)); 49 for(int i=0;i*BUF_SIZE+1<strlen(&message[3]);i++){ 50 message[i*BUF_SIZE]=2; 51 message[i*BUF_SIZE+1]=i+1; 52 message[i*BUF_SIZE+2]=BUF_SIZE; 53 if((i+1)*BUF_SIZE>=strlen(&message[3])) 54 message[i*BUF_SIZE+2]=strlen(&message[3])-1-i*BUF_SIZE; 55 sendto(sock,&message[i*BUF_SIZE],message[i*BUF_SIZE+2]+3,0,(struct sockaddr*)&serv_adr,sizeof(serv_adr)); 56 } 57 } 58 59 str_len=recvfrom(sock,message,BUF_SIZE+3,0,(struct sockaddr*)&from_adr,&adr_sz); 60 message[str_len]=0; 61 if(message[0]==0){ 62 n=message[1]; 63 message[0]=message[1]=message[2]='1'; 64 message[3]=0; 65 for(int i=0;i<n;i++){ 66 str_len=recvfrom(sock,&message1,BUF_SIZE*2,0,(struct sockaddr*)&from_adr,&adr_sz); 67 message1[str_len]=0; 68 strcat(message,&message1[3]); 69 //printf("%d,%d,%d,%s\n",message1[0],message1[1],message1[2],&message1[3]); 70 } 71 } 72 printf("Message from server : %s \n",&message[3]); 73 } 74 close(sock); 75 return 0; 76 } 77 78 void error_handling(char *message) 79 { 80 fputs(message,stderr); 81 fputc('\n',stderr); 82 exit(1); 83 }