Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // printf
- #include <stdlib.h> // exit
- #include <unistd.h> // close
- #include <errno.h> // errno
- #include <string.h> // strerror
- #include <netinet/in.h> // sockaddr_in
- #include <arpa/inet.h> // inet_ntop
- // Useful constants.
- #define SERVER_PORT 4567
- #define DATA_BUFFER_SIZE 256
- int main() {
- printf("Starting TCP Server\n\n");
- // Create the socket on which we will communicate
- // PF_INET -> Internet Protocol Family, SOCK_STREAM -> TCP
- // Last arg is for socket flags, which we do not set.
- int socket_descriptor = socket(PF_INET, SOCK_STREAM, 0);
- if(socket_descriptor < 0){
- perror("Can't create socket");
- exit(0);
- }
- // This will hold the address of our server
- struct sockaddr_in server_address;
- // Set up the internet address structure.
- server_address.sin_family = AF_INET; // Internet address family
- // htons correctly sets endianness of port number for the network protocol
- server_address.sin_port = htons(SERVER_PORT); // host-to-network-short
- // Set the actually address to a sort of wildcard, so we will listen on
- // any addresses that this host may have (server machines often have multiple
- // network cards!).
- server_address.sin_addr.s_addr = htonl(INADDR_ANY);
- // Now bind the socket to the address, so we can soon tell it to listen for
- // connections.
- int result = bind(
- socket_descriptor, // Socket to bind
- (struct sockaddr *) &server_address, // Address to bind to.
- sizeof (struct sockaddr_in)); // Size of server_address
- // Check the status of bind.
- if (result < 0) {
- perror("Could not bind to socket.");
- exit(1);
- }
- printf("Socket now bound to the all host address on port %d\n", SERVER_PORT);
- // Now put the socket into listening mode with a maximum queue length
- // of 5 connections, after which new connections will be rejected.
- listen(socket_descriptor, 5);
- // We're going to go into a loop soon, so that we can accept connections from
- // clients and then read some data from them, so we need to prepare some
- // variables for storing the sender's address and the incoming data.
- struct sockaddr_in sender_address;
- socklen_t sender_address_length;
- sender_address_length = sizeof(sender_address);
- char incoming_data[DATA_BUFFER_SIZE];
- printf("Listening for connections\n");
- // Loop for ever, accepting connections and responding to messages.
- while (1) {
- // Wait for then accept a connection, storing the client's address.
- int connected_socket = accept(
- socket_descriptor,
- (struct sockaddr*) &sender_address,
- &sender_address_length);
- if (connected_socket < 0) {
- perror("ERROR on accept");
- exit(1);
- }
- // Note: for a multi-threaded server (we'll look at threads soon) this is
- // where you would likely start a new thread, passing it
- // 'connected_socket' as an argument so that the server can continue to
- // accept new requests.
- // Wait for then receive some data from the client.
- int no_incoming_bytes = recv(
- connected_socket, // Note that we use the connected socket here not ...
- &incoming_data, // ... our listening socket.
- DATA_BUFFER_SIZE,
- 0);
- // Check...
- if (no_incoming_bytes < 0) {
- perror("Receive error");
- exit(1);
- }
- printf("Received: '%s'\n", incoming_data);
- // Get the sender's host and port in a format that we can easily print out.
- char ip_address[256];
- inet_ntop(
- AF_INET,
- &(sender_address.sin_addr), // Pass the address by reference.
- (char *) &ip_address, // It expects char* rather than char[]
- sizeof(ip_address));
- uint16_t port = ntohs(sender_address.sin_port); // Reverse of htons
- printf("Received some data from %s:%d\n", ip_address, port);
- // Reply to the client.
- char* response = "Hello from the Server";
- int no_bytes_writen = write(connected_socket,response,strlen(response)+1);
- if (no_bytes_writen != strlen(response)+1) {
- perror("Send error\n");
- exit(1);
- }
- // Close the client socket.
- close(connected_socket);
- }
- // Close the listening socket
- close(socket_descriptor);
- return 0;
- }
Add Comment
Please, Sign In to add comment