Download presentation
Presentation is loading. Please wait.
1
TCP Socket Programming
Presenter :Nicole
2
Outline What is a Socket? Stream Sockets Server code Client code
Solution Demo Reference
3
What is a Socket? Socket Types
Sockets allow communication between two different processes on the same or different machines. To be more precise, it's a way to talk to other computers using standard Unix file descriptors. Socket Types Stream Sockets Datagram Sockets Raw Sockets Sequenced Packet Sockets
4
Stream Sockets Server Client Socket() Bind() Listen() request Accept()
Bind() Listen() Connect() Accept() Recv() Send() Close() Server Client request response Data transmission Data transmission
5
Server python code import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host = “your IP address" port = your port sk.bind((host,port)) sk.listen(5) print("Server start....") while True: connection,addr = sk.accept() print("Got connection from",addr) msg = connection.recv(2048) print(msg.decode(encoding = "utf-8")) connection.send("ok!".encode(encoding = 'utf-8')) connection.close()
6
Client python code import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host = “your IP address" port = your port print("connecting to server....") sk.connect((host, port)) str = input("What do you want to do?") sk.send(str.encode(encoding = 'utf-8')) buffer = sk.recv(2048) print(buffer.decode(encoding = 'utf-8')) sk.close
7
Solution OSError: [Errno 113] No route to host-------因為server端防火牆擋住
(solution1) Service iptables stop (solution2) Iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport port -j ACCEPT OSError: [Errno 98] Address already in use # ps –x # kill pID
8
Demo server client
9
Reference What is socket
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.