2012-05-08 4 views
1

에서 분리 할 때 나는이 점점 계속 :오류 서버

Traceback (most recent call last): 
    File "C:\Users\T_Mac\Desktop\Rex's Stuff\PyNet\Client.py", line 14, in <module 
> 
    server.connect(ADDRESS) 
    File "C:\Python27\lib\socket.py", line 224, in meth 
    return getattr(self._sock,name)(*args) 
    File "C:\Python27\lib\socket.py", line 170, in _dummy 
    raise error(EBADF, 'Bad file descriptor') 
socket.error: [Errno 9] Bad file descriptor 

내 서버로이 코드 'changeclient'실행

# Server 

from socket import * 

PORT = 5000 
BUFSIZE = 1024 
ADDRESS = ('', PORT)  # '' = all addresses. 
server = socket(AF_INET, SOCK_STREAM) 

server.bind(ADDRESS) 
server.listen(5) 
# print stuff the user needs to know 
print '' 
print ' ____    _____  ___ _______ ' 
print '/ \ |  |/ \ /____\  |  ' 
print '|  | |  | |  | |   |  ' 
print ' \____/ \____/| |  | \____/  | v0.1' 
print ' |    |        ' 
print ' |    |        ' 
print ' |    |        ' 
print ' |  _____/        ' 
print 'Contact Rex for any bug reports at [email protected]' 
print '\n' 
print 'Please input the command when prompted with \'>\'' 
print 'The stdout stuff will be in this format: ' 
print '  (<stdout>, <stderr>)\n' 

while True: 
    END_SCRIPT = 0           #Setting command to something other than '1' 
    print '\nWaiting for connections...' 
    client, address = server.accept() 
    print '...client connected from ', address[0], '\n' 
    while True: 
     command = raw_input('> ') 
     if command == 'quit': 
      server.close() 
      END_SCRIPT = 1 
      break 
     elif command == 'exit': 
      server.close() 
      END_SCRIPT = 1 
      break 
     elif command == 'changeclient': 
      print 'Changing clients.....\n' 
      client.send(command) 
      break 
     else: 
      client.send(command) 
      commandJazz = client.recv(BUFSIZE) 
      print commandJazz 
    if END_SCRIPT == 1: 
     print 'Closing server......' 
     print 'Goodbye!' 
     break 
server.close() 

을 그리고이 내 클라이언트로 :

# client 

from subprocess import * 
from socket import * 
import time 
test = 0 
PORT = 5000 
IP = 'localhost'  #To change it to your ip, delete 'raw_input('> ')' and put your IP in its place. 
BUFSIZE = 1024 
ADDRESS = (IP, PORT) 
server = socket(AF_INET, SOCK_STREAM) 

while True: 
    server.connect(ADDRESS) 
    while True: 
     command = server.recv(BUFSIZE) 
     if command == 'changeclient': 
      server.close() 
      test = 1 
      break 
     else: 
      executeIt = Popen(command, shell = True, stdin = PIPE, stdout = PIPE, stderr = STDOUT) 
      commandJazz = executeIt.communicate() 
      strCommandJazz = str(commandJazz) 
      server.send(strCommandJazz) 

내 서버를 실행 한 다음 내 클라이언트의 인스턴스 두 개를 실행합니다. 그것은 잘 연결하고 모든 것이 잘 작동합니다. changeclient라는 명령을 사용하여 현재 클라이언트의 연결을 끊고 다른 클라이언트에 연결합니다. changeclient를 실행할 때마다 클라이언트에 이전에 게시 된 오류가 표시됩니다.

답변

1

소켓을 닫을 때 다시 사용하지 마십시오. 새 것을 만드십시오 :

server = socket(AF_INET, SOCK_STREAM) 
server.connect(ADDRESS) 

바로 지금 동일한 소켓 인스턴스에서 다시 연결을 시도하고 있습니다.

새 포트를 만드는 대신 다시 열 때 포트를 다시 사용하도록 소켓에 알리는 플래그를 사용해 볼 수도 있습니다.

server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)