-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
68 lines (53 loc) · 1.48 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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include<netinet/in.h>
int main(int argc, char** argv)
{
int alice,bob;
char *username;
username=(char*)malloc(21*sizeof(char));
char *connection_name;
connection_name=(char*)malloc(21*sizeof(char));
alice = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_alice;
server_alice.sin_family=AF_INET;
server_alice.sin_port=htons(atoi(argv[2]));
server_alice.sin_addr.s_addr=inet_addr(argv[1]);
bzero(&server_alice.sin_zero, 8);
if(connect(alice, (struct sockaddr*)&server_alice, sizeof(struct sockaddr_in)) == -1){
printf("Connection could not be established!");
exit(-1);
}
printf("Enter a temporary username (Not more than 20 characters): ");
scanf("%s",username);
//name exchange
recv(alice, connection_name, 21, 0);
send(alice, username, strlen(username), 0);
printf("Connected to %s.....\n",connection_name);
char *buffer;
buffer=(char*)malloc(350*sizeof(char));
while(1){
bzero(buffer, sizeof(buffer));
printf("%s: ", username);
scanf("%s",buffer);
send(alice, buffer, strlen(buffer), 0);
if((strncmp(buffer,"exit",4)) == 0){
printf("Exiting....\n");
break;
}
bzero(buffer, sizeof(buffer));
recv(alice, buffer, 350, 0);
printf("%s: %s\n", connection_name, buffer);
if((strncmp(buffer,"exit",4)) == 0){
printf("Exiting....\n");
break;
}
}
close(alice);
}