-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverpitfall.c
58 lines (44 loc) · 1.12 KB
/
serverpitfall.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h> //nonblocking socket library
#include <sys/wait.h>
#include <arpa/inet.h>
#include <unistd.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sock, newsock, oldsock, n, i, status;
struct sockaddr_in server, cli;
unsigned int len;
sock = socket(AF_INET, SOCK_STREAM, 0);
if ( sock < 0 )
error("Socket : ");
oldsock = sock;
server.sin_family = AF_INET;
server.sin_port = htons(1026); //pitfall 4
server.sin_addr.s_addr = INADDR_ANY;
bzero(&server.sin_zero, 8);
//pitfall 3
int on,ret;
on = 1;
ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
//pitfall 5
ret = write ( sock, "Message 1", 50);
ret = write ( sock, "Message 2", 50);
len = sizeof(struct sockaddr_in);
if ((bind (sock, (struct sockaddr *)&server, len)) < 0 )
error("bind");
if ((listen (sock, 5)) < 0 )
error("listen");
if ((newsock = accept(sock, (struct sockaddr *)&cli, &len)) < 0)
error("accept");
}