Assuming WebSocket communication, the basics of network programming are smooth. We plan to develop it based on the following.
** Stream type ** I want to make connection-oriented communication that maintains the connection with TCP.
example
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
** Datagram type ** I want to make connectionless communication that maintains the connection with UDP.
example
socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
With only IP, only one session of communication can be performed. Can multiple communication processes be performed using the IP + port? (Investigation required) Port number can be specified from 1 to 65535
Port num | Des |
---|---|
1~1023 | Known port number |
1024~49151 | The left is also registered |
49152~65535 | You can use it freely |
[Wiki port number list](https://ja.wikipedia.org/wiki/TCP%E3%82%84UDP%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B % E3% 83% 9D% E3% 83% BC% E3% 83% 88% E7% 95% AA% E5% 8F% B7% E3% 81% AE% E4% B8% 80% E8% A6% A7)
1.socket Example) I want to use TCP communication
example
s.socket(socket.AF_INET, socket.SOCK_STREAM)
2.bind Fix it to the created socket with the ip and port to be used.
example
s.bind((HOST,PORT))
3.listen The process of enabling the created socket and accepting server connections. Specify the number of connectable items. More than the specified number should be rejected ...
example
s.listen(1)
4.accept Make it connectable.
example
conn, addr = s.accept()
5.send or recv Actual reception processing One reception size can be specified in Byte.
example
recv_mess = s.recv()
6.close Close
example
s.close()
1.socket Example) I want to use TCP communication
example
s.socket(socket.AF_INET, socket.SOCK_STREAM)
2.connect Connect to the target ip and port
example
s.connect((host,port))
3.send or recv
example
s.sendall(msg)
4.closesocket Close
example
s.close(msg)
Recommended Posts