-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
65 lines (52 loc) · 2.42 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
//*********************************************************************************************
//Header files. *
//*********************************************************************************************
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
//*********************************************************************************************
//Defining Port number as constant *
//*********************************************************************************************
#define PORT 7820
//*********************************************************************************************
//Main Function *
//*********************************************************************************************
int main() {
struct sockaddr_in server_address;
int sock = 0;
int value = 6;
//Creating socket by calling socket function.
sock = socket(AF_INET, SOCK_STREAM, 0);
//Check whether the socket is created or not. If it is not created then display error message.
if(sock < 0) {
printf("Error creating socket\n");
return -1;
}
//Setting the server address value to 0's
memset(&server_address, 0, sizeof(server_address));
//Assigning Port number and address family to the socket.
server_address.sin_port = htons(PORT);
server_address.sin_family = AF_INET;
//Check whether the server address is valid or not. If it is invalid then display the error message.
if(inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr) <= 0) {
printf("Check the address\n");
return -1;
}
//Check whethe the socket is connected to the server or not. If it is not connected then display the error message.
if(connect(sock, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
printf("Cannot connect to the server \n");
return -1;
}
//send data to socket
write(sock, &value, sizeof(int));
//read data from socket
read(sock, &value, sizeof(int));
//Display the integer received from server.
printf("The integer received back from the server is %d\n", value);
return 0;
}