| | 1 | /* |
| | 2 | * Copyright 2008, Haiku, Inc. All rights reserved. |
| | 3 | * Distributed under the terms of the MIT License. |
| | 4 | * |
| | 5 | * Authors: |
| | 6 | * Vasilis Kaoutsis, kaoutsis@sch.gr |
| | 7 | */ |
| | 8 | |
| | 9 | |
| | 10 | /*! |
| | 11 | Server creates a socket to listen for the connection from client |
| | 12 | When the communication established, Server and Client exchange |
| | 13 | messages. |
| | 14 | |
| | 15 | Using socket() to create an endpoint for communication. It |
| | 16 | returns socket descriptor. Stream socket (SOCK_STREAM) is used here |
| | 17 | as opposed to a Datagram Socket (SOCK_DGRAM) |
| | 18 | Using bind() to bind/assign a name to an unnamed socket. |
| | 19 | Using listen() to listen for connections on a socket. |
| | 20 | Using accept() to accept a connection on a socket. It returns |
| | 21 | the descriptor for the accepted socket. |
| | 22 | */ |
| | 23 | |
| | 24 | |
| | 25 | #include <stdio.h> |
| | 26 | #include <stdlib.h> |
| | 27 | #include <string.h> |
| | 28 | #include <sys/types.h> |
| | 29 | #include <sys/socket.h> |
| | 30 | #include <unistd.h> |
| | 31 | #include <netinet/in.h> |
| | 32 | #include <netdb.h> |
| | 33 | |
| | 34 | |
| | 35 | int |
| | 36 | main(int argc, char* argv[]) |
| | 37 | { |
| | 38 | if (argc != 2) { |
| | 39 | printf("Usage: %s [server port]\n", argv[0]); |
| | 40 | exit(1); |
| | 41 | } |
| | 42 | |
| | 43 | const int serverPort = atoi(argv[1]); |
| | 44 | |
| | 45 | // Create a stream socket. |
| | 46 | int localSocketDescriptor = socket(AF_INET, SOCK_STREAM, 0); |
| | 47 | if (localSocketDescriptor == -1) { |
| | 48 | perror( "server: socket failed" ); |
| | 49 | exit(1); |
| | 50 | } |
| | 51 | |
| | 52 | struct sockaddr_in serverAddress; |
| | 53 | memset(&serverAddress, 0, sizeof(serverAddress)); |
| | 54 | serverAddress.sin_family = AF_INET; |
| | 55 | |
| | 56 | serverAddress.sin_port = htons(serverPort); |
| | 57 | // The address and the port are always stored |
| | 58 | // in network byte order. In particular, this means that is needed |
| | 59 | // a call to htons() on the number that is assigned to a port. |
| | 60 | // All address/port manipulation functions in the standard library |
| | 61 | // work in network byte order. |
| | 62 | |
| | 63 | serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); |
| | 64 | // There are several special addresses: INADDR_LOOPBACK (127.0.0.1) |
| | 65 | // always refers to the local host via the loopback device; |
| | 66 | // INADDR_ANY (0.0.0.0) means any address for binding; |
| | 67 | // INADDR_BROADCAST (255.255.255.255) means any host and |
| | 68 | // has the same effect on bind as INADDR_ANY for historical reasons. |
| | 69 | |
| | 70 | struct sockaddr_in clientAddress; |
| | 71 | memset(&clientAddress, 0, sizeof(clientAddress)); |
| | 72 | clientAddress.sin_family = AF_INET; |
| | 73 | |
| | 74 | int clientLength = sizeof(clientAddress); |
| | 75 | |
| | 76 | // Bind the socket to an internet port. |
| | 77 | if (bind(localSocketDescriptor, (const struct sockaddr*)&serverAddress, |
| | 78 | sizeof(serverAddress)) == -1 ) { |
| | 79 | perror( "server: bind failed" ); |
| | 80 | exit(1); |
| | 81 | } |
| | 82 | |
| | 83 | // Listen for clients. |
| | 84 | if (listen(localSocketDescriptor, 1) == -1 ) { |
| | 85 | perror( "server: listen failed" ); |
| | 86 | exit(1); |
| | 87 | } |
| | 88 | |
| | 89 | printf("Server is listening for clients to establish a connection\n"); |
| | 90 | |
| | 91 | int netSocketDescriptor = accept(localSocketDescriptor, |
| | 92 | (struct sockaddr*)&clientAddress, (socklen_t*)&clientLength); |
| | 93 | if (netSocketDescriptor == -1 ) { |
| | 94 | perror( "server: accept failed" ); |
| | 95 | exit(1); |
| | 96 | } |
| | 97 | |
| | 98 | printf("accept() successful! A client has connected! waiting for a message...\n"); |
| | 99 | char messageBuffer[64] = "Welcome to server!!\n"; |
| | 100 | write(netSocketDescriptor, messageBuffer, sizeof(messageBuffer)); |
| | 101 | |
| | 102 | |
| | 103 | char serverBuffer[512]; |
| | 104 | memset(serverBuffer, 0, sizeof(serverBuffer)); |
| | 105 | |
| | 106 | int bytesCounter; |
| | 107 | |
| | 108 | while (true) { |
| | 109 | // Read from network. |
| | 110 | bytesCounter = read(netSocketDescriptor, serverBuffer, sizeof(serverBuffer)); |
| | 111 | printf("client-> %s\n", serverBuffer); |
| | 112 | memset(serverBuffer, 0, sizeof(serverBuffer)); |
| | 113 | fflush(stdout); |
| | 114 | |
| | 115 | // Write to the network a response |
| | 116 | fprintf(stdout, "server-> "); |
| | 117 | fflush(stdout); |
| | 118 | read(STDIN_FILENO, serverBuffer, sizeof(serverBuffer)); |
| | 119 | fflush(stdin); |
| | 120 | write(netSocketDescriptor, serverBuffer, sizeof(serverBuffer)); |
| | 121 | memset(serverBuffer, 0, sizeof(serverBuffer)); |
| | 122 | } |
| | 123 | |
| | 124 | // FIXME: find a way to exit gracefully! |
| | 125 | |
| | 126 | close(netSocketDescriptor); |
| | 127 | close(localSocketDescriptor); |
| | 128 | |
| | 129 | return 0; |
| | 130 | } |