Last change
on this file since 35 was 2, checked in by Rick van der Zwet, 15 years ago |
Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)
|
File size:
1.5 KB
|
Line | |
---|
1 | /* Netlab 2009 - group 1 - assignment 3
|
---|
2 | * Johan IJsveld
|
---|
3 | * Rick van der Zwet
|
---|
4 | */
|
---|
5 | #include <sys/types.h>
|
---|
6 | #include <netdb.h>
|
---|
7 | #include <sysexits.h>
|
---|
8 | #include <stdio.h>
|
---|
9 | #include <string.h>
|
---|
10 | #include <unistd.h>
|
---|
11 |
|
---|
12 | #include <sys/socket.h>
|
---|
13 | #include <netinet/ip.h>
|
---|
14 | #include <netinet/tcp.h>
|
---|
15 |
|
---|
16 | #define LISTEN_BACKLOG 0
|
---|
17 | #define BUF_SIZE 1000
|
---|
18 |
|
---|
19 |
|
---|
20 | #include <signal.h>
|
---|
21 |
|
---|
22 |
|
---|
23 | int main() {
|
---|
24 | int conn_fd;
|
---|
25 | char buf[BUF_SIZE];
|
---|
26 | ssize_t recv_size;
|
---|
27 | int retno;
|
---|
28 |
|
---|
29 | /* Create a tcp/ip socket */
|
---|
30 | int mysocket = socket(PF_INET, SOCK_STREAM, 0);
|
---|
31 |
|
---|
32 | struct sockaddr_in my_addr;
|
---|
33 | struct sockaddr_in their_addr;
|
---|
34 | int sin_size;
|
---|
35 |
|
---|
36 |
|
---|
37 | // ip(7) Create listen port, NOTE addresses in byte-order BYTEORDER(3)
|
---|
38 | my_addr.sin_family = AF_INET; // By default
|
---|
39 | my_addr.sin_port = htons(12345);
|
---|
40 | my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
41 | memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
|
---|
42 |
|
---|
43 | // get connected
|
---|
44 | retno = bind(mysocket, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in));
|
---|
45 | retno = listen(mysocket, LISTEN_BACKLOG);
|
---|
46 | sin_size = sizeof(struct sockaddr_in);
|
---|
47 |
|
---|
48 | // Make it a server, e.g. never ending
|
---|
49 | while(1) {
|
---|
50 | conn_fd = accept(mysocket, (struct sockaddr *)&their_addr, &sin_size);
|
---|
51 | if (fork() == 0) {
|
---|
52 | // child will help to echo the stuff
|
---|
53 | while(1) {
|
---|
54 | retno = recv(conn_fd, &buf, BUF_SIZE, 0);
|
---|
55 | printf("%s\n", buf);
|
---|
56 | if (retno <= 0)
|
---|
57 | break;
|
---|
58 | retno = send(conn_fd, &buf, retno, 0);
|
---|
59 | if (retno <= 0)
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | close(conn_fd);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | close(mysocket);
|
---|
66 |
|
---|
67 |
|
---|
68 | return EX_OK;
|
---|
69 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.