First, we will make a server-side program that offers a connection to the client and sends a message to the client. Run server1.py
:
import socket host = "192.168.0.1" #Server address port = 12345 #Port of Server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host,port)) #bind server s.listen(2) conn, addr = s.accept() print addr, "Now Connected" conn.send("Thank you for connecting") conn.close()
The preceding code is very simple; it is minimal code on the server side.
First, import the socket module and define the host and port number: 192.168.0.1
is the server's IP address. Socket.AF_INET
defines the IPv4 protocol's family. Socket.SOCK_STREAM
defines the TCP connection. The s.bind((host,port))
statement takes only one argument. It binds the socket to the host and port number. The s.listen(2)
statement listens to the connection and waits for the client. The conn, addr = s.accept()
statement returns two values: conn
and addr
. The conn
socket is...