-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.c
46 lines (33 loc) · 1.41 KB
/
tcp_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
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h> //has APIs to create sockets
#include<sys/types.h>
#include<netinet/in.h> //has structure to store address information
#include <unistd.h> // close()
int main() {
// TCP Client
// Create a Socket
int network_socket; // socket descriptor
// AF_INET = domain of socket
// soc = type of socket; TCP socket (other is UDP socket)
// third is Protocol : 0=default=TCP(other is raw socket)
network_socket = socket(AF_INET,SOCK_STREAM, 0);
// Structure for connection address
struct sockaddr_in server_address;
server_address.sin_family = AF_INET; // type/family of connection address
server_address.sin_port = htons(8080); // remote connection port -> integer port put in right network order
server_address.sin_addr.s_addr = INADDR_ANY; // actual server address - we connect to any IP on local
// Connect to Other Side
// returns integer as to if the connectin was made(0) or not
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
if (connection_status == -1)
printf("There was an error making a connection to the remote server\n\n");
// Receive data from Server
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response), 0);
// print the data
printf("The server sent the data %s \n\n", server_response);
// close socket
close(network_socket);
return 0;
}