Browse Source

Add 'tcpclient.c'

master
parsley 4 months ago
parent
commit
81e03b00da
  1. 37
      tcpclient.c

37
tcpclient.c

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
// socket creation
int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);
// specify address for socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect(network_socket, (struct sockaddr*) &server_address, sizeof(server_address));
//check for error with the connection
if (connection_status == -1){
printf("There was an error making a connection to the remote socket \n\n");
}
// recieve data from server
char server_response[256];
recv(network_socket, &server_response, sizeof(server_response), 0);
// print server's response
printf("The server sent the data: %s\n", server_response);
// close socket
close(sock);
return 0;
}
Loading…
Cancel
Save