-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.c
39 lines (29 loc) · 1.21 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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main() {
//Creating a socket
int network_socket;
network_socket=socket(AF_INET, SOCK_STREAM, 0);
//Specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(3000);
server_address.sin_addr.s_addr = INADDR_ANY; //connects to 0.0.0.0 automatically
//Call the connect function
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
//Check for error with the connection
if(connection_status == -1){
printf("Error detected.");
}
//Receive data from the server
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response) , 0);
//Print the server's response
printf("Data received from server: %s\n", server_response);
//Close the socket
close(network_socket);
return 0;
}