Re: Socket Question




"Ali Hamad" <ali.hamad34@xxxxxxxxx> wrote in message news:mailman.1858.1222894062.3487.python-list@xxxxxxxxxxxxx
Hello All :

A socket question from a networking newbie. I need to create
a server that:

1) receive a message from client.
2) check that message and response to it.
3) the client get the server message and send another message.
4) finally, the server receive the message and close the connection.

I have successfully done this. However, I couldn't use the same socket
to send the second message
to the server. I have googled but all the examples are only for sending
one message and receiving the response.

in my client code, I have :

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 1888))
s.send("1st message")
response = s.recv(1024)
validate(response)
s.send("2nd message")
response2 = s.recv(1024)
s.close()

However, I got the first response just fine from the server but the
second message didn't get to the server.

So, the solution I came up with is to send the 1st message, close the
socket, create new socket,
and send the 2nd message.

I came up with something like :

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 1888))
s.send("1st message")
response = s.recv(1024)
s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 1888))
s.send("2nd message")
response = s.recv(1024)
s.close()

and it works !

My Question :

is it possible to send/receive from the same socket more than one message ?

Thank you for your assistance in advance,

Yes, you can send more than one message on a socket. The problem is likely in the server code. What does it look like? Also TCP is a streaming protocol. It has no concept of a start and end of message unless you implement something you can recognize as a complete message in the protocol.

-Mark

.



Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: ssh through vista connection sharing not working, NEW INFO: 802.1q headers from vista
    ... The ssh client is a linux laptop. ... connection with wireshark, and I see the response from the ssh server, ... Connection sharing is turned on in the Vista. ...
    (comp.os.linux.networking)
  • Re: Locking on async calls
    ... you must synchronize the entire SendMessage routine as an atomic ... operation to prevent mixed messages from being transmitted to the server. ... You are correct that read and write on the socket do not interfere with each ... If you want to handle multiple client connections from one server object ...
    (microsoft.public.dotnet.general)
  • Re: socket questions
    ... tcplistener, which is the server side, right? ... multiple connections use the same socket so it isn't as easy as listening to ... one channel for a response. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Design issue with WinSock/GetQueuedCompletionStatus
    ... delegate that to a shutdown routine called after all worker threads ... The application I've created is a server accepting connections on a few ... different TCP/IP ports and then lets the client run different commands. ... a TCP/IP socket can be closed for 2 different reasons: ...
    (microsoft.public.win32.programmer.networks)