Linux平台C语言Socket编程练习之UDP套接字

0x00 要求

实现一个基于UDP协议的服务器-客户端程序,要求完成以下功能:

服务端

1
2
3
接收客户的连接请求,并发送欢迎信息,显示客户的IP地址和端口号;
循环接收接收客户传来的字符串,反转后传递给客户;

客户端

1
2
3
4
5
从命令行读入服务器的IP地址;并连接到服务器;
循环从命令行读入一行字符串,并传递给服务器,由服务器对字符串反转,并将结果返回客户程序,如果用户输入的是quit,则关闭连接;
客户程序显示反转后的字符串;

0x01 代码

服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 1234
#define MAXDATASIZE 100
int main()
{
int sockfd;
socklen_t client_len;
ssize_t recv_num,send_num;
struct sockaddr_in server;
struct sockaddr_in client;
char recv_buf[MAXDATASIZE];
char send_buf[MAXDATASIZE];
char response[MAXDATASIZE];
char wel_msg[]="[!] Welcome,I can return your input,you can input 'quit' to exit:)";
if((sockfd = socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socket() failed.");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=htonl(INADDR_ANY);
client_len=sizeof(client);
if((bind(sockfd,(struct sockaddr *)&server,sizeof(server)))==-1)
{
perror("bind() failed.");
exit(1);
}
if((recvfrom(sockfd,response,MAXDATASIZE,0,(struct sockaddr *)&client,&client_len))==-1)
{
perror("recvfrom() failed.");
exit(1);
}
printf("[!] You got a connection: message <%s> client ip <%s> port <%d>\n",response,inet_ntoa(client.sin_addr),ntohs(client.sin_port));
if((sendto(sockfd,wel_msg,sizeof(wel_msg),0,(const struct sockaddr *)&client,client_len))==-1)
{
perror("sendto() failed");
exit(1);
}
while(1)
{
if((recv_num=recvfrom(sockfd,recv_buf,MAXDATASIZE,0,(struct sockaddr *)&client,&client_len))==-1)
{
perror("recvfrom() failed.");
exit(1);
}
recv_buf[recv_num]='\0';
printf("[>] Receive client inputed string:%s\n",recv_buf);
printf("[*] Return client inputed string:%s\n",recv_buf);
strcpy(send_buf,recv_buf);
if((send_num=(sendto(sockfd,send_buf,sizeof(send_buf),0,(struct sockaddr *)&client,client_len)))==-1)
{
perror("sendto() failed");
exit(1);
}
if(!strcmp(recv_buf,"quit"))
{
printf("[!] Client request to exit.\n");
close(sockfd);
exit(1);
}
}
close(sockfd);
return 0;
}

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 1234
#define MAXDATASIZE 100
int main(int argc,char *argv[])
{
int sockfd;
ssize_t recv_num,send_num;
socklen_t server_len;
char recv_buf[MAXDATASIZE];
char send_buf[MAXDATASIZE];
char request[]="hello";
struct hostent *remote_host;
struct sockaddr_in server;
if(argc!=2)
{
printf("Usage:%s <IP Address>\n",argv[0]);
exit(1);
}
if((remote_host=gethostbyname(argv[1]))==NULL)
{
printf("gethostbyname() failed.");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socket() failed.");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr=*((struct in_addr *)remote_host->h_addr);
server_len=sizeof(server);
if(sendto(sockfd,request,sizeof(request),0,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("sendto() failed.");
exit(1);
}
if((recv_num=recvfrom(sockfd,recv_buf,MAXDATASIZE,0,(struct sockaddr*)&server,&server_len))==-1)
{
perror("recvfrom() failed.");
exit(1);
}
printf("%s\n",recv_buf);
while(1)
{
printf("[*] Please input ASCII string:");
scanf("%s",send_buf);
send_buf[strlen(send_buf)]='\0';
if((send_num=sendto(sockfd,send_buf,strlen(send_buf),0,(struct sockaddr *)&server,sizeof(server)))==-1)
{
perror("sendto() failed.");
exit(1);
}
if((recv_num=recvfrom(sockfd,recv_buf,MAXDATASIZE,0,(struct sockaddr*)&server,&server_len))==-1)
{
perror("recvfrom() failed.");
exit(1);
}
printf("[>] Server return string:%s\n",recv_buf);
if(!strcmp(recv_buf,"quit"))
{
printf("[!] You inputed 'quit' to exit.\n");
close(sockfd);
exit(1);
}
}
close(sockfd);
return 0;
}

0x02 演示

服务端

UDP 服务端.gif

客服端

UDP 客户端.gif

0x03 心得

UDP使用recvfrom函数接收数据,使用sendto函数发送数据,注意它们的传输的参数。