-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_C.c
109 lines (90 loc) · 2.67 KB
/
server_C.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Per compilare il codice, spostarsi nella directory dove sono presenti i file sorgenti.
Per creare un eseguibile di nome "server", dove "server" e` il nome dell'eseguibile creato mentre "server_C.c" e` il nome del file sorgente:
$ gcc -o server server_5C.c
Da riga di comando eseguo il server:
$ ./server
Apro un'altra finestra e da riga di comando instauro una connessione con telnet ed invio il messaggio:
$ telnet localhost 52000
Apro un'altra finestra e da riga di comando verifico l'effettiva creazione del socket e connessione:
$ netstat -tunape | grep 52000
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT_NO 52000
/* Prototipi delle funzioni/procedure */
int socket (int domain, int type, int protocol);
int bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen);
int listen (int sockfd, int backlog);
int accept (int sockfd, struct sockaddr *addr, socklen_t * addrlen);
int close(int fd);
ssize_t recv (int sockfd, void *buf, size_t len, int flags);
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
/* */
//struct studente{
//char nome[256];
//int eta;
//int ID;
//}
int
main ()
{
//struct studente stud1, stud2;
//stud1.eta = 20;
//stud2.eta = 20;
int sockfd, backlog = 6, new_sockfd, client_l;
char pippo[256];
ssize_t mess_recv, mess_send;
struct sockaddr_in server_addr, client_addr;
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
printf ("Error during socket creation...\n");
exit (EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons (PORT_NO);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind (sockfd, (struct sockaddr *) &server_addr, sizeof (server_addr)) < 0)
{
printf ("Error during bind...\n");
exit (EXIT_FAILURE);
}
if (listen (sockfd, backlog) < 0)
{
printf ("Error during listen...\n");
exit (EXIT_FAILURE);
}
//int stud_l;
//stud_l = sizeof(stud2);
client_l = sizeof (client_addr);
new_sockfd = accept (sockfd, (struct sockaddr *) &client_addr, &client_l);
if (new_sockfd < 0)
{
printf ("Error during accept...\n");
exit (EXIT_FAILURE);
}
mess_recv = recv (new_sockfd, pippo, 255, 0);
if (mess_recv < 0)
{
printf ("Error during recv...\n");
exit (EXIT_FAILURE);
}
printf ("The message is: %s\n", pippo);
mess_send = send (new_sockfd, "Message received!", 18, 0);
if (mess_send < 0)
{
printf ("Error during send...\n");
exit (EXIT_FAILURE);
}
if (close (sockfd) < 0)
{
printf ("Error during close...\n");
exit (EXIT_FAILURE);
}
return 0;
}