-1

I am new to Winsock programming and came across this code while reading the book "Network Programming For Microsoft Windows " . But it seems that this code is not able to connect to the client. Please tell me how can I fix this problem .

My Server Code :

#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <ws2tcpip.h>

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

int main(){
    WSADATA wsadata;
    int ret;
    if ((ret = WSAStartup(MAKEWORD(2, 2), &wsadata)) != 0){
        cout << "Wsastartup failed" << endl;
    }
    else{
        cout << "connection made successfully" << endl;
    }

    SOCKET ListeningSocket, NewConnection;
    SOCKADDR_IN ServerAddr, ClientAddr;
    int port = 80;

    ListeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_port = htons(port);
    inet_pton(ServerAddr.sin_family,"127.0.0.1",&ServerAddr.sin_addr.s_addr);
    int res= bind(ListeningSocket,(SOCKADDR*)&ServerAddr,sizeof(ServerAddr));
    if (res == SOCKET_ERROR){
        cout << "binding failed" << endl;
    }
    res = listen(ListeningSocket,5);
    if (res == SOCKET_ERROR){
        cout << "Listening failed" << endl;
    }
    int c = 1;
    NewConnection=  accept(ListeningSocket,(SOCKADDR*)&ClientAddr,&c);
    if (NewConnection == INVALID_SOCKET){
cout << "COULD not CONNECT TO CLIENT . err code : "<<WSAGetLastError()  << endl;
    }


    closesocket(ListeningSocket);
    if (WSACleanup() == SOCKET_ERROR){
        cout << "WSACleanup failed with error : " << WSAGetLastError() << endl;
    }
    else{
        cout << "WinSock data cleaned successfully" << endl;
    }
cin.get();
}

On running this code , it shows "COULD not CONNECT TO CLIENT. err code 10014" . I've found this Description of the error code on windows dev center : Bad address.

The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr).

How can I fix this error ?

Saksham
  • 197
  • 3
  • 11
  • if error occurs in [accept](https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526(v=vs.85).aspx), it will return INVALID_SOCKET, not SOCKET_ERROR. try to print the exact error code or meaning. – BEPP Aug 16 '15 at 05:42
  • @NaveenKumar I have edited the code and used INVALID_SOCKET instead of SOCKET_ERROR but still getting the same error . – Saksham Aug 16 '15 at 05:55
  • From the error message you know that the failure occurs in the `accept` call. Looking at the [accept function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526.aspx) documentation we see that when an error occurs "a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.". We can also see a table of possible error codes and their meaning. As @NaveenKumar suggested you should retrieve that specific error to learn more about the `accept` failure. – Frank Boyne Aug 16 '15 at 05:56
  • @FrankBoyne My error code is 10014 and I've found this description on windows dev center : Bad address. I don't know how to fix it .. please help The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr). – Saksham Aug 16 '15 at 06:01
  • @AyushChaurasia read the documentation of the accept function. One of the parameters is wrong. There are 3 parameters. Check them all if you pass the correct data as specified in the documentation. – wimh Aug 16 '15 at 06:06
  • 2
    Try setting `c` to `sizeof(ClientAddr)` instead of `1`? – Dmitri Aug 16 '15 at 06:06
  • Finally this error got fixed by setting `c` to `sizeof(ClientAddr)` – Saksham Aug 16 '15 at 06:12

1 Answers1

1

When you call accept, the variable that the third parameter points to needs to hold the size of the buffer the second parameter points to. (When accept returns, it will hold the amount of space actually used)

In your code, change:

int c = 1;

to

int c = sizeof(ClientAddr);
user253751
  • 57,427
  • 7
  • 48
  • 90