473,326 Members | 2,114 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,326 software developers and data experts.

Re: Socket and cycle problem

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
1 2213
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: drs | last post by:
Hi, I have a program which opens a socket server in a thread. I need for the server to listen on the socket for a certain amount of time (say, ten seconds or so) and then close it, and am...
0
by: Bernhard Schmidt | last post by:
Hello, sorry for bothering, I'm not a programmer and I don't do much python, I'm more a networking guy trying to get his favourite linux distribution to update through the shiny new protocol...
3
by: Tom Opgenorth | last post by:
I'm experiencing a problem with sockets, and I'm really hoping someone can help me, please and thank you. I've written a TCP Server, which listens on a port for an incoming connection. When the...
6
by: Abubakar | last post by:
Hi, lets say I have a connected SOCKET s. At some point in time, I want to know if the "s" is still valid, that it is still connected. Is there any API that I can give me this information? And...
15
by: nephish | last post by:
hey there, i have a script that waits for message packets from a data server over a socket. it goes a little like this: while 1: x+=1 databack = sockobj.recv(158) if databack:
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
4
by: O.B. | last post by:
I have a socket configured as TCP and running as a listener. When I close socket, it doesn't always free up the port immediately. Even when no connections have been made to it. So when I open...
0
by: petr.poupa | last post by:
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 =...
2
by: Jean-Paul Calderone | last post by:
On Mon, 12 May 2008 11:16:08 -0700 (PDT), petr.poupa@gmail.com wrote: I'm not sure if you need to write a server or a client. In your original code, you had a client which repeatedly established...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.