【问题标题】:How to end a server socket listen如何结束服务器套接字侦听
【发布时间】:2017-07-03 17:59:29
【问题描述】:

所以我要做的是创建一个多线程服务器,为连接到它的每个客户端创建线程,并回复从客户端发送的字符串。

它有点工作,但我的服务器实际上并没有正确结束。我的 KerboardInterrupt 捕获似乎在 Windows 命令提示符下不起作用,唯一让我退出进程的方法是 ctrl + pause/break。谁能帮我想办法让服务器优雅地结束?

服务器代码:

import socket
import threading
import time
import datetime
import sys

def getTime():
    ts = time.time()
    timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-      %d_%H:%M:%S')
    return timeStamp

def ThreadFunction(clientsocket, clientaddr):
    global ReceivedData
    global SentData
    while True:

        #Receive data from client
        data = clientsocket.recv(bufferSize)
        #Get client IP and port
        clientIP, clientSocket = clientsocket.getpeername()

        #Add to total amount of data transfered
        ReceiveDataSize = len(data)
        ReceivedData += ReceiveDataSize

        #LOg the received data
        text_file.write(str(getTime()) + "__ Size of data received (" +     clientIP + ":" + str(clientSocket) + ") = " + str(ReceiveDataSize) + '\n')

        #Send data
        clientsocket.send(data)
        SentDataSize = len(data)
        SentData += SentDataSize

        #Log the sent data
        text_file.write(str(getTime()) + "__ Size of data sent (" + clientIP + ":" + str(clientSocket) + ") = " + str(SentDataSize) + '\n')


def Close(counter, ReceivedData, SentData):
    print ("Shutting down Server...")
    serversocket.close()
    text_file.write("\n\nTotal # of connections: " + str(counter))
    text_file.write("\nTotal data received: " + str(ReceivedData))
    text_file.write("\nTotal data sent: " + str(SentData))
    text_file.close()
    sys.exit()


if __name__ == '__main__':

    serverIP = raw_input('Enter your server IP \n')
    port = int(raw_input('What port would you like to use?\n'))

    # Maintain how many connections
    connections = []
    counter = 0

    # Maintain amount of data sent to and from server
    ReceivedData = 0
    SentData = 0
    bufferSize = 1024

    # Create and initialize the text file with the date in the filename in the logfiles directory
    text_file = open("MultiThreadedServerLog.txt", "w")
    address = (serverIP, port)
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Bind server to port
    serversocket.bind(address)

    # The listen backlog queue size
    serversocket.listen(50)
    print ("Server is listening for connections\n")

    try:
        while 1:
            # Accept client connections, increment number of connections
            clientsocket, clientaddr = serversocket.accept()
            counter += 1

            # Log client information
            print (str(clientaddr) + " : " + " Just Connected. \n Currently connected clients: " + str(counter) + "\n")
            text_file.write(str(getTime()) + " - " + str(clientaddr) + " : " + " Just Connected. \n Currently connected clients: " + str(counter) + "\n")
            clientThread = threading.Thread(target=ThreadFunction, args=(clientsocket, clientaddr))
            clientThread.start()

    except KeyboardInterrupt:
        print ("Keyboard interrupt occurred.")
        Close(counter, ReceivedData, SentData)

客户代码:

from socket import *
import threading
import time
import random
import sys
import datetime


serverIP = ""
port = 8005
message = ""
msgMultiple = 1


def getTime():
    ts = time.time()
    timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S')
    return timeStamp



def run(clientNumber):
    buffer = 1024

    global totalTime

    s = socket(AF_INET, SOCK_STREAM)
    s.connect((serverIP, port))
    threadRTT = 0

    while 1:
        for _ in range(msgMultiple):
            cData = message + "  From: Client " + str(clientNumber)

            # Start timer and send data
            start = time.time()
            s.send(cData.encode('utf-8'))
            print "Sent: " + cData

            # Stop timer when data is received
            sData = s.recv(buffer)
            end = time.time()

            # Keep track of RTT and update total time
            response_time = end - start
            threadRTT += end - start
            totalTime += response_time
            print "Received: " + cData + '\n'
            t = random.randint(0, 9)
            time.sleep(t)

        # Log information of Client
        text_file.write(
            "\nClient " + str(clientNumber) + " RTT time taken for " + str(msgMultiple) + " messages was: " + str(
                threadRTT) + " seconds.")
        threadRTT = 0
        break


if __name__ == '__main__':
    serverIP = raw_input('Enter the server IP: ')
    port = int(input('Enter the port: '))
    clients = int(input('Enter number of clients: '))
    message = raw_input('Enter a message to send: ')
    msgMultiple = int(input('Enter the number of times you would like to send the message: '))

    # Initialize Log file
    text_file = open("ClientLog.txt", "w")

    # Used to maintain list of all running threads
    threads = []
    totalTime = 0

    # Create a seperate thread for each client
    for x in range(clients):
        thread = threading.Thread(target=run, args=[x])
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()
    # Calculations for log data
    bytes = sys.getsizeof(message)
    totalRequests = clients * msgMultiple
    totalBytes = totalRequests * bytes
    averageRTT = totalTime / totalRequests
    # Output data
    print("Bytes sent in message was : " + str(bytes))
    print("Total Data sent was : " + str(totalBytes) + " Bytes.")
    print("Average RTT was : " + str(averageRTT) + " seconds.")
    print("Requests was : " + str(totalRequests))

    # Write data to log file
    text_file.write("\n\n Bytes sent in message was : " + str(bytes))
    text_file.write("\nTotal Data sent was : " + str(totalBytes) + " Bytes.")
    text_file.write("\nAverage RTT was : " + str(averageRTT) + " seconds.")
    text_file.write("\nRequests was : " + str(totalRequests))

此外,如果其他人有任何一般性的改进,他们会添加到此代码中,请告诉我。我对 python 还是很陌生,而且仍然很粗糙。

这是我从服务器获得的正常预期输入。

但是当它到达最后一个连接的客户端时,由于某种原因它开始拖拽。

最后一张图片,大部分文本文件的输入持续了很长时间。似乎有些事情没有正确结束。

【问题讨论】:

  • 哪个EOF ?套接字、数据包、文件对象?
  • KeyboardInterrupt 只会结束主线程。您还必须添加一个控制机制来结束子线程。
  • @KlausD。我尝试让我的线程成为守护进程,但这没有用。还有什么方法可以结束线程吗?

标签: python multithreading sockets network-programming


【解决方案1】:

通过添加 if 语句来检查字节

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多