-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
33 lines (32 loc) · 927 Bytes
/
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
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main(int argc, char const* argv[])
{
char buffer[1024] = { 0 };
int status, valread, client_fd;
struct sockaddr_in serv_addr;
char* value = "Message from client";
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8080);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if ((status = connect(client_fd, (struct sockaddr*)&serv_addr,sizeof(serv_addr))) < 0) {
printf("\nConnection Failed \n");
return -1;
}
send(client_fd, value, strlen(value), 0);
printf("Message sent from client\n");
valread = read(client_fd, buffer,1024 - 1);
printf("%s\n", buffer);
close(client_fd);
return 0;
}