此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出。欢迎各位前来交流。(部分材料来源网络,若有侵权,立即删除)
本人博客所有文章纯属学习之用,不涉及商业利益。不合适引用,自当删除!
若被用于非法行为,与我本人无关
基于C语言采用UDP协议实现通信功能的程序
实验目的
客户端要求
- 客户端可多次向服务器发送数据;
- 客户端能够接收服务器端发送数据,并回显正确;
服务器端要求
- 运行端口可配置
- 将客户端发送来的消息正确显示,并将该消息发送给客户端;
- 支持多个客户端同时发送
实验环境
- vm下Seed Ubuntu
- C语言
实验代码
客户端
/**********************************************************\
* *\
* program name:tcpclient.c *
* Author:Liang gang & Hu Xiao-qin *\
* Funtion:This program is just a demostrate program *\
* to tell students how to use socket to *\
* make a tcp communation. *\
* Date: 03-06-2009 *\
* *\
***********************************************************/
#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>
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];
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);
}
len=sizeof(struct sockaddr);
while(1)
{
memset(send_buf,0,2048);
memset(recv_buf,0,2048);
printf("what words do you want to tell to server:\n");
gets(send_buf);
if (0>sendto(sockfd,send_buf,strlen(send_buf),0,(struct sockaddr*)&server,len))
{
perror("send data error\n");
close(sockfd);
exit(1);
}
if (0>(recvfrom(sockfd,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&server,&len)))
{
perror("recv data error\n");
close(sockfd);
exit(1);
}
printf("the message from the server is:%s\n",recv_buf);
}
close(sockfd);
}
服务端
/**********************************************************\
* *\
* program name:tcpserver.c *
* Author:Liang gang & Hu Xiao-qin *\
* Funtion:This program is just a demostrate program *\
* to tell students how to use socket to *\
* make a tcp communation. *\
* Date: 03-06-2009 *\
* *\
***********************************************************/
#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>
int main(int argc,char** argv)
{
struct sockaddr_in server;
struct sockaddr_in client;
int length;
int sockfd;
int port;
int connectd;
int sendnum;
int recvnum;
char send_buf[2048];
char recv_buf[2048];
if ((1>argc)|| (argc >2))
{
print_usage(argv[0]);
exit(1);
}
if (2==argc)
{
port = atoi(argv[1]);
}
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);
length = sizeof(struct sockaddr_in);
if (-1==bind(sockfd,(struct sockaddr *)&server,sizeof(struct sockaddr)))
{
perror("bind error\n");
exit(1);
}
while (1)
{
memset(send_buf,0,2048);
memset(recv_buf,0,2048);
if (0>(recvnum = recvfrom(sockfd,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&client,&length))<0)
{
perror("recv error\n");
close(sockfd);
exit(1);
}
printf ("the message from the client is: %s\n",recv_buf);
printf ("the message send to the client is: %s\n",recv_buf);
if (0>(sendto(sockfd,recv_buf,strlen(recv_buf),0,(struct sockaddr*)&client,length)))
{
perror("error occar in sending data\n");
close(sockfd);
exit(1);
}
}
close(sockfd);
}
编译过程
功能检验
- 编译完成后打开一组端口在多个客户端进行多次数据发送
- 更换另一组端口进行多个客户端多次发送数据
实验结果
- 在ubuntu上使用c语言进行编写实现了UDP通信,完成了给定的要求
- 客户端可多次向服务器发送数据;
- 客户端能够接收服务器端发送数据,并回显正确;
- 运行端口可配置,通过传参数进行端口修改;
- 将客户端发送来的消息正确显示,并将该消息发送给客户端;
- 支持多个客户端同时发送
end