Thread: Sockets API

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    3

    Sockets API

    Greetings,

    I am new to the forums and I want to say I have gained a pretty good foundation off of here the last two years or so. I have been accommodating myself with the sockets API and I wanted to try a project. I have a client and server as usual. Below is my server. It is meant to receive STDOUT from a remote client and "puts" it in the entirety to the screen. No matter how much I increase the buffer size, there is a cut off from my puts output. I run wireshark and tested in out on loopback and I can see my execve netstat in the TCP stream altogether as it should be. I am afraid I am not accounting for something.

    server:

    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<netinet/in.h>
    
    
    
    
    #define BUFFSIZE 6000
    
    
    int main( int argc, char *argv[] ) {
    
    
    if (argc < 2 || argc > 2) {
    puts("<port>");
    exit(1);
    }
    //Variables
    in_port_t servPort = atoi(argv[1]);
    char buffer[BUFFSIZE];
    
    
    
    
    
    
    //CREATE SOCKET
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    
    //STRUCTURES
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(servPort);
    
    
    //Bind
    
    
    bind(sock, (struct sockaddr *) &server, sizeof(server));
    
    
    //Listen
    
    
    listen(sock, 10);
    
    
    for (;;) {
    
    
    //CLIENTstruct
    struct sockaddr_in client;
    socklen_t clientLen = sizeof(client);
    
    
    
    
    //ACCEPT
    int cliSock;
    cliSock = accept(sock, (struct sockaddr *) &client, &clientLen);
    
    
    read(cliSock,buffer,BUFFSIZE);
    puts(buffer); 
    }
    
    
    return 0;
    
    }


    client:

    Code:
    #include<stdio.h>
    #include<sys/socket.h>
    #include<arpa/inet.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    
    
    
    
    int main(int argc, char *argv[]) {
    
    
    if (argc != 3) {
    printf("<server><port>\n");
    exit(1);
    }
    
    
    //Variables
    char *servIP = argv[1];
    in_port_t servPort = atoi(argv[2]);
    
    
    //CREATE SOCKET
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    
    //STRUCTURES
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    
    
    //CONVERTADDRESS
    inet_pton(AF_INET, servIP, &server.sin_addr.s_addr);
    server.sin_port = htons(servPort);
    
    
    
    
    
    
    
    
    //CONNECT
    connect(sock, (struct sockaddr *) &server, sizeof(server));
    
    
    
    
    //DUP2 SEND STDIN AND ERR OVER SOCKET
    dup2(sock, STDOUT_FILENO);
    dup2(sock, STDERR_FILENO);
    
    
    
    
    //EXECVE LINUX SYSTEM CALL
    char *execargv[] = {"/bin/netstat", "-an",  NULL };        //Initialize and fill arrays with exec values
    char *environ[] = {NULL};
    execve("/bin/netstat", execargv, environ);                 //perform execve
    
    
    return 0;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well you're ignoring a whole load of return results for one thing.

    Also, read doesn't append a \0, like higher level operations such as fgets()

    So perhaps
    Code:
    ssize_t r = read(cliSock,buffer,BUFFSIZE-1);  // -1 allows room for the \0
    if ( r > 0 ) {
        buffer[r] = '\0';
        puts(buffer);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    3
    I am ignoring the return results just to expedite this, I am going to add them in later. I attempted to add in your suggestions and compile it. I get the same result, of getting a partial netstat.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > I am ignoring the return results just to expedite this
    And a fantastic job you're making of it as well.

    Look at your loop - you have accept + read + accept + read + accept + read....

    Code:
        ssize_t r;
        while ( (r=read(cliSock,buffer,BUFFSIZE))>0 ) {
          //!!buffer[r] = 0;
          //!!puts(buffer); // writes a \n as well
          write(STDOUT_FILENO,buffer,r);
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    3
    You are a gentlemen and a scholar. Mr. Salem you have been the first person to assist me on here so thank you very much. I will go back and write this a little neater. I just needed to work that out. I'm just self taught, so taking this upon myself, there are holes in my knowledge and logic that doesn't make sense. I laughed at your last comment and can appreciate your humorous perspective.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GTK + sockets help
    By Annonymous in forum C Programming
    Replies: 1
    Last Post: 04-21-2012, 09:29 PM
  2. Sockets tutorial, datagram sockets example not working for me
    By caesius in forum Networking/Device Communication
    Replies: 14
    Last Post: 12-26-2009, 03:40 PM
  3. sockets in c
    By St0rM-MaN in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-15-2007, 03:03 AM
  4. Sockets
    By devour89 in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 11:09 AM
  5. sockets
    By eldian() in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 07:08 AM