[linux]小程序:UDP下client向server传输多个字符串
还是实验的作业~
一共需要两个文件,这是client
1 #include <stdio.h> 2 #include <sys/socket.h> 3 #include <arpa/inet.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <unistd.h> 7 8 #define ECHOMAX 255 9 10 int main(int argc, char *argv[]) { 11 int sock; 12 struct sockaddr_in echoServAddr; 13 struct sockaddr_in fromAddr; 14 unsigned short echoServPort; 15 unsigned int fromSize; 16 char *servIP; 17 char *echoString; 18 char echoBuffer[ECHOMAX+1]; 19 int echoStringLen=0; 20 int respStringLen; 21 int i;//for counting 22 23 if (argc<3) { 24 printf("Usage:%s <Server IP> <Echo Port> [<Echo Words>]\n",argv[0]); 25 exit(1); 26 } 27 28 servIP = argv[1]; 29 30 for(i=3;i<argc;i++){ 31 echoStringLen = echoStringLen + strlen(argv[i]); 32 } 33 if (echoStringLen > ECHOMAX) 34 printf("Echo word too long.\n"); 35 36 echoServPort = atoi(argv[2]); 37 38 if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) <0) 39 printf("socket() failed.\n"); 40 memset(&echoServAddr,0,sizeof(echoServAddr)); 41 echoServAddr.sin_family = AF_INET; 42 echoServAddr.sin_addr.s_addr = inet_addr(servIP); 43 echoServAddr.sin_port = htons(echoServPort); 44 45 for(i=3;i<argc;i++) { 46 echoString = argv[i]; 47 if ((sendto(sock,echoString,strlen(echoString),0,(struct sockaddr *)&echoServAddr,sizeof(echoServAddr))) != strlen(echoString)) 48 printf("sendto() sent a different number of bytes than expected.\n"); 49 } 50 } 51 echoBuffer[strlen(echoString)] = '\0'; 52 close(sock); 53 exit(0); 54 } 55
这是server
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define ECHOMAX 255 int main(int argc,char *argv[]) { int sock; struct sockaddr_in echoServAddr; struct sockaddr_in echoClntAddr; unsigned int cliAddrLen; char echoBuffer[ECHOMAX]; unsigned short echoServPort; int recvMsgSize; int i;//for counting if (argc != 2) { printf("Usage: %s <UDP SERVER PORT>\n",argv[0]); exit(1); } echoServPort = atoi(argv[1]); if ((sock = socket(PF_INET,SOCK_DGRAM,0)) < 0) printf("socket() failed.\n"); memset(&echoServAddr,0,sizeof(echoServAddr)); echoServAddr.sin_family = AF_INET; echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); echoServAddr.sin_port = htons(echoServPort); if ((bind(sock,(struct sockaddr *)&echoServAddr,sizeof(echoServAddr))) <0) printf("bind() failed.\n"); for (;;) { cliAddrLen = sizeof(echoClntAddr); if ((recvMsgSize = recvfrom(sock,echoBuffer,strlen(echoBuffer),0,(struct sockaddr *)&echoClntAddr,&cliAddrLen)) < 0) printf("recvfrom() failed.\n"); printf("Handling client %s\n",inet_ntoa(echoClntAddr.sin_addr)); printf("Message: "); for(i=0;i<recvMsgSize;i++) { printf("%c",echoBuffer[i]); if(i==(recvMsgSize-1)) printf("\n"); } } printf("\n"); }
使用示例:
例如想要传输的字符串为Hello, world!
那么首先需要运行server:
./server 1500
1500是端口号(其实在普通用户的权限下,使用一个>1024的端口就可以)
然后运行client:(按alt+F2切换到新窗口运行)
./client 127.0.0.1 1500 Hello, world!
然后alt+F1返回原来的窗口,可以看到:
Handling client 127.0.0.1
Message: Hello,
Handling client 127.0.0.1
Message: world!