1

I'm trying to open server socket using python

the code that I'm using wait for connection and pause the loop until the next connection achieved when its trying to execute this line>>

connection, client_address = sock.accept()

but I don't need to pause the loop

Is there any method to make the code skip this line and continue the loop if there is no connection

code :

Server

import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
# Listen for incoming connections
sock.listen(5)
while True:
 # Wait for a connection
 print >>sys.stderr, 'waiting for a connection'
 connection, client_address = sock.accept()
	
 try:
 print >>sys.stderr, 'connection from', client_address
 # Receive the data in small chunks and retransmit it
 while True:
 data = connection.recv(16)
 print >>sys.stderr, 'received "%s"' % data
 if data:
 print >>sys.stderr, 'sending data back to the client'
 connection.sendall(data)
 else:
 print >>sys.stderr, 'no more data from', client_address
 break
 
 finally:
 # Clean up the connection
 connection.close()	

Client

import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)
try:
 
 # Send data
 message = 'This is the message. It will be repeated.'
 print >>sys.stderr, 'sending "%s"' % message
 sock.sendall(message)
 # Look for the response
 amount_received = 0
 amount_expected = len(message)
 
 while amount_received < amount_expected:
 data = sock.recv(32)
 amount_received += len(data)
 print >>sys.stderr, 'received "%s"' % data
finally:
 print >>sys.stderr, 'closing socket'
 sock.close()
	

asked Apr 8, 2015 at 15:21

2 Answers 2

1

What you want is either:

  • Accept the socket in a different thread
  • Use an asynchronous socket API

I don't know Python very well, but I'm pretty sure it doesn't support threads for this, so async IO it is. A quick search gives me https://docs.python.org/2/library/asyncore.html

answered Apr 8, 2015 at 15:25
Sign up to request clarification or add additional context in comments.

1 Comment

I tried to connect to this code but it refuse client connection
0

You can set the socket to be non blocking, this will then return an error if there is no connection ready rather than blocking indefinetly

sock.setblocking(False)
try:
 connection, client_address = sock.accept()
except:
 print 'No Connection'
answered Apr 8, 2015 at 15:35

1 Comment

I will use this code to control ROV and there is PID controller .. I can't pause the PID code when I'm trying to receive joystick data I think i'll try to use "define PID" and recall it twice once below "try" and once again below "except"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.