Thursday, 30 August 2012

TCP/IP Sockets

The following pairs of scripts show how to set up a TCP connection in Python.

The scripts are to be run in two terminal windows on the same machine.
  1. Run the server script and the server will start to listen call from port 5000.
  2. Run the client script in the same machine, the client will start to connect with the server through port 5000.
  3. In the client, enter any message and send it to the server.
  4. See what happens in the server.
  5. In the client, enter c to close the client connection without closing the server.
  6. While the server is still running, you may start a new client in another terminal window to call the server.
  7. In the client, enter x to close both the client and the server.

Source Code


Server Side
# TCP server example
import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(1)

mEndServer = False
while not mEndServer:
    print "Waiting for call..."
    client_socket, address = server_socket.accept()
    print "I got a connection from ", address

    # handle a client connection
    while True:
        data = client_socket.recv(512)
        if data=='x':
            # ending server
            print "Server is closed\n"
            mEndServer = True
            break
        elif data=='c':
            # close client only without
            # ending server
            print "Client is closed\n"
            client_socket.close()
            break
        elif data <> False:
            print "RECEIVED from client:" , data
            #print "Client is waiting for me..."
            #mres = raw_input("Press Enter to send back...")
            mMsg = "Your data is %s\n" % data
            client_socket.send(mMsg)

Client Side
# TCP client example
import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5000))

while True:
    data = raw_input("Message sent to server:")
    client_socket.send(data)
    if data == 'x':
        # tell server to close
        break
    if data == 'c':
        # close client only
        break
    if data == '':
        # close client only
        break
    print "Waiting for server to feed back..."
    data = client_socket.recv(512)
    print "RECEIVED from server:" , data

client_socket.close()            

If you want to connect to the server with a different machine on the network, you should change localhost in the client script to the IP of the server (say 192.168.0.80).

Output


Server Side
Waiting for call...
I got a connection from  ('127.0.0.1', 55171)
RECEIVED from client: Hello!
RECEIVED from client: I am client 1
RECEIVED from client: Do you see me?
RECEIVED from client: Bye
Client is closed

Waiting for call...
I got a connection from  ('127.0.0.1', 55172)
RECEIVED from client: Here is client 1 again.
RECEIVED from client: Server, pls close youself.
Server is closed

Client Side
Message sent to server:Hello!
Waiting for server to feed back...
RECEIVED from server: Your data is Hello!

Message sent to server:I am client 1
Waiting for server to feed back...
RECEIVED from server: Your data is I am client 1

Message sent to server:Do you see me?
Waiting for server to feed back...
RECEIVED from server: Your data is Do you see me?

Message sent to server:Bye   
Waiting for server to feed back...
RECEIVED from server: Your data is Bye

Message sent to server:c

#

Message sent to server:Here is client 1 again.
Waiting for server to feed back...
RECEIVED from server: Your data is Here is client 1 again.

Message sent to server:Server, pls close youself.
Waiting for server to feed back...
RECEIVED from server: Your data is Server, pls close youself.

Message sent to server:x

Reference


http://www.thefengs.com/wuchang/work/courses/cs594/PyNet.pdf

No comments:

Post a Comment