-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
54 lines (45 loc) · 1.24 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
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h> //easy conversions to binary form
#define PORT_NUM 8080
#define ADDRESS "127.0.0.1"
#define BUFF_SIZE 2048
int main()
{
struct sockaddr_in server_address;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("Couldn't initiate socket");
return EXIT_FAILURE;
}
//initialize server_address
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT_NUM);
if (inet_pton(AF_INET, ADDRESS, &server_address.sin_addr) <= 0)
{
printf("\n Address %s is not supported \n", ADDRESS);
return EXIT_FAILURE;
}
if (connect(sock, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
perror("Could not connect");
return EXIT_FAILURE;
}
char *init = "Hi, there!";
if (send(sock, init, strlen(init), 0) < 0)
{
perror("Error sending!");
return EXIT_FAILURE;
}
char buffer[BUFF_SIZE] = {0};
read(sock, buffer, BUFF_SIZE);
printf("rtn -> %s\n", buffer);
return 0;
}