2017-05-17 312 views
0

서버 기반 클라이언트에 대한 클라이언트를 만들고 서버와 클라이언트를 설정할 때마다 서버는 연결을 계속 수신합니다. 클라이언트가이 오류를 연결하려고 시도하면 [WinError 10054] An existing connection was forcibly closed by the remote host이 표시되지만 서버는 여전히 연결을 수신 대기합니다. 나는 확실히 포트 7999이 열려 있고 서버가 로컬 네트워크에없는 경우 3.6.1Python : [WinError 10054] 기존 연결이 원격 호스트에 의해 강제로 닫혔습니다.

스크립트

import socket 
import threading 

clients = [] 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 

mode = input("Enter 1 for server mode and enter 2 for client mode") 
if mode == "1": 

    def get_ip(): 
     try: 
      stest = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
      stest.connect(('10.255.255.255', 1)) 
      IP = stest.getsockname()[0] 
     except: 
      IP = '127.0.0.1' 
     finally: 
      stest.close() 
     return IP 
    print("IP: " + get_ip()) 
    s.bind((get_ip(), 7999)) 
    s.setblocking(0) 

    print("Waiting for connection") 
    while True: 
     try: 
      data, addr = s.recvfrom(1024) 
      if data.decode() == "Quit": 
       quitmsg = str(addr) + " has quit the chat." 
       clients.remove(addr) 
       for client in clients: 
        s.sendto(quitmsg.encode("utf-8"), client) 
       break 
      elif data: 
       print(data) 
       if addr not in clients: 
        clients.append(addr) 
        print(clients[0]) 
       for client in clients: 
        print(client) 
        s.sendto(data, client) 
     except: 
      pass 
     s.close() 


elif mode == "2": 
    while True: 
     try: 
      ip = input("Enter in an ip address") 
      socket.inet_aton(ip) 
     except socket.error: 
      print("Invalid IPv4 address") 
      continue 
     while True: 
      try: 
       port = int(input("Enter in port number")) 
      except ValueError: 
       print("Invalid Port Number") 
       continue 
      break 
     addr = (ip, port) 
     break 
    s.connect(addr) 

    def recvmsg(): 
     while True: 
      data = s.recv(1024) 
      print(data) 
      if data: 
       print(data.decode()) 

    recv = threading.Thread(name="recvmsg", target=recvmsg) 
    recv.daemon = True 
    recv.start() 

    while True: 
     string = input(">>") 
     s.send(string.encode("utf-8")) 
     if str(string) == "Quit": 
      break 

    s.close() 
    print("Disconnected from chat") 

Server Waiting For connection even after client can't connect

Client Failed to connect

답변

0

만들 파이썬 버전을 사용하고 있습니다 액세스 포인트에서 전달됩니다. 그렇지 않으면 패킷이 라우터에 의해 삭제됩니다. 로컬 네트워크에있는 경우에도 전달을 시도하십시오. 또한 https://www.speedguide.net/port.php?port=7999에 따르면 포트가 웜에 의해 사용되었으므로 다른 네트워크에서 영구적으로 닫힐 수 있습니다. ?? 있을 법하지는 않지만 가능합니다.

+0

처음에는 라우터가 AP 격리 모드에 있다고 생각했지만 루프백 인터페이스 127.0.0.1에서 시도해 보았습니다. 동일한 응답을 받았습니다. – Kozero

+0

흠, 방화벽 문제 일 수 있습니까? 로그를 살펴 봅니다. – pointerless

+0

다른 스크립트에서 클라이언트와 서버 기능을 사용할 때 정상적으로 작동하고 있다고 생각하지 않습니다. 또한 문제가되는 경우 동일한 스크립트의 두 가지 인스턴스를 실행합니다. – Kozero