Browse Source

Add 'tcp_server.c'

master
parsley 4 months ago
parent
commit
a67ef5a9bc
  1. 37
      tcp_server.c

37
tcp_server.c

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
int main() {
// create to hold data being sent to clients.
char server_message[256] = "You have reached the server!";
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
// define address structure
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;
// bind the socker to our specified IP and PORT
bind(server_socket, (struct socketaddr*) &server_address, sizeof(server_address));
listen(server_socket, 5);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
send(client_socket, server_message, sizeof(server_message), 0);
//close the socket
close(server_socket);
return 0;
}
Loading…
Cancel
Save