accpet() Returns 10014 Error! C/C++ Winsock. Can You Help?

I ported my code from linux to windows. I get weird error about 10014 refering to accept() error...The error lies around the code below.

Output:
Choose a port to listen on: 32324
waiting for a connection ...
ERROR: 10014

&clientInfo_size address of to the exact size of sockaddr. It works in Linux! In Win, doesn't. Why? What should i do?

Note: i have to pass the size by ref.
struct sockaddr clientInfo; //obtains clients information once accept() succeed
int clientInfo_size;  //holds the size of cleintInfo
 
//get the size of sockaddr struct of clientAddr
clientInfo_size = sizeof clientInfo;
 
.
.
.
.
.
 
//accept pending connection. Information of incoming connection will go to clientInfo
clientSockD = accept(serverSockD, &clientInfo, &clientInfo_size);
 
if(clientSockD == INVALID_SOCKET )
{
	cout<<"ERROR: "<<WSAGetLastError()<<endl;
	closesocket(clientSockD);
	closesocket(serverSockD);
	WSACleanup();
	return 5;
}

Open in new window

LVL 1
F-J-KAsked:
Who is Participating?
 
Infinity08Commented:
>> Error is 10061

That's WSAECONNREFUSED.

So, either :

1) you are connecting to the wrong IP and/or port and/or are not using TCP
2) a firewall is blocking the connection (pausing the firewall - whatever that means - will likely not change that)
0
 
Infinity08Commented:
error 10014 means that you used an invalid address. Usually, this means that either the size is incorrect, or an invalid pointer was passed.
0
 
F-J-KAuthor Commented:
yea, its weird. I tried to do  &(sizeof(clientInfo)), it did not work. I suggest i will set clientInfo to struct sockaddr_storage & then cast it back to sockaddr inside accept(). I'll try it. Hopefully, its going to work
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Infinity08Commented:
>> &(sizeof(clientInfo))

Don't do that heh.


Can you show the exact and complete code you are using ?
0
 
F-J-KAuthor Commented:

#include<iostream>
#include<string>
#include<sstream>
#include"Winsock2.h"            //VC++
#include<Ws2tcpip.h>            //VC++
#include<Winerror.h>            //VC++
using namespace std;
 
#define BACKLOG 2    	// how many pending connections queue will hold
#define MAX_DATA 1024	//maximum bytes that could be sent or received 
 
//describe the program
void progDesc()
{
	cout<<"This server listens for a connection from client, and waits\n";
	cout<<"some data from client. Server echos back same message to client."<<endl;
}
 
//get sockaddr, IPv4 or IPv6:
void *get_client_addr(struct sockaddr *clientInformation)
{
	//carries the address of the place which hold the clients' IP Address
	void *addr;
	
	//check the version of client's IP
	if (clientInformation->sa_family == AF_INET) {
		//struct sockaddr_in *ipv4 = (struct sockaddr_in *)clientInformation;
		//addr = &(ipv4->sin_addr);
		addr = &( ((struct sockaddr_in *)clientInformation)->sin_addr ); //same as above
		return addr;
	}else{
		//struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)clientInformation;
		//addr = &(ipv6->sin6_addr);
		addr = &( ((struct sockaddr_in6 *)clientInformation)->sin6_addr );
		return addr;
   	}
}
 
u_short get_client_port(struct sockaddr *clientInformation)
{
	//carries the port number that client connected from
	u_short portNumber;
	
	if (clientInformation->sa_family == AF_INET) {
		struct sockaddr_in *ipv4 = (struct sockaddr_in *)clientInformation;
		portNumber = ipv4->sin_port;
		return portNumber;
	}else{
		struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)clientInformation;
		portNumber = ipv6->sin6_port;
		return portNumber;
   	}
}
 
//Run the program
int main()
{
 
	SOCKET serverSockD;            //the server's socket that listen incoming connections
	SOCKET clientSockD;            //the client's socket that works as a medium between client and server to send(), recv(), etc
	struct addrinfo hints;      //gets some hints about the server information - input
	struct addrinfo *servInfo;  //holds server information - output
	struct sockaddr clientInfo; //obtains clients information once accept() succeed
	int clientInfo_size;  //holds the size of cleintInfo
	char *listenPort = new char[10];    // the port users will be connecting to - the port we (server) will be listening on
 
	//*****************VC++********************//
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
 
	/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
    wVersionRequested = MAKEWORD(2, 2);
 
    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        return 1;
    }
	//*****************VC++********************//
 
	
	cout<<"Choose a port to listen on: ";
	cin>>listenPort;
 
	//set size of hints to zero
	memset(&hints, 0, sizeof hints);
 
	//fill some of the server info (your current host info)
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;     //your (the server's) local ip address will be specified automatically
 
	//output a more specific server's information, such as IP Addresses, to servInfo
	int status = getaddrinfo(NULL, listenPort, &hints, &servInfo);
	
	//make sure getaddrinfo succeeded
	if(status == -1)
	{
		fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
		WSACleanup();
		return 1;
	}
 
    //create a socket - this socket will be used to listen on PORT for incoming connections
	serverSockD = socket(servInfo->ai_family, servInfo->ai_socktype, servInfo->ai_protocol);
 
	if(serverSockD == INVALID_SOCKET )
	{
		cout<<"Error: failed to create a socket.\n";
		closesocket(serverSockD);
		WSACleanup();
		return 2;
	}
	
 
	//now bind the server socket to the ip address & port specified
	if(bind(serverSockD, servInfo->ai_addr, servInfo->ai_addrlen) == SOCKET_ERROR) 
	{
		perror("server: bind");
		closesocket(serverSockD);
		WSACleanup();
	    return 3;
	}
 
	//listen to the incoming connection, take up to 10 connections
	if(listen(serverSockD, BACKLOG) == SOCKET_ERROR) 
	{
	    perror("listen");
		closesocket(serverSockD);
		WSACleanup();
	    return 4;
	}
 
	cout<<"waiting for a connection ... "<<endl;
 
	//get the size of sockaddr struct of clientAddr
	clientInfo_size = sizeof clientInfo;
 
	//holds number of bytes sent
	int bytes_received;
 
	//holds message sent from client
	char data[MAX_DATA];
 
	//holds an IP address of the client's
	char printableClientIP[INET6_ADDRSTRLEN];
 
	//holds the exact IP address for the client() you accepted his/her connection
	void *clientIP;
 
	//holds client's port number that he connected from
	u_short portNum;
 
	//listens forever, and accepts all connections that come in
	while(1)
	{
 
		clientSockD = accept(serverSockD, &clientInfo, &clientInfo_size);
 
		if(clientSockD == INVALID_SOCKET )
		{
			cout<<"ERROR: "<<WSAGetLastError()<<endl;
			closesocket(clientSockD);
			closesocket(serverSockD);
			WSACleanup();
	   		return 5;
		}
 
		//get_client_addr gives you the current client IP Address
		clientIP = get_client_addr(&clientInfo);
 
		//get_client_port gives you the clients' port number
		portNum =  get_client_port(&clientInfo);
 
		inet_ntop(clientInfo.sa_family, clientIP, printableClientIP, sizeof printableClientIP);
 
		u_short portHostByte = ntohs(portNum);
 
		printf("New Client connected from port no. %d and IP %s\n", portHostByte, printableClientIP);
 
		bytes_received = 1;
 
		while(bytes_received)
		{
 
			bytes_received = recv(clientSockD, data, MAX_DATA, 0);
 
			if(bytes_received)
			{
				send(clientSockD, data, bytes_received, 0);
 
				data[bytes_received] = '\0';
				printf("Sent mesg: %s", data);
			}
 
		}
 
		printf("Client disconnected\n");
 
		cout<<endl;
 
		closesocket(clientSockD);
	}
 
	closesocket(serverSockD);
 
	//deallocate
	delete[] listenPort;
 
	//free linkedlist of struct addrinfo *servInfo
	freeaddrinfo(servInfo);
    WSACleanup();
 
	return 0;
}

Open in new window

0
 
F-J-KAuthor Commented:
This is a rough code, i did not do error proper error checkings, etc ...
0
 
Infinity08Commented:
>> i did not do error proper error checkings, etc ...

That's one thing you should never leave out, not even for rough code, because it WILL lead to disaster.


Also, check whether clientInfo_size wasn't modified before the accept call.
0
 
F-J-KAuthor Commented:
Thanks for the tip....I will double check it out later & let you know what the problem is.
0
 
F-J-KAuthor Commented:
Worked....

clientInfo should be declared with a larger size, which sockaddr_storage would make it work...

some other part of the code has been changed slightly...

The problem now is:

I run the server and make it listen to 3342
I run the client and connect to the server on 3342.

Client can not establish connection, it says connection failed!
I'm pretty sure the client works, because i tried to connect it
to one of the open port in my pc, and it connected. Since, i'm making my server
listens on 3342, my client should be able to connect to it...

Note: client and server both worked great under linux. I just ported it to Windows, i did not much change really! so it should work...Any idea about why my client can not establish connection to server/3342?! I tried several ports, same problem....


struct sockaddr_storage clientInfo; 
int clientInfo_size; 
 
clientInfo_size = sizeof clientInfo;
 
.
.
.
.
.
 
clientSockD = accept(serverSockD, (struct sockaddr *)&clientInfo, &clientInfo_size);
 
if(clientSockD == INVALID_SOCKET )
{
        cout<<"ERROR: "<<WSAGetLastError()<<endl;
        closesocket(clientSockD);
        closesocket(serverSockD);
        WSACleanup();
        return 5;
}

Open in new window

0
 
Infinity08Commented:
When your server is running, do a 'netstat -an' to see if it's really listening on that port.
0
 
F-J-KAuthor Commented:
Yes listening:

TCP    [::]:4432              [::]:0                 LISTENING

hm, weird. Client should connect...

I will try to figure it out later and let you know the results,,,,Hopefully, i find a solution
0
 
Infinity08Commented:
I thought you said 3342 ? That's not the same as 4432.

Anyway, assuming your client connects to the right port (TCP, not UDP) ... Is that client on the same machine ? Is there a firewall running on the server machine ? What's the error code returned in the client ?
0
 
F-J-KAuthor Commented:
I changed it to 4432 ...

Yes same machine...
Kaspersky is running...
Error is 10061

I paused the firewall. Error 10061 occurs
0
 
F-J-KAuthor Commented:
If server listens on a specific port, does it mean this port is open?
0
 
F-J-KAuthor Commented:
This usually happens when i try to connect to a blocked port. When i created a simple local port scanner used to get this error 10061(Cables connection) & 10060 (Wireless connections) when port is closed - blocked.
0
 
F-J-KAuthor Commented:
Alright....THANKS for help....I think such a problem is not a big deal....You get the credits :-) as usual
0
 
F-J-KAuthor Commented:
Best Member Surely....
0
 
F-J-KAuthor Commented:
By the way, it didn't work as i was inserting IPv4....
I grabbed my local ipv6 & connected to it. It worked.....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.