Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
crzqj opened this issue Apr 24, 2024 · 1 comment
Open

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

crzqj opened this issue Apr 24, 2024 · 1 comment

Comments

@crzqj
Copy link

crzqj commented Apr 24, 2024

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

@sunshinekoo
Copy link
Contributor

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

你的测试服务代码有问题,连上后就断开了。

import socket

def tcp_server(host, port):
    # 创建一个TCP套接字
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    # 开始监听传入的连接
    server_socket.listen()

    print ("Server listening on %s" % host,port)

    while True:
        # 接受连接
        client_socket, client_address = server_socket.accept()
        print ("Connection from ", client_address)

        while True:
            # 接收客户端发送的数据
            data = client_socket.recv(1024)
            if not data:
                break
            print ("Received: ", data.decode())

            # 回复相同的数据
            client_socket.sendall(data)
            print ("Sent: ", data.decode())

        client_socket.close()
        print ("Connection with %s closed." % client_address)

if __name__ == "__main__":
    tcp_server("127.0.0.1", 12345)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants