Thread: Socket Problem (with shell server)

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    2

    Socket Problem (with shell server)

    Hi!
    (firstly : sorry for my english, i posted my problem in a french forum but i diddn't found any answer )
    So:
    I have to make a shell in a server.
    The client has to connect to the server, ask some tasks to the server, the server has to resolve the task and then give the result to the client.
    The solution:
    I redirected stdout and stderr to the client, then when the client ask someting to the server, the server use execvp and then the client read the stdout of the execvp.
    The problem :
    Some tasks (like chmod) doesn't have any output.
    With those tasks, i have a big problem : The client waits a message from the server, but he receives nothing !
    I can't post my code because it's for the school, but i can show some importants parts of it :
    this is the main part of my client-Socket:
    Code:
    • fgets(requete,250,stdin);
    • while (strcmp(task,"exit\n" )){
    • write(sd,task,250);
    • read(sd,ligne,taille);
    • printf(ligne);
    • fgets(task,250,stdin);
    • }
    server :

    Code:
    • while(condition) {
    • i=0;
    • tokens[i]=strtok(ligne," \n" );
    • while (tokens[i] != NULL) tokens[++i]=strtok(NULL," \n" );
    • if (fork()==0)
    • {
    • fflush(stdout);
    • execvp(tokens[0],tokens);
    • fflush(stdout);
    • printf("invalid task\n" );
    • exit(1);
    • }
    • wait(0);
    • read(h, ligne, strlen(ligne));
    • }
    • }
    with the others tasks (like "echo hello world" everything works fine).

    Sorry again for my english, and thanks for helping

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    This is a problem that is generally solved in one of two ways: either by sending the size of the data followed by the data itself, or by sending the data followed by a terminating character or characters.

    The downside to the first solution is that you have to know in advance how much data you have, and the downside to the second solution is that your data cannot contain the terminating character sequence without being escaped somehow.

    Basically, you need to devise a simple protocol for the communication. It's up to you to figure out what makes sense for your application, assuming your assignment does not specify one.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you create a new (temporary) socket just to handle the stdout of the exec'ed program (use dup() to connect the socket fd to the stdout fd), then that temporary socket will close when the exec'ed program closes.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    2
    Hi, fisrtly thank you for ur answers !

    Quote Originally Posted by cas View Post
    This is a problem that is generally solved in one of two ways: either by sending the size of the data followed by the data itself, or by sending the data followed by a terminating character or characters.

    The downside to the first solution is that you have to know in advance how much data you have, and the downside to the second solution is that your data cannot contain the terminating character sequence without being escaped somehow.

    Basically, you need to devise a simple protocol for the communication. It's up to you to figure out what makes sense for your application, assuming your assignment does not specify one.
    I just redirect stdout of the server to the client, i can't sending the data followed by a terminating character :/ I don't know how to use one of those solutions with that :/

    If you create a new (temporary) socket just to handle the stdout of the exec'ed program (use dup() to connect the socket fd to the stdout fd), then that temporary socket will close when the exec'ed program closes.
    I have to use only 2 sockets

    Thank u again for ur help

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I only counted 2

    One socket is the control socket, where you send the commands.
    The other socket is the session socket which is created and destroyed for each command executed.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with server socket
    By KIBO in forum C# Programming
    Replies: 8
    Last Post: 02-08-2012, 07:07 AM
  2. Raw Socket Client and Server Communication Problem
    By rplumii in forum C Programming
    Replies: 6
    Last Post: 02-10-2011, 07:02 AM
  3. Problem with simple socket client/server program
    By spencer88 in forum C Programming
    Replies: 6
    Last Post: 05-05-2009, 11:05 PM
  4. BSD Socket Server Problem
    By ma_mazmaz in forum C Programming
    Replies: 3
    Last Post: 01-26-2009, 01:03 PM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM