-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3-recv.c
130 lines (102 loc) · 2.95 KB
/
ex3-recv.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*---------------------------------------------------------------------------
* Programmazione di rete
*
* Listener UDP : seconda versione
*
* Questo programma termina con un CRTL+C, entra in ciclo infinito.
* lanciare questo programma PRIMA di avviare il sender UDP.
*
* Ing. Alessio Palma
*
* $Id: ex3-recv.c,v 1.1 2002/05/28 10:39:54 alessiop Exp $
*
*/
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<sys/socket.h>
#include<sys/time.h>
main()
{
int socket_descriptor, i, bytes_read;
unsigned int counter;
struct sockaddr_in socket_address, remote_address;
socklen_t addr_len;
/*
* file descriptor
* serve per poter avere informazioni sullo stato del socket.
*
*/
fd_set fd_read;
/*
* timeval
* ci serve per indicare quanto tempo deve durare il controllo
* sullo stato del socket
*
*/
struct timeval timeout;
char buffer[ 255 ];
if ( (socket_descriptor = socket(AF_INET, SOCK_DGRAM,0)) < -0 ) {
printf("non posso aprire il socket\n");
exit(1);
}
socket_address.sin_family = AF_INET;
socket_address.sin_addr.s_addr = htonl(INADDR_ANY);
socket_address.sin_port = htons( 6601 );
if ( bind( socket_descriptor,
(struct sockaddr *) &socket_address, sizeof(socket_address) )
) {
printf("non posso collegare il socket\n");
exit(1);
}
/*
* Inizializziamo le struttura del timeout
* nessuna attesa. P.S: usec = microsecondi non milli.
*
*/
timeout.tv_sec = 0;
timeout.tv_usec = 0;
/*
* Inizializzazione della struttra File Description
*
*/
FD_ZERO( &fd_read );
for (i=0;;) {
/*
* Inizializziamo le struttura del file descriptor
*
* FD_SET associa la struttura al nostro socket.
*
*/
FD_SET ( socket_descriptor, &fd_read);
/*
* Interroga il socket
* Attenzione al +1
*
*/
if ( select( socket_descriptor+1, &fd_read, 0, 0, &timeout) == -1 ) {
printf("select error\n");
exit(1);
}
/*
* Ci sono dati?
*
* SI: allora leggiamoli, recvfrom non si bloccherà.
*
*/
if ( FD_ISSET( socket_descriptor, &fd_read) ) {
bytes_read = recvfrom( socket_descriptor, buffer, 255,0,(struct sockaddr *) &remote_address, &addr_len );
printf("> counter: %u :: letti %d bytes : %s\n", counter, bytes_read, buffer);
i++;
}
/*
* Il contatotore continua a camminare e questo significa che il
* programma non si blocca se non sono presenti dati nel socket.
*
*/
counter++;
}
close(socket_descriptor);
}