6.1 TCP Client Server
1. TCP Client
In Cisco Packet Tracer, when using the scripting environment (e.g. with the TCPClient
in the Packet Tracer IoT device or PC scripting panel), there’s no direct socket
object as in Python’s standard socket
library. However, you can get the socket pair (i.e., client IP:port and server IP:port) using the available client
object methods:
from tcp import *from time import *
serverIP = "192.168.1.2"serverPort = 1234
client = TCPClient()
def onTCPConnectionChange(type): print("connection to " + client.remoteIP() + " changed to state " + str(type))
def onTCPReceive(data): print("received from " + client.remoteIP() + " with data: " + data)
def main(): client.onConnectionChange(onTCPConnectionChange) client.onReceive(onTCPReceive)
print("Connecting to", serverIP, "on port", serverPort)
if client.connect(serverIP, serverPort): print("Connected.") print("Socket pair: {}:{} -> {}:{}".format( client.localIP(), client.localPort(), client.remoteIP(), client.remotePort() )) else: print("Connection failed.") return
count = 0 while True: count += 1 data = "hello " + str(count) client.send(data)
sleep(5)
if __name__ == "__main__": main()
Explanation:
client.localIP()
andclient.localPort()
give the local (client-side) IP and port.client.remoteIP()
andclient.remotePort()
give the server’s IP and port.
This gives you the full socket pair like:
Socket pair: 0.0.0.0:1027 -> 192.168.1.2:1234
2. TCP Server
To print the socket pair (i.e., server IP:port ↔ client IP:port) on the server side in Cisco Packet Tracer, you can update the onTCPNewClient
function to include this information using the client.localIP()
, client.localPort()
, client.remoteIP()
, and client.remotePort()
methods.
Here’s the modified server code:
from tcp import *from time import *
port = 1234server = TCPServer()
def onTCPNewClient(client): print("New connection:") print("Socket pair: {}:{} <-> {}:{}".format( client.localIP(), client.localPort(), client.remoteIP(), client.remotePort() ))
def onTCPConnectionChange(type): print("connection to " + client.remoteIP() + " changed to state " + str(type))
def onTCPReceive(data): print("received from " + client.remoteIP() + " with data: " + data) # send back same data client.send(data)
client.onConnectionChange(onTCPConnectionChange) client.onReceive(onTCPReceive)
def main(): server.onNewClient(onTCPNewClient) print(server.listen(port))
# don't let it finish while True: sleep(3600)
if __name__ == "__main__": main()
Output Example When a Client Connects:
New connection:Socket pair: 192.168.1.2:1234 <-> 192.168.1.1:1027
This shows the server IP/port and the client IP/port, helping you verify the TCP connection endpoints.
3. Callback Functions
In Cisco Packet Tracer’s scripting environment, especially when working with IoT devices or PC scripting, these functions:
onTCPNewClient
onTCPConnectionChange
onTCPReceive
are callback functions (event handlers) that you define to respond to specific TCP events.
🔁 1. onTCPNewClient(client)
- Purpose: Called when a new client connects to the TCP server.
- Where it’s used: Passed to
server.onNewClient(...)
. - Parameter:
client
— aTCPClientConnection
object representing the new client connection.
server.onNewClient(onTCPNewClient)
🔄 2. onTCPConnectionChange(type)
- Purpose: Called when the connection state changes (e.g., connected, disconnected).
- Where it’s used: Registered per client using
client.onConnectionChange(...)
. - Parameter:
type
— an integer (usually 1 = connected, 0 = disconnected).
client.onConnectionChange(onTCPConnectionChange)
📩 3. onTCPReceive(data)
- Purpose: Called when data is received from a client or server.
- Where it’s used: Registered per client using
client.onReceive(...)
. - Parameter:
data
— a string containing the received data.
client.onReceive(onTCPReceive)
How it works (short version):
You are assigning these handlers to TCP events so that your script reacts to activity like:
- a new client connecting
- a client disconnecting
- or receiving data over the connection
This model mimics event-driven programming often used in real-world servers.