Skip to content

Commit 17527c0

Browse files
committed
added linux server
1 parent 53b8a56 commit 17527c0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

linuxServer.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <sys/types.h>
2+
#include <sys/socket.h>
3+
#include <unistd.h>
4+
#include <stdio.h>
5+
#include <netinet/in.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
10+
int main() {
11+
struct sockaddr_in addr;
12+
char buffer[1024];
13+
ssize_t recsize;
14+
socklen_t fromlen;
15+
16+
int sock = socket(AF_INET, SOCK_DGRAM, 0);
17+
18+
memset(&addr, 0, sizeof addr);
19+
addr.sin_family = AF_INET;
20+
addr.sin_port = htons(1100);
21+
addr.sin_addr.s_addr = htonl(INADDR_ANY);
22+
23+
fromlen = sizeof addr;
24+
25+
if (bind(sock, (struct sockaddr *)&addr, sizeof addr) == -1) {
26+
close(sock);
27+
exit(EXIT_FAILURE);
28+
}
29+
30+
printf("Server running ...\n");
31+
32+
for (;;) {
33+
recsize = recvfrom(sock, (void *)buffer, sizeof buffer, 0, (struct sockaddr *)&addr, &fromlen);
34+
printf("recsize: %d\n", (int)recsize);
35+
sleep(1);
36+
printf("datagra: %.*s\n", (int)recsize, buffer);
37+
}
38+
39+
close(sock);
40+
}
41+

0 commit comments

Comments
 (0)