加油
要求: Client:
- 客户端 可多次向服务器发送数据
- C可接收S发送的数据,回显
Server:
- 运行端口可配置
- 可接受回显C发来的数据
- 发消息给C
- 支持多个C 同时发送
先写服务端:
- 创建连接
- 接收客户端的数据
- 等待客户端发包,若客户端没有建立连接,则阻塞
- 接收数据
再写客户端
代码
udpserver
#include<stdlib.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
void print_usage(char * cmd)
{
fprintf(stderr," %s usage:\n",cmd);
fprintf(stderr,"%s IP_Addr [port]\n",cmd);
}
int main(int argc,char** argv)
{
struct sockaddr_in server;
struct sockaddr_in client;
int len;
int port;
int sockfd;
int sendnum;
int opt;
int recvnum;
char send_buf[2048];
char recv_buf[2048];
int addr_len = sizeof(struct sockaddr_in);
if ((1>argc)||(argc>2))
{
print_usage(argv[0]);
exit(1);
}
if (2==argc)
{
port = atoi(argv[1]);
}
memset(send_buf,0,2048);
memset(recv_buf,0,2048);
opt = SO_REUSEADDR;
if (-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0)))
{
perror("Create socket error!\n");
exit(1);
}
memset(&server,0,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port);
if (-1==bind(sockfd,(struct sockaddr *)&server,sizeof(struct sockaddr)))
{
perror("bind error\n");
exit(1);
}
while(1)
{
if (0>(recvnum = recvfrom(sockfd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&client,&addr_len)))
{
perror("recv error\n");
continue;
}
recv_buf[recvnum]='\0';
printf ("The message from the client is: %s\n",recv_buf);
if (0==strcmp(recv_buf,"quit"))
{
perror("The client break the server process.\n");
break;
}
sendnum = sprintf(send_buf,"The message from client is %s\n",recv_buf);
sendto(sockfd, send_buf, sizeof(send_buf), 0, (struct sockaddr *)&client, sizeof(client));
continue;
}
close(sockfd);
return 0;
}
udpclient
#include<stdlib.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define PORT 8900
void print_usage(char * cmd)
{
fprintf(stderr," %s usage:\n",cmd);
fprintf(stderr,"%s IP_Addr [port]\n",cmd);
}
int main(int argc,char** argv)
{
struct sockaddr_in server;
int ret;
int len;
int port;
int sockfd;
int sendnum;
int recvnum;
char send_buf[2048];
char recv_buf[2048];
int addr_len = sizeof(struct sockaddr_in);
if ((2>argc)|| (argc >3))
{
print_usage(argv[0]);
exit(1);
}
if (3==argc)
{
port = atoi(argv[2]);
}
if (-1==(sockfd=socket(AF_INET,SOCK_DGRAM,0)))
{
perror("can not create socket\n");
exit(1);
}
memset(&server,0,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons(port);
if (0>(ret=connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))))
{
perror("connect error");
close(sockfd);
exit(1);
}
while(1)
{
printf("What words do you want to tell to server:\n");
fgets(send_buf, 2048, stdin);
if (0>(len=sendto(sockfd,send_buf,sizeof(send_buf),0, (struct sockaddr *)&server, sizeof(server))))
{
perror("send data error\n");
close(sockfd);
exit(1);
}
if (0>(len=recvfrom(sockfd,recv_buf,sizeof(recv_buf),0, (struct sockaddr *)&server, &addr_len)))
{
perror("recv data error\n");
close(sockfd);
exit(1);
}
recv_buf[len]='\0';
printf("The message from the server is:%s\n",recv_buf);
}
close(sockfd);
}
编译
server:
gcc -o udps udpserver.c
./udps 8900
clients :
gcc -o udpc udoclient.c
./udpc ip 8900
|