Socket Programming Basics
-----------------------------------------------[Python]------------------------------------------------
>>> import socket
>>> tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> tcpSocket.bind((“0.0.0.0”, 8000))
>>> tcpSocket.listen(2)
>>> (client, (ip, port)) = tcpSocket.accept()
---------------------------------------------[Terminal]-----------------------------------------------
root@TheLaughingMan:~# ncat 192.168.1.150 8000
-----------------------------------------------[Python]------------------------------------------------
>>> client
<socket._socketobject object at 0xb74f6e9c>
>>> ip
‘192.168.1.10’
>>> port
1031
>>> client.send(“Give me six hours to chop down a tree and I will spend the first four sharpening the axe.\n”)
89
>>> data = client.recv(2048)
---------------------------------------------[Terminal]-----------------------------------------------
Give me six hours to chop down a tree and I will spend the first four sharpening the axe.
Brilliant!
-----------------------------------------------[Python]------------------------------------------------
>>> data
‘Brilliant!\n’
-----------------------------------------------------------------------------------------------------------
---------------------------------------[Save as Server.py]----------------------------------------
import socket
tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpSocket.bind((“0.0.0.0”, 8000))
tcpSocket.listen(2)
print “Waiting for a Client ... ”
(client, (ip, sock)) = tcpSocket.accept()
print “Received connection from : ”, ip
print “Starting ECHO output .... ”
data = ‘dummy’
while len(data):
data = client.recv(2048)
print “Client sent: ”, data
client.send(data)
print “Closing connection ...”
client.close()
print “Shutting down server ...”
--------------------------------------------[Terminal1]-----------------------------------------------
root@TheLaughingMan:~# python Server.py
Waiting for a Client ...
--------------------------------------------[Terminal2]-----------------------------------------------
root@ThePuppeteer:~# ncat –nv 192.168.1.150 8000
Ncat Version 6.47 ( http://nmap.org/ncat )
Ncat: Connected to 192.168.1.150:8000.
--------------------------------------------[Terminal1]-----------------------------------------------
Received connection from : 192.168.1.10
Starting ECHO output ....
--------------------------------------------[Terminal2]-----------------------------------------------
Give me six hours to chop down a tree and I will spend the first four sharpening the axe.
Give me six hours to chop down a tree and I will spend the first four sharpening the axe.
--------------------------------------------[Terminal1]-----------------------------------------------
Client sent: Give me six hours to chop down a tree and I will spend the first four sharpening the axe.
-----------------------------------------------------------------------------------------------------------
· Create a simple Echo Server to handle 1 client
· Create a Multi-Threaded Echo Server
· Create a Multi-Process Echo Server
· Create a Non-Blocking Multiplexed Echo Server using Select()











