Socket Question

A

Ali Hamad

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,
 
S

safecom

Maybe you need to close the socket somewhere else, rather than to
close it when you receive the your response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top