Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
apsrcreatix
GitHub Repository: apsrcreatix/Socket-Programming-With-C
Path: blob/master/03-tcp-ip-client-server/client.c
137 views
1
/*
2
Creating the TCP Client workflow in this program using C.
3
4
* Title : TCP client
5
* Name : Aditya Pratap Singh Rajput
6
* Subject : Network Protocols And Programming using C
7
Note : please consider the TYPOS in comments.
8
Thanks.
9
*/
10
11
// adding headers
12
#include <stdio.h>
13
#include <stdlib.h>
14
//for socket and related functions
15
#include <sys/types.h>
16
#include <sys/socket.h>
17
//for including structures which will store information needed
18
#include <netinet/in.h>
19
#include <unistd.h>
20
#define SIZE 1000
21
22
//main functions
23
int main()
24
{
25
int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
26
// server address
27
struct sockaddr_in serverAddress;
28
serverAddress.sin_family = AF_INET;
29
serverAddress.sin_port = htons(9002);
30
serverAddress.sin_addr.s_addr = INADDR_ANY;
31
32
// communicates with listen
33
connect(socketDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
34
35
char serverResponse[SIZE];
36
recv(socketDescriptor, &serverResponse, sizeof(serverResponse), 0);
37
printf("Ther server sent the data : %s", serverResponse);
38
39
//closing the socket
40
close(socketDescriptor);
41
return 0;
42
}
43
44