Why does the message send only once?

D

danfolkes

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>
 
B

Bjoern Schliessmann

danfolkes said:
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
 
M

Marc 'BlackJack' Rintsch

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
 
B

Bruno Desthuilliers

danfolkes a écrit :

<OT>
You already posted the same question here one hour and a half ago.
Please avoid.
</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top