-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
61 lines (55 loc) · 1.79 KB
/
server.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
/* Example: server.c receiving and sending datagrams on system generated port in UDP */
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MAX_MES_LEN 1024
main(void) /* server program called with no argument */
{
int sock, namelen, buflen;
char srv_buf[MAX_MES_LEN];
struct sockaddr_in name;
/*create socket*/
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0) {
perror("opening datagram socket");
exit(1);
}
/* create name with parameters and bind name to socket */
name.sin_family = AF_INET;
name.sin_port = 0;
name.sin_addr.s_addr = INADDR_ANY;
if(bind(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
perror("getting socket name");
exit(2);
}
namelen=sizeof(struct sockaddr_in);
/* Find assigned port value and print it for client to use */
if(getsockname(sock, (struct sockaddr *)&name, &namelen) < 0){
perror("getting sock name");
exit(3);
}
printf("Server waiting on port # %d\n", ntohs(name.sin_port));
/* waiting for connection from client on name and print what client sends */
namelen = sizeof(name);
buflen = MAX_MES_LEN;
if(recvfrom(sock, srv_buf, buflen, 0, (struct sockaddr *)&name, &namelen) < 0) {
perror("error receiving");
exit(4);
}
printf("Server receives: %s\n", srv_buf);
/* server sends something back using the same socket */
bzero(srv_buf, MAX_MES_LEN);
strcpy(srv_buf, "Hello back in UDP from server");
printf("Server sends: %s\n", srv_buf);
if(sendto(sock, srv_buf, buflen, 0, (struct sockaddr *)&name, namelen) < 0) {
perror("sending datagram");
exit(5);
}
/* server terminates connection, closes socket, and exits */
close(sock);
exit(0);
}