Ticket #2060: net_sockets.diff

File net_sockets.diff, 7.3 KB (added by kaoutsis, 17 years ago)
  • src/tests/kits/net/server_with_socket.cpp

     
     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
     35int
     36main(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}
  • src/tests/kits/net/client_with_socket.cpp

     
     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    Client creates a socket to connect to server.
     12    When the communication established, Client sends a small piece of data to server,
     13    server responses to client, etc.
     14*/
     15
     16
     17#include <stdio.h>
     18#include <sys/types.h>
     19#include <sys/socket.h>
     20#include <netinet/in.h>
     21#include <netdb.h>
     22#include <unistd.h>
     23#include <string.h>
     24#include <stdlib.h>
     25
     26
     27int
     28main(int argc, char* argv[])
     29{
     30
     31    if (argc != 3) {
     32        printf( "Usage: %s [hostname] [serverPort]\n", argv[0]);
     33        exit(1);
     34    }
     35
     36    const int serverPort = atoi(argv[2]);
     37
     38    struct sockaddr_in server_addr;
     39    memset(&server_addr, 0, sizeof(server_addr));
     40
     41    server_addr.sin_family = AF_INET;
     42    server_addr.sin_port = htons(serverPort);
     43    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     44
     45    struct hostent* hp;
     46
     47    // Get the host.
     48    hp = gethostbyname(argv[1]);
     49    if (hp == NULL ) {
     50        printf( "%s: %s unknown host\n", argv[0], argv[1] );
     51        exit(1);
     52    }
     53    printf("gethostbyname() successful!\n");
     54
     55    bcopy(hp->h_addr_list[0], (char*)&server_addr.sin_addr, hp->h_length);
     56
     57    // Create a socket.
     58    int sd = socket(AF_INET, SOCK_STREAM, 0);
     59    if (sd == -1) {
     60        perror( "client: socket failed" );
     61        exit(1);
     62    }
     63    printf("socket() successful!\n");
     64
     65    // Connect the socket.
     66    if (connect(sd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
     67        perror( "client: connect failed" );
     68        exit(1);
     69    }
     70
     71    fprintf(stdout, "connect() successful!\n");
     72    char serverMessage[64];
     73
     74    // Server will have a greating message, read it.
     75    read(sd, serverMessage, sizeof(serverMessage));
     76    fprintf(stdout, "%s\n", serverMessage);
     77
     78    char clientBuffer[512];
     79    memset(clientBuffer, 0, sizeof(clientBuffer));
     80
     81    while (true) {
     82        // Send a string to the server,
     83        // and show it to the screen too.
     84        fprintf(stdout, "client-> ");
     85        fflush(stdout);
     86        read(STDIN_FILENO, clientBuffer, sizeof(clientBuffer));
     87        fflush(stdin);
     88        write(sd, clientBuffer, sizeof(clientBuffer));
     89
     90        // Read from server
     91        memset(clientBuffer, 0, sizeof(clientBuffer));
     92        read(sd, clientBuffer, sizeof(clientBuffer));
     93        printf("server-> %s\n", clientBuffer);
     94        memset(clientBuffer, 0, sizeof(clientBuffer));
     95        fflush(stdout);
     96    }
     97
     98    // FIXME: find a way to exit gracefully!
     99
     100    close(sd);
     101    return 0;
     102}
  • src/tests/kits/net/Jamfile

     
    1111
    1212SimpleTest getpeername : getpeername.cpp : $(TARGET_NETWORK_LIBS) ;
    1313
     14SimpleTest server_with_socket : server_with_socket.cpp : $(TARGET_NETWORK_LIBS) ;
     15SimpleTest client_with_socket : client_with_socket.cpp : $(TARGET_NETWORK_LIBS) ;
     16
     17
    1418SubInclude HAIKU_TOP src tests kits net DialUpPreflet ;
    1519SubInclude HAIKU_TOP src tests kits net multicast ;
    1620SubInclude HAIKU_TOP src tests kits net netperf ;