473,395 Members | 1,678 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,395 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 2218
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.