in reply to Re^2: Socket hangs
in thread Socket hangs
First of all, navarro, what behaviour are you looking for? Your first version accepts a single connection from the server and expects to see a sequence of multiple commands being received over it, the second changes to one new socket accept per command, and the last one reverts to behaviour that is almost like the first one except that it has an additional bug (see below). Which one are you looking for?
If you want to receive multiple commands one after another over the same socket, then the client (the side that executes the command) will have to use some kind of scheme to mark the end of the results of that command so that the server knows to stop reading output and send a new command. You expressly do not want to use shutdown for the purpose in this case because you want the socket to stay open bidirectionally for the next command. In this case you should look at how some of the common internet protocols like SMTP do it: schemes like dot stuffing. Any kind of quoting scheme will do if the command output can contain arbitrary data, and if the command output cannot contain arbitrary data, then some marker that consists of impossible data for the command will do. When you receive this marker, you would exit from the read loop (it doesn't matter whether it's read or sysread). Your most recent example does not do this properly: it exits from the while loop (via last) after the first sysread unconditionally (you may as well have not had a loop there). You may or may not have finished collecting all of the output from the command at that point!
If, on the other hand, you want to accept only one command per socket and open a new socket for the next command, then it becomes easier. The client end (which you have not shown code for) should shutdown the socket after it has finished sending the command output and the server will then see an EOF in its read loop. This is analogous to what FTP does with its dedicated data TCP connection for each data transfer.
More importantly than any of this, you should seriously consider how secure all of this is! You have already noticed how you can send multiple commands by seperating them with a semicolon, which I guess means you are passing the commands to a shell on the client side, but even if you were not doing that, your client program appears to be a "please crack me" client. It connects to a server that is not required to authenticated itself, via an unprotected channel, and proceeds to execute any arbitrary command that comes over that socket. Are you sure you want to do this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Socket hangs
by navarro (Initiate) on Dec 23, 2005 at 19:55 UTC |