美文网首页
TCP socket编程

TCP socket编程

作者: tmax | 来源:发表于2018-10-25 20:22 被阅读0次

    server

    from socket import *
    serverName = 'localhost'
    serverPort = 2333
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind((serverName, serverPort))
    serverSocket.listen(1)
    while True:
        connectionSocket, addr = serverSocket.accept() # 新建TCP连接,完成三次握手
        message = connectionSocket.recv(2048)
        connectionSocket.send( message.decode().upper().encode() )
        connectionSocket.close()
    
    

    client

    from socket import *
    serverName = 'localhost'
    serverPort = 2333
    clientSocket = socket(AF_INET, SOCK_STREAM)
    clientSocket.connect((serverName, serverPort))
    message = raw_input('input lowercase sentences:') #py2.x
    clientSocket.send(message.encode())
    modifiedMessage = clientSocket.recv(2048)
    clientSocket.close()
    print modifiedMessage
    

    相关文章

      网友评论

          本文标题:TCP socket编程

          本文链接:https://www.haomeiwen.com/subject/vlnctqtx.html