-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9-send.c
181 lines (144 loc) · 4.12 KB
/
ex9-send.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*---------------------------------------------------------------------------
* Programmazione di rete
*
* Semplicissimo sender UDP.
* Questo programma si aspetta in ingresso i parametri indicati qui di seguito:
*
* ./send indirizzoIP numero porta
*
* Questa nuova versione trasmette in multicast e l'indirizzo IP fornito in riga
* di comando DEVE appartenere alla classe degli indirizzi IP multicast.
*
* Ing. Alessio Palma
*
* $Id$
*
*/
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<sys/socket.h>
/* contiene la struttura comune ai due moduli */
#include"esempi.h"
void initialize_socket( char *, int );
void read_message( void );
void send_message( void );
void ask_usrname( void );
/*------------------------------------------------------------------------
*
* Dichiarazioni variabili
*
*/
int socket_descriptor;
struct sockaddr_in socket_address, remote_address;
struct in_addr ipaddr;
/* prestate attenzione che il pacchetto che invia è della versione 2 */
struct type_pacchetto_v2 pacchetto;
/* vale 1 se l'utente vuole terminare */
int user_quit=0;
/*----------------------------------------------------------------------
parametri per l'impostazione multicast
uchar_ttl = 1 significa che il pacchetto rimane nella LAN
I pacchetti inviati sono ricevuti anche dal mittente
uchar_loop = 1;
*/
u_char uchar_ttl = 1;
u_char uchar_loop = 1;
/*----------------------------------------------------------------------
* Initialize_socket( void )
*
* inizializza il socket e lo collega alla porta ed indirizzo IP voluti
*
*/
void initialize_socket(char *IPaddress, int port)
{
/*
* Per trasmettere in multicast serve un indirizzo appropriato
*/
if(!IN_MULTICAST(htonl(inet_addr(IPaddress)))) {
printf("L'Indirizzo IP fornito deve appartenere alla classe MULTICAST\n");
exit(1);
}
if ( (socket_descriptor = socket(AF_INET, SOCK_DGRAM,0)) < 0 ) {
printf("non posso aprire il socket\n");
exit(1);
}
/*
* socket che il programma alloca ed userà
*
*/
socket_address.sin_family = AF_INET;
socket_address.sin_addr.s_addr = htonl(INADDR_ANY);
socket_address.sin_port = htons( 0 );
/*
* socket ed indirizzo IP verso cui spedire i dati
*
*/
remote_address.sin_family = AF_INET;
remote_address.sin_port = htons( port );
inet_aton( IPaddress, &remote_address.sin_addr );
memset(&remote_address.sin_zero, 0x00, 8);
/*
* attiva le opzioni per il multicasting
*/
if (setsockopt( socket_descriptor, IPPROTO_IP, IP_MULTICAST_TTL,
&uchar_ttl, sizeof(uchar_ttl)) ) {
printf("Non posso impostare \n");
exit(1);
}
if ( bind( socket_descriptor, (struct sockaddr *)&socket_address,
sizeof(socket_address) ) ) {
printf("non posso collegare il socket\n");
exit(1);
}
}
/*----------------------------------------------------------------------
* Initialize_socket( void )
*
* carica la struttura pacchetto.
*
*/
void read_message()
{
printf("messaggio:");
fflush(stdin);
fgets((char *)pacchetto.messaggio, MAXLEN_MSG, stdin );
user_quit = ( strncmp( pacchetto.messaggio , QUIT_STRING, 4) == 0 );
}
/*----------------------------------------------------------------------
* send_messagge( void )
*
* Invia la struttura pacchetto in rete.
*
*/
void send_message()
{
sendto( socket_descriptor, (void *)&pacchetto,
sizeof(pacchetto)/sizeof(char),0,
(struct sockaddr *)&remote_address,sizeof(remote_address));
}
/*----------------------------------------------------------------------
* ask_username( void )
*
* inizializza la struttura pacchetto con il nome dell'utente
*
*/
void ask_username()
{
fflush(stdin);
printf("Inserisci il tuo nickname:");
fgets((char *)&pacchetto.nome, MAXLEN_USRNAME, stdin);
printf("ok\n");
}
main(int argc, char **argv )
{
initialize_socket( argv[1], atoi(argv[2]));
ask_username();
while( ! user_quit ) {
read_message();
send_message();
}
close(socket_descriptor);
}