473,396 Members | 1,967 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,396 software developers and data experts.

Why does the message send only once?

Why does the message send only once?

The node sends once, then fails after that.

<code>
import socket
import thread
import time

def Node(nodeAddress):

'''connect to node and get its file load'''

sN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Echo client program
HOST = nodeAddress # The remote host
PORT = 50007 # The same port as used by the server
sN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sN.connect((HOST, PORT))
sN.send('Hello, world')
data = sN.recv(1024)
sN.close()
print 'Received', repr(data)

def Server(address):

''' starts a socket server and decides what to do with incomming
stuff'''
message = "hello from server"
HOST = '' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port
sP = None
sL = None
sR = None
sS = None
sStor = 0
sTotalStorage = 0
sCT = (sP,)
'''look in server.conf for connect_to_server value'''

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
print message
time.sleep(1)
data = conn.recv(1024)
#if not data: break
conn.send(data)
conn.close()

thread.start_new(Server,('',))
thread.start_new(Node,('127.0.0.1',))
while 1:
time.sleep(5)
thread.start_new(Node,('127.0.0.1',))
</code>

Oct 16 '07 #1
3 1723
danfolkes wrote:
Why does the message send only once?
Because your server is designed to only accept one connection, wait
forever except there is an exception, and quit thereafter.
def Server(address):
[...]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1) # ----- set up listening
conn, addr = s.accept() # ----- accept one connection
print 'Connected by', addr
while 1:
print message
time.sleep(1)
data = conn.recv(1024) # ----- receive stuff
#if not data: break
conn.send(data) # ----- send stuff
conn.close() # ----- close connection
# ----- quit
Personally, I like the Twisted framework more than low-level socket
interfaces. Consider giving it a try -- your code will be much
easier to read and maintain. You don't have to waste time
implementing the server or client basics but can fully concentrate
on your protocol(s).

Regards,
Björn

--
BOFH excuse #168:

le0: no carrier: transceiver cable problem?

Oct 16 '07 #2
On Tue, 16 Oct 2007 15:55:31 +0000, danfolkes wrote:
Why does the message send only once?

The node sends once, then fails after that.
Because the `Server` just seems to handle the first connection and is done
then. Insert some ``print``\s to see what's going on.

Ciao,
Marc 'BlackJack' Rintsch
Oct 16 '07 #3
danfolkes a écrit :

<OT>
You already posted the same question here one hour and a half ago.
Please avoid.
</OT>
Oct 16 '07 #4

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

Similar topics

8
by: jasonbrown1999 | last post by:
Someone told me the following script could be used to run harmful commands on the server, by passing commands into the script. What the script does is encode an affiliate URL, create two frames,...
2
by: Jagdip Singh Ajimal | last post by:
I have setup an email notifications system, that basically takes each row from a table and sents out an email according to the data in that row. The emails get sent, with the subject being filled...
2
by: Chris | last post by:
Im hoping someone can help me understand why this works? Requirement: Navigate DOM through javascript to perform DHTML actions. Issue: GIANT html document, up to 4MB in size, thousands and...
1
by: Kitchen Bin | last post by:
Hi. I am trying to use Sockets to do multiple Send and Receives via HTTP (not simultaneously). A first pair of Send/Receives works fine and sure enough I receive HTML back, but the next...
4
by: Winston Nimchan | last post by:
Hi: I'm currently developing a socket application and would like to precede the data being sent with a 4 byte message length header (bin4). Can anyone help Regards Winston
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
7
by: Guy Davidson | last post by:
Hi Folks, I'm having some issues with an small socket based server I'm writing, and I was hoping I could get some help. My code (attached below) us supposed to read an HTTP Post message...
2
chunk1978
by: chunk1978 | last post by:
i wrote a custom PHP script last year and everything worked perfectly... recently my webserver upgraded to PHP 5 and now my script doesn't work properly. what the script does is: users fill out...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.