-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.c
79 lines (66 loc) · 2 KB
/
client.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include "client_comm.h"
#include "string.h"
#define TCP_PORT 8787
#define QUEUE_SIZE 10
#define POOL_SIZE 4
void ctrl_c_signal_handler(int sig_num);
int main(int argc, char *argv[]){
int tcp_port = 0;
int sockid;
if((sockid = socket(AF_INET,SOCK_STREAM,0))<0)
{
fprintf(stderr, "Error: Client Socket can not be created !! \n");
fprintf(stderr, "errno : %d\n", errno);
return -1;
}
char *server_addr = "127.0.0.1";
char *file = "client.c";
int opt;
while ((opt = getopt (argc, argv, "p:i:f:")) != -1){
switch(opt){
case 'p':
tcp_port = atoi(optarg);
break;
case 'i':
server_addr = optarg;
break;
case 'f':
file = optarg;
break;
case '?':
//getopt gives ? if it is unable to recoganize an option
fprintf(stderr, "Error: wrong format\n");
printf("Usage ./client [-p tcp_port_server] [-i ip_addr_server] [-f file_to_transfer]\n");
return -1;
break;
}
}
if(tcp_port == 0)
tcp_port = TCP_PORT;
printf("Client Sending TCP REQUEST on \n IP \t: %s\n PORT\t: %d\n", server_addr, tcp_port);
struct sockaddr_in serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port=htons(tcp_port);
if(inet_pton(AF_INET, server_addr, &serv_addr.sin_addr) <= 0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if(connect(sockid, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
printf("Server connected\n");
client_comm *temp = client_init(sockid,file,server_addr);
client_run(temp);
return 0;
}