Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44698

socket programming

Newsgroups comp.lang.python
Date 2013-05-03 19:13 -0700
Message-ID <4aef55bd-f550-4a3d-b11a-285b6fa9892b@googlegroups.com> (permalink)
Subject socket programming
From Pedro <pedro@ncf.ca>

Show all headers | View raw


I'm writing a simple app that uses socket programming, I'm using the following from tutorialspoint as a framework but I'm having a couple of issues implementing it. 

First - this code constantly loops around an open socket. Is there a way to use something like an interrupt so I don't have to loop constantly to monitor the socket?

Second, if any part of the program fails execution (crashes) the port often remains open on my windows machine and the only way to close it that i know of is through task manager or by rebooting the machine. Is there an easy way around this problem ? If I don't close the port the program can't open it again and crashes.

Thanks for looking



SERVER:

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection

CLIENT:
import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close                     # Close the socket when done

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

socket programming Pedro <pedro@ncf.ca> - 2013-05-03 19:13 -0700
  Re: socket programming Chris Angelico <rosuav@gmail.com> - 2013-05-04 12:23 +1000
    Re: socket programming Pedro <pedro@ncf.ca> - 2013-05-03 20:37 -0700
      Re: socket programming Chris Angelico <rosuav@gmail.com> - 2013-05-04 13:56 +1000
        Re: socket programming Pedro <pedro@ncf.ca> - 2013-05-03 21:03 -0700
  Re: socket programming Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-05-04 11:37 +0200
    Re: socket programming Chris Angelico <rosuav@gmail.com> - 2013-05-04 20:00 +1000
    Re: socket programming Pedro <pedro@ncf.ca> - 2013-05-06 08:54 -0700
      Re: socket programming Chris Angelico <rosuav@gmail.com> - 2013-05-07 02:05 +1000
      Re: socket programming Arnaud Delobelle <arnodel@gmail.com> - 2013-05-06 19:54 +0100

csiph-web