第一个是UDPsocekt升级版
client.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<string.h>
#include <arpa/inet.h>
#define ECHOMAX 255
#define WORDMAX 10
int main(int argc,char *argv[]){
int sock;
struct sockaddr_in echoServAddr;
struct sockaddr_in fromAddr;
unsigned short echoServPort;
unsigned int fromSize;
char *servIP;
char *echoString;
char echoBuffer[ECHOMAX+1];
int echoStringLen;
int respStringLen;
if(argc<3){
printf("Usage: %s <Servcer IP><Echo port><Echo Word>\n",argv[0]);
exit(1);
}
servIP=argv[1];
echoServPort=atoi(argv[2]);//7
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=inet_addr(servIP);
echoServAddr.sin_port=htons(echoServPort);
int i;
for(i=0;i<WORDMAX;i++){
printf("Operation No.%d\n",i+1);
echoString=argv[i+3];
char temp[]="";
if((echoString=argv[i+3])<=temp){
printf("word end\n");
break;
}
echoStringLen=strlen(echoString);
if((sendto(sock,echoString,echoStringLen,0,(struct sockaddr*)&echoServAddr,sizeof(echoServAddr)))!=echoStringLen){
printf("sendto() sent a different number of bytes than expected.\n");
}
printf("Sending to %s, data: %s\n",servIP,echoString);
fromSize=sizeof(fromAddr);
if((respStringLen=recvfrom(sock,echoBuffer,ECHOMAX,0,(struct sockaddr*)&fromAddr,&fromSize))!=echoStringLen){
printf("recvfrom() failed\n");
}
if(echoServAddr.sin_addr.s_addr!=fromAddr.sin_addr.s_addr){
printf("Error: received a packet from unknow source.\n");
exit(1);
}
echoBuffer[respStringLen]='\0';
printf("received: %s\n",echoBuffer);
printf("\n");
printf("\n");
}
close(sock);
exit(0);
}
server.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.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;
if(argc!=2){
printf("Usage: %s<UDP SERVER PORT>\n",argv[0] );
exit(1);
}
echoServPort=atoi(argv[1]);
printf("Waiting data on port :%d\n",echoServPort);
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,ECHOMAX,0,(struct sockaddr*) &echoClntAddr,&cliAddrLen))<0){
printf("recvfrom() failed.\n");
}
printf("from %s receving: %s\n",inet_ntoa(echoClntAddr.sin_addr),echoBuffer);
if((sendto(sock,echoBuffer,recvMsgSize,0,(struct sockaddr*) &echoClntAddr,sizeof(echoClntAddr)))!=recvMsgSize){
printf("sendto() sent a different number of bytes than expected.\n");
}
memset(echoBuffer,'\0',ECHOMAX);
}
return 0;
}