By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,182 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,182 IT Pros & Developers. It's quick & easy.

Re: Socket and cycle problem

P: n/a
On Mon, 12 May 2008 08:34:07 -0700 (PDT), pe********@gmail.com wrote:
>Hello,
I am beginner but so I need help. I have small script for receive data
from port 3883, but it print only once.

import socket

HOST = 'localhost'
PORT = 3883
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(2048)
s.close()
print 'receive data from server:', `data`

So I try to write cycle to this script, like this:

import socket

HOST = 'localhost'
PORT = 3883
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while 1:
s.connect((HOST, PORT))
data = s.recv(2048)
s.close()
print 'receive data from server:', `data`

But Python reporting:

Traceback (most recent call last):
File "C:\Documents and Settings\poupa\Plocha\TCP3.py", line 7, in
<module>
s.connect((HOST, PORT))
File "<string>", line 1, in connect
File "C:\Python25\lib\socket.py", line 141, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: (9, 'Bad file descriptor')

Where is the mistake? I dont know.
You cannot reconnect a socket. You need to create a new one for each
connection. It's also almost certainly the case that the way you are
receiving data is incorrect. There is no guarantee that you will get
2048 bytes from socket.recv(2048). It isn't even guaranteed that all
the bytes written to the socket by the peer will be returned by such
a call. Instead, you need a framing protocol to determine when all
data has been received. For example, you might length prefix the
data, or you might insert a delimiter (or a terminator) at the end. Or
if there is exactly one message to receive, then you should just read
until the recv call returns '', indicating EOF.

Jean-Paul
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 12 Kvě, 17:54, Jean-Paul Calderone <exar...@divmod.comwrote:
On Mon, 12 May 2008 08:34:07 -0700 (PDT), petr.po...@gmail.com wrote:
Hello,
I am beginner but so I need help. I have small script for receive data
from port 3883, but it print only once.
import socket
HOST = 'localhost'
PORT = 3883
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(2048)
s.close()
print 'receive data from server:', `data`
So I try to write cycle to this script, like this:
import socket
HOST = 'localhost'
PORT = 3883
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while 1:
s.connect((HOST, PORT))
data = s.recv(2048)
s.close()
print 'receive data from server:', `data`
But Python reporting:
Traceback (most recent call last):
File "C:\Documents and Settings\poupa\Plocha\TCP3.py", line 7, in
<module>
s.connect((HOST, PORT))
File "<string>", line 1, in connect
File "C:\Python25\lib\socket.py", line 141, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: (9, 'Bad file descriptor')
Where is the mistake? I dont know.

You cannot reconnect a socket. You need to create a new one for each
connection. It's also almost certainly the case that the way you are
receiving data is incorrect. There is no guarantee that you will get
2048 bytes from socket.recv(2048). It isn't even guaranteed that all
the bytes written to the socket by the peer will be returned by such
a call. Instead, you need a framing protocol to determine when all
data has been received. For example, you might length prefix the
data, or you might insert a delimiter (or a terminator) at the end. Or
if there is exactly one message to receive, then you should just read
until the recv call returns '', indicating EOF.

Jean-Paul
ok thanks, but I am true greenhorn, so you think that the best way is
write new script?
Could you send me code if it isnt long, becase I have no idea and
unfortunately time as well.

Petr
Petr
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.