2012-03-19 4 views
1

моя проблема заключается в том, что я не могу подключить два компьютера через сокет (windows xp и windows7), хотя сервер, созданный с помощью сокета, прослушивает, и я могу его подключить. Он получает тогда информацию и делает то, что должно быть сделано, но если я запустил соответствующий сокет-клиент, я получаю ошибку 10061. Более того, я за брандмауэром - эти два компьютера работают в моей локальной сети, брандмауэры Windows отключены,Не удалось подключиться к сокету, telnet OK

comp1 [клиент]: 192.168.1.2 порт 12345

comp2 [сервер]: 192.168.1.5 порт 12345

маршрутизатор: 192.168.1.1

Возможно перенаправление портов может помочь? Но самое важное для меня - ответить на вопрос, почему Sockets терпит неудачу, если telnet работает нормально.

клиент:

int main(){ 
     // Initialize Winsock. 
     WSADATA wsaData; 
     int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 
     if (iResult != NO_ERROR) 
      printf("Client: Error at WSAStartup().\n"); 
     else 
      printf("Client: WSAStartup() is OK.\n"); 
     // Create a socket. 
     SOCKET m_socket; 
     m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 

     if (m_socket == INVALID_SOCKET){ 
      printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError()); 
      WSACleanup(); 
      return 7; 
     }else 
      printf("Client: socket() is OK.\n"); 

     // Connect to a server. 
     sockaddr_in clientService; 

     clientService.sin_family = AF_INET; 
     //clientService.sin_addr.s_addr = inet_addr("77.64.240.156"); 
     clientService.sin_addr.s_addr = inet_addr("192.168.1.5"); 
     //clientService.sin_addr.s_addr = inet_addr("87.207.222.5"); 
     clientService.sin_port = htons(12345); 

     if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR){ 
      printf("Client: connect() - Failed to connect.\n"); 
      wprintf(L"connect function failed with error: %ld\n", WSAGetLastError()); 
      iResult = closesocket(m_socket); 
      if (iResult == SOCKET_ERROR) 
      wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError()); 
      WSACleanup(); 
      return 6; 
     } 

     // Send and receive data 
     int bytesSent; 
     int bytesRecv = SOCKET_ERROR; 
     // Be careful with the array bound, provide some checking mechanism 
     char sendbuf[200] = "Client: Sending some test string to server..."; 
     char recvbuf[200] = ""; 

     bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0); 
     printf("Client: send() - Bytes Sent: %ld\n", bytesSent); 

     while(bytesRecv == SOCKET_ERROR){ 
      bytesRecv = recv(m_socket, recvbuf, 32, 0); 
      if (bytesRecv == 0 || bytesRecv == WSAECONNRESET){ 
       printf("Client: Connection Closed.\n"); 
       break; 
      }else 
       printf("Client: recv() is OK.\n"); 

      if (bytesRecv < 0) 
       return 0; 
      else 
       printf("Client: Bytes received - %ld.\n", bytesRecv); 
     } 
     system("pause"); 
     return 0; 
    } 

сервер:

int main(){ 
WORD wVersionRequested; 
WSADATA wsaData={0}; 
int wsaerr; 

// Using MAKEWORD macro, Winsock version request 2.2 
wVersionRequested = MAKEWORD(2, 2); 
wsaerr = WSAStartup(wVersionRequested, &wsaData); 
if (wsaerr != 0){ 
    /* Tell the user that we could not find a usable WinSock DLL.*/ 
    printf("Server: The Winsock dll not found!\n"); 
    return 0; 
}else{ 
     printf("Server: The Winsock dll found!\n"); 
     printf("Server: The status: %s.\n", wsaData.szSystemStatus); 
} 

/* Confirm that the WinSock DLL supports 2.2.*/ 
/* Note that if the DLL supports versions greater */ 
/* than 2.2 in addition to 2.2, it will still return */ 
/* 2.2 in wVersion since that is the version we  */ 
/* requested.          */ 
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2){ 
/* Tell the user that we could not find a usable WinSock DLL.*/ 
printf("Server: The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion)); 
     WSACleanup(); 
     return 0; 
}else{ 
     printf("Server: The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion)); 
     printf("Server: The highest version this dll can support: %u.%u\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion)); 
} 
//////////Create a socket//////////////////////// 
//Create a SOCKET object called m_socket. 
SOCKET m_socket; 
// Call the socket function and return its value to the m_socket variable. 
// For this application, use the Internet address family, streaming sockets, and the TCP/IP protocol. 
// using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4 
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 

// Check for errors to ensure that the socket is a valid socket. 
if (m_socket == INVALID_SOCKET){ 
    printf("Server: Error at socket(): %ld\n", WSAGetLastError()); 
    WSACleanup(); 
    //return 0; 
}else{ 
    printf("Server: socket() is OK!\n"); 
} 

////////////////bind////////////////////////////// 
// Create a sockaddr_in object and set its values. 
sockaddr_in service; 

// AF_INET is the Internet address family. 
service.sin_family = AF_INET; 
// "127.0.0.1" is the local IP address to which the socket will be bound. 
service.sin_addr.s_addr = htons(INADDR_ANY);//inet_addr("127.0.0.1");//htons(INADDR_ANY); //inet_addr("192.168.1.2"); 
// 55555 is the port number to which the socket will be bound. 
// using the htons for big-endian 
service.sin_port = htons(12345); 

// Call the bind function, passing the created socket and the sockaddr_in structure as parameters. 
// Check for general errors. 
if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR){ 
    printf("Server: bind() failed: %ld.\n", WSAGetLastError()); 
    closesocket(m_socket); 
    //return 0; 
}else{ 
    printf("Server: bind() is OK!\n"); 
} 
// Call the listen function, passing the created socket and the maximum number of allowed 
// connections to accept as parameters. Check for general errors. 
if (listen(m_socket, 1) == SOCKET_ERROR) 
     printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError()); 
else{ 
    printf("Server: listen() is OK, I'm waiting for connections...\n"); 
} 

// Create a temporary SOCKET object called AcceptSocket for accepting connections. 
SOCKET AcceptSocket; 

// Create a continuous loop that checks for connections requests. If a connection 
// request occurs, call the accept function to handle the request. 
printf("Server: Waiting for a client to connect...\n"); 
printf("***Hint: Server is ready...run your client program...***\n"); 
// Do some verification... 
while (1){ 
    AcceptSocket = SOCKET_ERROR; 

     while (AcceptSocket == SOCKET_ERROR){ 
     AcceptSocket = accept(m_socket, NULL, NULL); 
     } 
    // else, accept the connection... note: now it is wrong implementation !!!!!!!! !! !! (only 1 char) 
    // When the client connection has been accepted, transfer control from the 
    // temporary socket to the original socket and stop checking for new connections. 
    printf("Server: Client Connected! Mammamija. \n"); 
    m_socket = AcceptSocket; 
    char recvBuf[200]=""; 
    char * rc=recvBuf; 
    int bytesRecv=recv(m_socket,recvBuf,64,0); 

    if(bytesRecv==0 || bytesRecv==WSAECONNRESET){ 
     cout<<"server: connection closed.\n"; 
     }else{ 
      cout<<"server: recv() is OK.\n"; 
      if(bytesRecv<0){ 
       return 0; 
       }else{ 
        printf("server: bytes received: %ld.\n",recvBuf); 
      } 
    } 

выход из клиента:

PS C:\Users\Piter\documents\vs2010\projects\client_socket\debug> ./client_socket.exe 
Client: WSAStartup() is OK. 
Client: socket() is OK. 
Client: connect() - Failed to connect. 
connect function failed with error: 10061 
PS C:\Users\Piter\documents\vs2010\projects\client_socket\debug> ipconfig 

Я Создали прослушивание сокета с помощью Netcat и PowerShell (не уверен, надлежащим образом):

PS C:\netcat> ./nc.exe -v -l -p 12345 
listening on [any] 12345 ... 
Warning: forward host lookup failed for cf16.chello.pl: h_errno 11001: HOST_NOT 
connect to [192.168.1.2] from cf16.chello.pl [192.168.1.2] 4473: HOST_NOT_FOUND 

третьи и четвертые строки то, что произошло при создании клиента в другом с помощью PowerShell:

PS C:\netcat> ./nc.exe 192.168.1.2 12345 

ок. при вынужденном NetStat не разрешить, но использовать IP задан, теперь он соединяет:

PS C:\netcat> ./nc.exe -n -v -l -p 12345 
listening on [any] 12345 ... 
connect to [192.168.1.2] from (UNKNOWN) [192.168.1.2] 4622 

но мой C++ все равно возвращается ошибка 10061. нет сообщений в PowerShell с сервером работает - кажется, что мой клиент не подключается на всех - сервер ничего не говорит, только клиент, что есть ошибка 10061. любые идеи? пожалуйста, помогите: D

+0

Вы не даете нам много информации! Как вы создаете «сокеты» и как вы точно тестируете соединение? –

+0

becouse Я думаю, что создание сокетов в порядке, но без проблем я прикрепил код. – 4pie0

+1

Ошибка '10061'' WSAECONNREFUSED', что означает, что ваш клиент может связаться с сервером, но он отказывается от соединения. Либо серверная программа не запущена, либо серверный компьютер блокирует соединение с брандмауэром. –

ответ

2

Вы сказали, что

comp1: 192.168.1.2 port 12345 

comp1: 192.168.1.6 port 12345 

Я не знаю, какой компьютер является сервером и клиентом.

Но ваш код клиента выглядит следующим образом.

clientService.sin_addr.s_addr = inet_addr("192.168.1.5"); 

Вы подключаетесь к неправильному серверу. Проверьте свой код.

+0

в коде это нормально, это была ошибка в описании темы, это не причина, почему – 4pie0

+0

делает здесь динамический или статический IP-дистрибутив? – 4pie0

+1

service.sin_addr.s_addr = htons (INADDR_ANY); Вы должны использовать htonl для преобразования. – Magic

Смежные вопросы