#include
#include
#include
#include
#include
#include
int main(int argc ,char* argv[])
{
if(argc < 3)
{
printf("./server [addr:] [port:]\n");
return 4;
}
//创建一个套接字
int sock = socket(AF_INET,SOCK_DGRAM,0);//SOCK_DGRAM代表UDP
if(sock < 0)
{
perror("socket");
return 1;
}
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(atoi(argv[2]));
local.sin_addr.s_addr = inet_addr(argv[1]);
//绑定
if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
{
perror("bind");
return 2;
}
char buf[1024];
struct sockaddr_in client;
socklen_t len = sizeof(client);
while(1)
{
ssize_t s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len);
if(s<0)
{
perror("recvfrom");
return 3;
}
else if(s == 0)
{
printf("client closed\n");
}
else
{
buf[s] = 0;
printf("[%s : %d]:%s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buf);
sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&client,len);
if(strcmp(buf,"quit") == 0)
break;
}
}
return 0;
}
client客户端部分
#include
#include
#include
int main(int argc ,char* argv[])
{
if(argc < 3)
{
printf("./client [addr:][port:]\n");
return 4;
}
int sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock < 0)
{
perror("socket");
return 1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
char buf[1024];
struct sockaddr_in peer;
socklen_t len = sizeof(peer);
while(1)
{
printf("#please enter# ");
fflush(stdout);
ssize_t s = read(0,buf,sizeof(buf)-1);
if(s < 0)
{
perror("read");
return 2;
}
buf[s-1] = 0;
sendto(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&server,sizeof(server));
s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&peer,&len);
if(s<0)
{
perror("recvfrom");
return 3;
}
else if(s == 0)
{
printf("server closed\n");
}
else
{
buf[s] = 0;
printf("#server echo# %s\n",buf);
if(strcmp(buf,"quit") == 0)
break;
}
}
return 0;
}
Makefile文件
.PHONY:all
all:client server
client:client.c
gcc -o $@ $^
server:server.c
gcc -o $@ $^
.PHONY:clean
clean:
rm -f client server
运行结果: