2012-11-11 5 views
0

특정 포트 번호를 수신하는 소켓 서버를 구현하려고합니다. 클래스를 작성하지 않고 코드를 작성하면 올바르게 작동합니다. 내가받은액세스중인 회원의 문제

import socket; 
from ServerConfig import ServerConfig; 

class SyncServerRK: 
    def __init__(self): 
     self.config = ServerConfig()   #Call Initialize config class   
     #Send my IP address to managing_agent 
     self.Listener()   #Call listener method 

    def Listener(self): 
     s = socket.socket()   # Create a socket object 
     host = socket.gethostname()      # Get local machine name 
     port = self.config.Connect_Port()    # Reserve a port for your service. 
     s.bind((host, port))   # Bind to the port 
     while True: 
      c, addr = s.accept()  # Establish connection with client. 
      print ('Got connection from', addr) 
      c.send('Thank you for connecting'.encode()) 
      print ('Message received:',c.recv(1024).decode()) 
      c.close()    # Close the connection    
     print(self.config.Managing_Agent()) 

if __name__ == "__main__": 
    SyncServerRK() 

오류는 다음과 같습니다 :

Traceback (most recent call last): 
    File "C:/Share/SyncServerRK.py", line 24, in <module> 
    SyncServerRK() 
    File "C:/Share/SyncServerRK.py", line 8, in __init__ 
    self.Listener()   #Call listener method 
    File "C:/Share/SyncServerRK.py", line 16, in Listener 
    c, addr = s.accept()  # Establish connection with client. 
    File "C:\Python33\lib\socket.py", line 135, in accept 
    fd, addr = self._accept() 
OSError: [WinError 10022] An invalid argument was supplied 

사람은 객체 지향 철학을 사용하여 스레드 서버 소켓을 구현하는 방법을 알려 주시기 바랍니다 수하지만 아래와 같은 클래스를 구현할 때 작동되지 않습니다.

잘 작동 비 클래스 버전 :

import socket    # Import socket module 

s = socket.socket()   # Create a socket object 
host = socket.gethostname() # Get local machine name 
port = 12345    # Reserve a port for your service. 
s.bind((host, port))  # Bind to the port 

s.listen(5)     # Now wait for client connection. 
while True: 
    c, addr = s.accept()  # Establish connection with client. 
    print ('Got connection from', addr) 
    c.send('Thank you for connecting'.encode()) 
    print ('Message received:',c.recv(1024).decode()) 
    c.close()    # Close the connection  
+0

http://docs.python.org/2/library/socketserver.html에는 모든 종류의 소켓 서버 (threaded, forked, async)에 대한 예제가 있습니다. – xbonez

+0

당신에게 적합한 비 클래스 기반 버전을 게시하십시오. . – gecco

+0

@gecco는 질문을 편집하여 작동 한 비 클래스 기반 버전을 게시합니다. 감사합니다 – Romaan

답변

1

당신은 클래스 기반 버전의 s.listen(5) 누락되었습니다. 연결을 허용하기 전에 소켓을 주소에 바인딩하고 연결을 청취해야합니다.

+0

빙고 ... 고마워요 @gecco. 나는 아주 어리 석다. (다시 한번 감사드립니다. – Romaan

관련 문제