c - stikketilslutning mislykkedes, telnet OK

Indlæg af Hanne Mølgaard Plasc

Problem



mit problem er, at jeg ikke kan forbinde to comps via socket (windows xp og windows7) selvom serveren skabt med socket lytter og jeg kan telnet den. Den modtager så information og gør hvad der skal gøres, men hvis jeg kører det tilsvarende socket klient Jeg får fejl 10061. Desuden er jeg bag firewall - disse to comps kører i mit LAN, windows firewalls er slukket,


comp1 [[klient]]: 192.168.1.2 port 12345


comp2 [[server]]: 192.168.1.5 port 12345


router: 192.168.1.1


Måske kan port forwarding hjælpe? Men det vigtigste for mig er at svare på, hvorfor stikkontakter mislykkes, hvis telnet fungerer fint.


klient:


 int main(){
        // Initialize Winsock.
        WSADATA wsaData;
        int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
        if (iResult != NO\_ERROR)
             printf("Client: Error at WSAStartup().
");
        else
             printf("Client: WSAStartup() is OK.
");
        // 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
", WSAGetLastError());
            WSACleanup();
            return 7;
        }else
           printf("Client: socket() is OK.
");

        // 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.
");
            wprintf(L"connect function failed with error: \%ld
", WSAGetLastError());
            iResult = closesocket(m\_socket);
            if (iResult == SOCKET\_ERROR)
            wprintf(L"closesocket function failed with error: \%ld
", 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
", bytesSent);

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

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


server:


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!
");
    return 0;
}else{
       printf("Server: The Winsock dll found!
");
       printf("Server: The status: \%s.
", 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!
", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
       WSACleanup();
       return 0;
}else{
       printf("Server: The dll supports the Winsock version \%u.\%u!
", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
       printf("Server: The highest version this dll can support: \%u.\%u
", 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
", WSAGetLastError());
    WSACleanup();
    //return 0;
}else{
    printf("Server: socket() is OK!
");
}

////////////////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.
", WSAGetLastError());
    closesocket(m\_socket);
    //return 0;
}else{
    printf("Server: bind() is OK!
");
}
// 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.
", WSAGetLastError());
else{
    printf("Server: listen() is OK, I'm waiting for connections...
");
}

// 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...
");
printf("***Hint: Server is ready...run your client program...***
");
// 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. 
");
    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.
";
        }else{
            cout<<"server: recv() is OK.
";
            if(bytesRecv<0){
                return 0;
                }else{
                    printf("server: bytes received: \%ld.
",recvBuf);
            }
    }


output fra klient:


PS C:UsersPiterdocumentsvs2010projectsclient\_socketdebug> ./client\_socket.exe
Client: WSAStartup() is OK.
Client: socket() is OK.
Client: connect() - Failed to connect.
connect function failed with error: 10061
PS C:UsersPiterdocumentsvs2010projectsclient\_socketdebug> ipconfig


Jeg har skabt lytte socket ved hjælp af netcat og powershell (ikke sikker på en ordentlig måde):


PS C:
etcat> ./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


den tredje og fjerde linje er, hvad der skete, da der oprettedes klient i en anden powerhell af:


PS C:
etcat> ./nc.exe 192.168.1.2 12345


Okay.
når tvunget netstat ikke løser, men bruger IP givet, nu forbinder det:


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


men min C ++ returnerer stadig fejl 10061. Der er ingen besked i powerhellen med serveren kører - synes som min klient ikke har forbindelse til det hele - serveren siger ingenting, kun klient, at der er fejl 10061.
nogle ideer?
Venligst hjælp: D

Bedste reference


Du sagde det


comp1: 192.168.1.2 port 12345

comp1: 192.168.1.6 port 12345


Jeg ved ikke hvilken computer er server og klient.


Men din klientkode er som følger.


clientService.sin\_addr.s\_addr = inet\_addr("192.168.1.5");


Du er tilsluttet en forkert server. Tjek venligst din kode.