Skip to content

转ws收发正常,转tcp能发不能收 #2

Description

@crzqj

import socket

def tcp_server(host, port):
# 创建一个TCP套接字
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
# 绑定到指定的主机和端口
server_socket.bind((host, port))
# 开始监听传入的连接
server_socket.listen()
print(f"Server listening on {host}:{port}")

    while True:
        # 接受连接
        client_socket, client_address = server_socket.accept()
        print(f"Connection from {client_address}")
        
        with client_socket:
            while True:
                # 接收客户端发送的数据
                data = client_socket.recv(1024)
                if not data:
                    break
                print(f"Received: {data.decode()}")
                
                # 回复相同的数据
                client_socket.sendall(data)
                print("Sent: ", data.decode())
                
        print(f"Connection with {client_address} closed")

if name == "main":
HOST = '127.0.0.1' # 服务器主机
PORT = 12345 # 服务器端口
tcp_server(HOST, PORT)

使用上述简单的tcp服务器进行测试,连接正常,发送正常,但是收不到数据。
测试工具:http://wstool.js.org/
测试地址:ws://10.15.101.99:1443?token=127.0.0.1:12345

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions