473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Socket Question

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,
Oct 1 '08 #1
2 2067
Maybe you need to close the socket somewhere else, rather than to
close it when you receive the your response.
On 9ÔÂ30ÈÕ, ÉÏÎç7ʱ01·Ö, Ali Hamad <ali.hama...@gmail.comwrote:
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,
Oct 1 '08 #2

"Ali Hamad" <al*********@gmail.comwrote in message
news:ma**************************************@pyth on.org...
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

Oct 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gonçalo Rodrigues | last post by:
Hi, My setup is the following: I have socket s from which I want to read and write. So I made the following set up: There is a thread whose only job is to read. Any data read (from recv call)...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
6
by: Sean | last post by:
Hi Everyone, My apologies for a somewhat dump question but I am really stuck. I have been working on this code for two days straight I am dont know what is wrong with it. when I run the code, All...
6
by: billiejoex | last post by:
Hi there. I'm setting up test suite for a project of mine. situations, if the socket is closed on the other end or not. I noticed that I can "detect" such state if a call to socket.read() returns...
1
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.