-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
83 lines (74 loc) · 2.07 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
80
81
82
83
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
char *port =malloc(sizeof(char) *16);
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Nie mogę utworzyć socketa");
}
puts("Socket utworzony");
if(argv[1] != NULL && argv[2] !=NULL){
port = strcpy(port,argv[2]);
int result_port = atoi(port);
printf("%d", result_port);
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_family = AF_INET;
server.sin_port = htons( result_port );
printf("Server IP: %s Port: %s\n", argv[1], argv[2]);
}else if(argv[2] ==NULL){
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
printf("Server IP: %s Port 8888\n", argv[1]);
}else{
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
printf("Server IP: 127.0.0.1 Port:8888\n");
}
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("conn. failed. Error");
return 1;
}
puts("Połączony\n");
//keep communicating with server
while(1)
{
//recive data
if( recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}
if (strcmp(server_reply, "quit")==0){
close(sock);
exit(0);
}
puts("Serwer :");
puts(server_reply);
memset(server_reply, 0, sizeof(server_reply));
printf("napisz wiadomość(help): ");
scanf("%s" , message);
//Send some data
if( send(sock , message , strlen(message)+1 , 0) < 0)
{
puts("Send failed");
return 1;
}
fflush(stdout);
}
close(sock);
free(port);
return 0;
}