-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
74 lines (64 loc) · 1.26 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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <strings.h>
#define SIZE 1014
#define STD_IN 0
void communicate(int fd)
{
char buff[SIZE];
while(1)
{
bzero(buff,SIZE);
printf("Enter Message\n");
read(STD_IN,buff,SIZE);
write(fd,buff,SIZE);
read(fd,buff,SIZE);
printf("Server Sent %s",buff);
if(strncmp("exit",buff,4) == 0)
{
printf("Exit...");
break;
}
}
}
int main()
{
int socket_fd;
struct sockaddr_in server;
// Create a socket
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == -1)
{
perror("socket creation failed\n");
exit(1);
}
else
{
printf("Socket created successfully...\n");
}
// Server Config
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(8080);
//Accepting
int connection_socket_fd = connect(socket_fd,(struct sockaddr *)&server,sizeof(server));
if(connection_socket_fd == 0)
{
printf("Connected...\n");
}
else
{
perror("Connection failed...");
exit(1);
}
communicate(socket_fd);
//Closing the connection
close(socket_fd);
return 0;
}