码迷,mamicode.com
首页 > 其他好文 > 详细

2019.10.22 用TCP实现服务端并发接收

时间:2019-10-22 20:38:46      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:soc   sockets   tip   try   tar   def   art   process   from   

client



import socket

client = socket.socket()
client.connect(
    ('127.0.0.1',8888)
)


while 1:
    msg = input('input>>>')
    if  msg == 'q':
        break
    client.send(msg.encode('utf-8'))
    data = client.recv(1024).decode('utf-8')
    print(data)

client.close()

server线程解决

import socket
from  multiprocessing  import Process,Queue
import threading
import socketserver

# q = Queue(4)

server = socket.socket()
server.bind(
    ('127.0.0.1', 8888)
)
server.listen(3)

def se():


    while 1:
        conn,addr = server.accept()
        print(addr)

        while 1:
            try:
                data = conn.recv(1024).decode('utf-8')
                print(data)
                conn.send(data.encode('utf-8'))

            except Exception as e:
                print(e)
                break

        conn.close()



if __name__ == '__main__':

    p1 = threading.Thread(target=se)
    p2 = threading.Thread(target=se)
    p3 = threading.Thread(target=se)
    p1.start()
    p2.start()
    p3.start()

server进程解决

from  multiprocessing  import Process
import socket




def se(ser):


    while 1:

        # q.put(sb)
        conn,addr = ser.accept()
        print(addr)

        while 1:
            try:
                data = conn.recv(1024).decode('utf-8')
                print(data)
                conn.send(data.encode('utf-8'))

            except Exception as e:
                print(e)
                break

        conn.close()



if __name__ == '__main__':

    server = socket.socket()
    server.bind(
        ('127.0.0.1', 8888)
    )
    server.listen(3)
    p1 = Process(target=se,args = (server,))
    p2 = Process(target=se,args = (server,))
    p3 = Process(target=se,args = (server,))


    p1.start()
    p2.start()
    p3.start()

2019.10.22 用TCP实现服务端并发接收

标签:soc   sockets   tip   try   tar   def   art   process   from   

原文地址:https://www.cnblogs.com/agsol/p/11721903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!