-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
55 lines (49 loc) · 1.66 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
// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int client_socket = 0; long valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
// Buffer to store the message sent by the server.
char buffer[1024] = {0};
// Creating socket file descriptor
// socket(domain, type, protocol)
// domain: AF_INET (IPv4 protocol)
// type: SOCK_STREAM (TCP)
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
// Allocate mem for create serv_addr
memset(&serv_addr, '0', sizeof(serv_addr));
// Define the address based on the spec of our TCP/IP socket.
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(client_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
// Has a client I send a message to the server.
send(client_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
// I wait for the server to send me a message and print it after.
valread = read( client_socket , buffer, 1024);
printf("%s\n",buffer );
return 0;
}