服务器不会收集和发送消息

时间:2016-11-08 16:17:58

标签: python

我正在尝试使用线程构建服务器和客户端,以便服务器有两个线程recv和send以及客户端。但我的问题是我不知道为什么我的服务器会输出send和recv错误。

我的服务器

from threading import Thread
import socket

global recv_msg
global connect


def socket_connect():
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(("0.0.0.0", 8350))
        print "Server start"
        sock.listen(1)
        connect, addr = sock.accept()
        print "Client Ip: " + str(addr[0])
    except socket.error:
        print "Server error"


def send_to_client():
    try:
        connect.send("Server:" + recv_msg + "\n")
    except:
        print "Error send"


def recv_from_client():
    try:
        recv_msg = connect.recv(1024)
    except:
        print "Error Recv"


def main():
    while True:
        recv_thread = Thread(target=recv_from_client, )
        send_thread = Thread(target=send_to_client,)

        recv_thread.start()
        send_thread.start()

        recv_thread.join()
        send_thread.join()


if __name__ == "__main__":
    main()

我的客户

import socket
from threading import Thread

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    client_socket.connect(("89.139.105.181", 8350))
    print "Client connection success"
except socket.error:
    print "Client error"


def send_to_server():
    msg = raw_input("Client:")
    client_socket.send(msg)


def recv_from_server():
    response_server = client_socket.recv(1024)
    print response_server


def main():
    while True:
        send_thread = Thread(target=send_to_server,)
        recv_thread = Thread(target=recv_from_server,)

        send_thread.start()
        recv_thread.start()

        send_thread.join()
        recv_thread.join()

if __name__ == "__main__":
    main()

错误可能来自哪里?

0 个答案:

没有答案