-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_udp_server.test.c
More file actions
35 lines (34 loc) · 937 Bytes
/
my_udp_server.test.c
File metadata and controls
35 lines (34 loc) · 937 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
int main(int argc,char* argv[]){
int sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock<0){
perror("sock");
exit(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");
exit(2);
}
char buf[1024];
struct sockaddr_in client;
while(1){
socklen_t len = sizeof(client);
size_t s = recvfrom(sock,buf,sizeof(buf)-1,0,(struct sockaddr*)&client,&len);
if(s> 0){
buf[s] = 0;
printf("client$: %s\n",buf);
}
sendto(sock,buf,strlen(buf),0,(struct sockaddr*)&client,sizeof(client));
}
}