我有以下代码...
客户代码
#include<stdio.h>
#include<string.h>
//#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
struct sockaddr_in client,server;
int s,n;
char b1[100],b2[100];
s=socket(AF_INET,SOCK_DGRAM,0);
server.sin_family=AF_INET;
server.sin_port=2000;
server.sin_addr.s_addr=inet_addr("127.0.0.1");
printf("\nClient ready....\n");
n=sizeof(server);
while(1)
{
printf("\nClient:");
gets(b2);
sendto(s,b2,sizeof(b2),0,(struct sockaddr *)&server,n);
if(strcmp(b2,"end")==0)
break;
recvfrom(s,b1,sizeof(b1),0,NULL,NULL);
printf("\nServer:%s",b1);
}
}
服务器代码......
#include<stdio.h>
#include<string.h>
//#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
struct sockaddr_in cliaddr,myaddr;
int servsock,clisock;
char b1[100],b2[100];
servsock=socket(AF_INET,SOCK_DGRAM,0);
myaddr.sin_family=AF_INET;
myaddr.sin_port=2000;
myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(servsock,(struct sockaddr *)&myaddr,sizeof(myaddr));
printf("\nServer ready,waiting for client....\n");
clisock=sizeof(cliaddr);
while(1)
{
recvfrom(servsock,b1,sizeof(b1),0,(struct sockaddr *) &cliaddr,&clisock);
if(!(strcmp(b1,"end")))
break;
printf("\nCliaddr:%s",b1);
printf("\nmyaddr:");
gets(b2);
sendto(servsock,b2,sizeof(b2),0,(struct sockaddr *) &cliaddr,clisock);
}
}
这些代码用于单个客户端 UDP 聊天..我需要进行哪些更改才能使其成为多客户端 UDP 聊天..我希望多个客户端向服务器发送消息..