1
1
#include "../common/common.h"
2
2
#include "client.h"
3
3
4
-
5
-
6
4
int main (int argc , char const * argv [])
7
- {
8
- if (argc != 3 ){
5
+ {
6
+ if (argc != 3 )
7
+ {
9
8
printf ("Error syntax : ./clientUCP <ip_server> <port_server>\n" );
10
9
return 1 ;
11
10
}
12
11
13
12
// get IP andd port from cmd line aregument
14
- const char * IP_SERVER = argv [1 ];
13
+ const char * IP_SERVER = argv [1 ];
15
14
int PORT = atoi (argv [2 ]);
16
15
char userInput [100 ];
17
16
18
-
19
17
// init the addresses
20
18
struct sockaddr_in serverAddress ;
21
19
22
20
// create socket
23
- int clientSocket = (int ) malloc (sizeof (int ));
21
+ int clientSocket = (int )malloc (sizeof (int ));
24
22
clientSocket = socket (AF_INET , SOCK_DGRAM , 0 );
25
23
26
24
if (clientSocket == -1 )
@@ -37,71 +35,53 @@ int main(int argc, char const *argv[])
37
35
38
36
// assign IP, PORT
39
37
serverAddress .sin_port = htons (PORT );
40
- inet_aton (IP_SERVER ,(struct in_addr * ) serverAddress .sin_addr .s_addr );
38
+ inet_aton (IP_SERVER , (struct in_addr * )serverAddress .sin_addr .s_addr );
41
39
serverAddress .sin_family = AF_INET ;
42
40
43
41
socklen_t addressLength = sizeof (serverAddress );
44
42
45
43
// connect
46
- if (UDPConnect (clientSocket , & serverAddress ,addressLength ))
44
+ if (UDPConnect (clientSocket , & serverAddress , addressLength ))
47
45
{
48
46
info ("HANDSHAKE" , "handshake completed 🤝" ); //receiveSYN
49
47
}
50
48
else
51
49
{
52
- info ("HANDSHAKE" ,"failed to connect to server" );
50
+ info ("HANDSHAKE" , "failed to connect to server" );
53
51
}
54
52
55
-
56
- // get user input
57
- printf ("enter data to be sent over\n" );
58
- gets (userInput );
59
-
60
- //write data into the socket
61
- int writeValue = write (clientSocket , userInput , sizeof (userInput ));
62
- if (writeValue < 0 )
63
- {
64
- printf ("writing failed...\nwrite value: %d\n" , writeValue );
65
- return 1 ;
66
- }
67
- else
68
- {
69
- printf ("data written with success 🎉...\n" );
70
-
71
- }
53
+ recvFile (clientSocket , & serverAddress , addressLength );
72
54
73
55
close (clientSocket );
74
56
75
57
return 0 ;
76
58
}
77
59
78
-
79
- int UDPConnect ( int clientSocket , struct sockaddr_in * clientAddress , int clientLengthUDP ) {
60
+ int UDPConnect ( int clientSocket , struct sockaddr_in * clientAddress , int clientLengthUDP )
61
+ {
80
62
81
63
//send SYN
82
- if (sendto (clientSocket , SYN , sizeof (SYN ),0 , (const struct sockaddr * ) clientAddress , clientLengthUDP ) > 0 )
64
+ if (sendto (clientSocket , SYN , sizeof (SYN ), 0 , (const struct sockaddr * )clientAddress , clientLengthUDP ) > 0 )
83
65
{
84
66
printf ("[CONNECT] SYN sent 🎉...\n" );
85
-
86
67
}
87
68
else
88
69
{
89
70
perror ("SYN couldn't be sent...\n" );
90
71
return 0 ;
91
72
}
92
73
//receive SYNACK
93
- if (isReceive (SYNACK ,clientSocket , clientAddress ,clientLengthUDP ))
74
+ if (isReceive (SYNACK , clientSocket , clientAddress , clientLengthUDP ))
94
75
{
95
76
printf ("[CONNECT] SYNACK received properly 🎉🎉...\n" );
96
-
97
77
}
98
78
else
99
79
{
100
80
perror ("SYNACK reception failed...\n" );
101
81
return 0 ;
102
82
}
103
83
//send ACK
104
- if (sendto (clientSocket , ACK , sizeof (ACK ), 0 , (const struct sockaddr * ) clientAddress , clientLengthUDP ) > 0 )
84
+ if (sendto (clientSocket , ACK , sizeof (ACK ), 0 , (const struct sockaddr * )clientAddress , clientLengthUDP ) > 0 )
105
85
{
106
86
printf ("[CONNECT] ACK sent 🎉🎉🎉...\n" );
107
87
}
@@ -114,3 +94,61 @@ int UDPConnect(int clientSocket, struct sockaddr_in * clientAddress, int clientL
114
94
return 1 ;
115
95
}
116
96
97
+ int recvFile (int clientSocket , struct sockaddr_in * clientAddress , int clientLengthUDP )
98
+ {
99
+ // rcv header
100
+ char header [18 ];
101
+ if (recvfrom (clientSocket , header , sizeof (header ), 0 , (struct sockaddr * )clientAddress , (socklen_t * )& clientLengthUDP ) > 0 )
102
+ {
103
+ info ("RCVFILE" , "Received header 🎉..." );
104
+ char fileSizeChar [6 ];
105
+ int i = 9 ;
106
+ while (header [i ] != NULL ){
107
+ fileSizeChar [i - 9 ] = header [i ];
108
+ i ++ ;
109
+ }
110
+ int fileSize = atoi (fileSizeChar );
111
+ char fileBuffer [fileSize ];
112
+ int numberSegments = fileSize / 1020 ;
113
+
114
+ bzero (fileBuffer , sizeof (fileBuffer ));
115
+
116
+ for (int segmentID = 0 ; segmentID < numberSegments ; segmentID ++ ){
117
+ char segmentBuffer [1020 ];
118
+ if (recvfrom (clientSocket , segmentBuffer , sizeof (segmentBuffer ), 0 , (struct sockaddr * )clientAddress , (socklen_t * )& clientLengthUDP ) > 0 )
119
+ {
120
+ char segmentIDrcvChar [3 ];
121
+ int segmentIDrcv = 0 ;
122
+ for (int i = 0 ; i < 3 ; i ++ )
123
+ {
124
+ segmentIDrcvChar [i ] = segmentBuffer [i ];
125
+ printf ("%d" , i );
126
+ }
127
+ segmentIDrcv = (int ) atoi (segmentIDrcvChar [2 ]);
128
+ printf ("[RCVFILE] received segment %d\n" , segmentIDrcv );
129
+ char ack [6 ];
130
+ bzero (ack , sizeof (ack ));
131
+
132
+ sprintf (ack , "ACK%d" , segmentIDrcv );
133
+ if (sendto (clientSocket , ack , sizeof (ack ), 0 , clientAddress , clientLengthUDP ) > 0 )
134
+ {
135
+ info ("ACK" ,"Ack sent..." );
136
+ }
137
+ else
138
+ {
139
+ info ("ACK" ,"ACK couldn't be sent..." );
140
+ exit (1 );
141
+ }
142
+
143
+ }
144
+ }
145
+
146
+ info ("RCVFILE" , "Reciving segmented files ..." );
147
+ }
148
+ else
149
+ {
150
+ info ("RCVFILE" , "Header reception failed..." );
151
+ exit (1 );
152
+ }
153
+ return 1 ;
154
+ }
0 commit comments