Skip to content

Commit 4cf54a2

Browse files
authored
Merge pull request #87 from xjinGao/main
Add TCP server support。
2 parents 2ac0319 + 730d892 commit 4cf54a2

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

en-us/api/pythonStdlib.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,8 @@ type - socket type
11991199
proto - protocol number
12001200

12011201
* usocket.IPPROTO_TCP
1202-
12031202
* usocket.IPPROTO_UDP
1203+
* usocket.IPPROTO_TCP_SER : socket for TCP Server
12041204

12051205
Others
12061206

@@ -1216,6 +1216,8 @@ import usocket
12161216
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
12171217
# Creating UDP-based Datagram Sockets
12181218
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
1219+
# Creating TCP-based server sockets
1220+
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM, usocket.IPPROTO_TCP_SER)
12191221
```
12201222

12211223
##### Translate the host/port argument into a sequence of 5-tuples
@@ -1230,6 +1232,25 @@ Translate the host/port argument into a sequence of 5-tuples. The resulting list
12301232

12311233
#### Class Socket Methods
12321234

1235+
##### Bind specified address
1236+
1237+
> **socket.bind(address)**
1238+
1239+
Bind the socket to address. The socket must not already be bound. (The format of address depends on the address family)
1240+
1241+
* `address` : A tuple or list containing addresses and port numbers
1242+
1243+
Example:
1244+
1245+
```
1246+
# Bind the datacall IP to the server address
1247+
socket.bind(("",80))
1248+
# Bind a custom IP to the server address
1249+
socket.bind(("192.168.0.1",80))
1250+
```
1251+
1252+
#####
1253+
12331254
##### Enable a server to accept connections
12341255

12351256

@@ -1243,7 +1264,7 @@ Enable a server to accept connections. The maximum number of connections can be
12431264

12441265
> **socket.accept()**
12451266
1246-
Accept a connection and return a tuple. The socket must be bound to an address and listening for connections. The return value is a pair `(conn, address)`
1267+
Accept a connection and return a tuple. The socket must be bound to an address&port and listening for connections. The return value is a pair `(conn, address, port)`
12471268

12481269
* `conn` : new socket object usable to send and receive data on the connection
12491270

@@ -1399,6 +1420,8 @@ Get socket status. The status values are described as follows:
13991420

14001421
Note:
14011422

1423+
The BG95 platform does not support this API.
1424+
14021425
If the user calls the ` socket.getsocketsta () ` after calling the ` socket.close () ` method, it returns-1 because the object resources and so on created at this point have been freed.
14031426

14041427
**Socket Communication Example**

zh-cn/api/pythonStdlib.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,8 @@ proto - 协议号
12011201

12021202
* usocket.IPPROTO_UDP
12031203

1204+
* usocket.IPPROTO_TCP_SER :对应TCP socket 服务端套接字
1205+
12041206
其他
12051207

12061208
* usocket.SOL_SOCKET - 套接字选项级别,
@@ -1215,6 +1217,8 @@ import usocket
12151217
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
12161218
# 创建基于UDP的数据报套接字
12171219
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
1220+
# 创建基于TCP的服务端套接字
1221+
socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM, usocket.IPPROTO_TCP_SER)
12181222
```
12191223

12201224
##### 将主机域名(host)和端口(port)转换为用于创建套接字的5元组序列
@@ -1229,6 +1233,26 @@ socket = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
12291233

12301234
**socket类的方法**
12311235

1236+
##### 服务端绑定指定地址address
1237+
1238+
1239+
> **socket.bind(address)**
1240+
1241+
服务端绑定指定地址address的服务器。
1242+
1243+
* `address` :包含地址和端口号的元组或列表
1244+
1245+
注意:该方法在服务端套接字使用时,绑定address时会将address设为客户端可连接address。其他客户端是否可连接,需确认运营商网络是否支持。
1246+
1247+
示例:
1248+
1249+
```
1250+
#绑定拨号IP为服务器地址,端口自定义
1251+
socket.bind(("",80))
1252+
#绑定自定义address
1253+
socket.bind(("192.168.0.1",80))
1254+
```
1255+
12321256
##### 允许服务端接受连接
12331257

12341258

@@ -1242,12 +1266,14 @@ socket = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
12421266

12431267
> **socket.accept()**
12441268
1245-
接受连接请求,返回元组,包含新的套接字和客户端地址,形式为:`(conn, address)`
1269+
接受连接请求,返回元组,包含新的套接字和客户端地址以及客户端端口,形式为:`(conn, address, port)`
12461270

12471271
* `conn` :新的套接字对象,可以用来发送和接收数据
12481272

12491273
* `address` :连接到服务器的客户端地址
12501274

1275+
* `port` :连接到服务器的客户端端口
1276+
12511277
##### 连接到指定地址address的服务器
12521278

12531279
> **socket.connect(address)**
@@ -1412,6 +1438,7 @@ socket.setsockopt(usocket.SOL_SOCKET, usocket.TCP_KEEPALIVE, 1)
14121438

14131439
注意:
14141440

1441+
BG95平台不支持该API。
14151442
如果用户调用了 `socket.close()` 方法之后,再调用 `socket.getsocketsta()` 会返回-1,因为此时创建的对象资源等都已经被释放。
14161443

14171444

0 commit comments

Comments
 (0)