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

Trying to Send Repeated Messages to a Server Using sockets

Hey Everyone, I am trying to send repeated messages from a "Node" to a
"Server". It works the first time I send the from the Node to Server,
but after that it either errors, or does not do anything.

I would love some help, here is the 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',))

Oct 16 '07 #1
1 2706
danfolkes a écrit :
Hey Everyone, I am trying to send repeated messages from a "Node" to a
"Server". It works the first time I send the from the Node to Server,
but after that it either errors, or does not do anything.

I would love some help, here is the code:
Posting the trackbacks may have help too.
import socket
import thread
import time
def Node(nodeAddress):
pep08:
- function names should be all_lower. MixedCase is for class names.
- variable names should be all_lower
'''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
These two last lines are useless here. The second would be meaningful
outside the function's body. The first one looks like a constant, but is
only an alias for the nodeAddress param.
sN = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
This discards the first socket instanciated above...
sN.connect((HOST, PORT))
sN.send('Hello, world')
data = sN.recv(1024)
sN.close()
print 'Received', repr(data)

def Server(address):
pep08 again.
''' 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
Idem. But this time, you just don't use the 'address' param...
sP = None
sL = None
sR = None
sS = None
sStor = 0
sTotalStorage = 0
sCT = (sP,)
None of those very badly named variables are used in the code.
'''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()
Hem...
print 'Connected by', addr
while 1:
print message
time.sleep(1)
data = conn.recv(1024)
#if not data: break
conn.send(data)
Your server loops forever on the first connection it got...

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',))
Here's a somewhat corrected version. I'm not sure it's an example to
follow wrt/ threaded server implementation (it's the first time I write
one myself), but at least it works.

import socket
import thread
import time

HOST = ''
PORT = 50007

def node(host, num):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, PORT))
print "node %s: sending" % num
s.send("node '%s' says : Hello, world" % num)
time.sleep(10) # play with this to see how the server reacts...
print "node %s: receiving" % num
data = s.recv(1024)
s.close()
print "node %s: received '%s'" % (num, data)
print "node %s: done" % num
def server(address):
thread_ids = []

def handle(conn, addr):
tid = thread.get_ident()
print "server (handle %s): handling connection for %s" % (tid,
str(addr))

print "server (handle %s): receiving" % tid
data = conn.recv(1024)
while data:
print "server (handle %s): got '%s'" % (tid, data)
print "server (handle %s): sending '%s'" % (tid, data)
conn.send(data)
time.sleep(2)
print "server (handle %s): receiving" % tid
data = conn.recv(1024)

print "server (handle %s): no more data for %s, closing" %
(tid, str(addr))
conn.close()
thread_ids.remove(tid)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)

while 1:
print "server: active handler threads : %s" % thread_ids
nb_handlers = len(thread_ids)
while nb_handlers 10:
print "server: too much connections, waiting for current
handlers to terminate"
time.sleep(5)
nb_handlers = len(thread_ids)

print "server: accepting connections - actually %s handlers" %
nb_handlers
conn, addr = s.accept()
print "server: connected by", addr
print "server: starting a new handler thread for connection %s"
% nb_handlers
thread_ids.append(thread.start_new(handle, (conn, addr)))
time.sleep(1)

def main():
print "starting server..."
thread.start_new(server,('',))

num = 1
while 1:
time.sleep(1)
print "new node %s..." % num
thread.start_new(node,('127.0.0.1',num))
num += 1

if __name__ == '__main__':
main()
Oct 16 '07 #2

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

Similar topics

1
by: Steve | last post by:
Please take a look at the simple code segment below and advise me what is wrong. According to the help and examples I've seen it should work unless I misunderstand something. The problem is...
1
by: Kueishiong Tu | last post by:
How do I send and receive internet messages using socket in the .net VC++ environment? Coding sample will be helpful.
5
by: zorhel | last post by:
Hi. My clients will be IE, Mozilla and Opera in a Windows and *nix OS. So, my web app need to, from a server, send messages to a specific client (browser), send messages for all clients,...
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
3
by: growse | last post by:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is to send a simple string from B to A. A is always running. I've overridden the WndProc method to give me messages that are...
2
by: SammyBar | last post by:
Hi all, I'm trying to send a message from ASP.NET to another PC by using MSMQ. I created my ASP.NET project by using Visual Studio 2005 but I initially set the project to be located on the File...
14
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
1
by: kirany82 | last post by:
I want to write a server such that it receives UDP and TCP messages on same port continuously. I have created 2 sockets tcpsock, udpsock. I am binding them to same port by using (setsockopt(sock,...
7
by: John Straumann | last post by:
Hi all: I am trying to write some code to send an email from an Assembly file, and found some examples on MSDN. However when I run the program (I am using a console app for testing), the email...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
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...

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.