簡體   English   中英

我如何獲取套接字以繼續偵聽客戶端並不斷從客戶端打印信息

[英]How do i get the socket to continue listening to the client and continuosly print information from the client

服務器端 (服務器打印從客戶端發送的第一行信息,然后它只是保持打開狀態,似乎不繼續監聽,而是保持打開狀態。是否有辦法讓服務器更多地監聽客戶端並打印?)

import time

import socket

import signal

from datetime import datetime
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 8089))
serversocket.listen(1024) # become a server socket, maximum 5 connectionn

def clientsocketentry():
    while True:
      connection, addr = serversocket.accept()
      buf = connection.recv(64)

      if not buf:
          break

      elif buf == 'killsrv':
          connection.close()
          sys.exit()

      else:
          print (buf)
          buf = buf.decode("utf-8")
          buf = buf.split(',')
          serverLong = buf[0]
          print('Longitude:' + '' + serverLong)
          serverLat = buf[1]
          print('Lattitude:' + '' + serverLat)
          serverAlt = buf[2]
          print('Altitude:' + '' + serverAlt)
          serverTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
          print('Time of Entry:' + ' ' + serverTime)
          connection.close()



clientsocketentry()

客戶端 (我只能發送信息字符串之一,然后服務器保持打開狀態,而不會從客戶端獲取更多信息)

import socket

import time

clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

clientsocket.connect(('localhost', 8089))

a = '39.163100,-76.899428,0'
clientsocket.send(a.encode('utf-8'))
time.sleep(5)

a = '4.2,2.2415,0'
clientsocket.send(a.encode('utf-8'))
time.sleep(5)

a = '43454,354354,35435'
clientsocket.send(a.encode('utf-8'))
time.sleep(5)

a = '435742.,35.452,52434'
clientsocket.send(a.encode('utf-8'))
time.sleep(5)


clientsocket.close()

您正在關閉ClientSocketEntry函數中打印邏輯末尾的套接字。

serverTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print('Time of Entry:' + ' ' + serverTime)
connection.close()

而不是關閉連接,只有在用戶發送killsrv時才關閉它,因為每次關閉套接字上的連接時都在說您期望另一個客戶端連接到服務器。 因此,也許在進入while語句之前先接受連接,然后再將其傳遞到while語句中,因為當前構造結構的方式是期望來自不同客戶端的多個連接。

如果您一次接受一個連接(那么不需要1024個積壓...),您可以簡單地嵌套2個循環:外部的一個等待新連接,內部的一個處理來自唯一一個已建立連接的輸入。 如果需要處理多個連接,則必須使用select或線程。

這是一個單一連接的示例:

def clientsocketentry():
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.bind(('localhost', 8089))
    serversocket.listen(5) # become a server socket, maximum 5 connectionn
    cont = True
    while cont:
      connection, addr = serversocket.accept()
      while True:
          buf = connection.recv(64)

          if len(buf) == 0:     # end of connection
              connection.close()
              break

          elif buf == b'killsrv':     # request for closing server (beware of the byte string)
              connection.close()
              serversocket.close()
              cont = False
              break

          else:
              print (buf)
              buf = buf.decode("utf-8")
              buf = buf.split(',')
              serverLong = buf[0]
              print('Longitude:' + '' + serverLong)
              serverLat = buf[1]
              print('Lattitude:' + '' + serverLat)
              serverAlt = buf[2]
              print('Altitude:' + '' + serverAlt)
              serverTime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
              print('Time of Entry:' + ' ' + serverTime)
              # connection.close()    # wait for client to close

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM