-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex6-send.c
145 lines (115 loc) · 3.13 KB
/
ex6-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
/*---------------------------------------------------------------------------
* Programmazione di rete
*
* Semplicissimo sender UDP.
* Questo programma si aspetta in ingresso i parametri indicati qui di seguito:
*
* ./send indirizzoIP numero porta
*
*
*
* 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 type_pacchetto pacchetto;
/* vale 1 se l'utente vuole terminare */
int user_quit=0;
/*----------------------------------------------------------------------
* Initialize_socket( void )
*
* inizializza il socket e lo collega alla porta ed indirizzo IP voluti
*
*/
void initialize_socket(char *IPaddress, int port)
{
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);
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);
}