-
Notifications
You must be signed in to change notification settings - Fork 50
/
simple_udpserver.c
64 lines (57 loc) · 1.5 KB
/
simple_udpserver.c
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
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
/**
*filename:simple_udpserver.c
*purpose:基本编程步骤说明, 演示UDP编程的服务器端编程步骤
*/
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr,cliaddr;
int sockfd;
socklen_t clilen;
int len;
char buff[128];
/*创建socket,关键在于在SOCK_DGRAM*/
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
perror("socket");
exit(errno);
}
else
printf ("create socket.\n");
memset(&servaddr, 0, sizeof(struct sockaddr_in));
/*设置地址和信息*/
servaddr.sin_family = AF_INET;
if (argv[2])
servaddr.sin_port = htons(atoi(argv[2]));
else
servaddr.sin_port = htons(7838);
if (argv[1])
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
else
servaddr.sin_addr.s_addr = INADDR_ANY;
/*绑定地址和端口信息*/
if((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) == -1){
perror("bind");
exit(errno);
}else
printf("bind address to socket.\n\r");
/*循环接收数据*/
clilen = sizeof(cliaddr);
for( ; ; ){
len = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&cliaddr, &clilen);
if(len < 0){
perror("recvfrom");
exit(errno);
}
buff[len] = '\0';
printf ("收到来自%s:%d的消息:%s\n\r",
inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), buff);
}
return 0;
}