?
Solved

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

Posted on 2008-11-07
18
Medium Priority
?
2,076 Views
Last Modified: 2013-11-13
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

0
Comment
Question by:F-J-K
  • 12
  • 6
18 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22908857
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
 
LVL 1

Author Comment

by:F-J-K
ID: 22909061
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22909073
>> &(sizeof(clientInfo))

Don't do that heh.


Can you show the exact and complete code you are using ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:F-J-K
ID: 22909105

#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
 
LVL 1

Author Comment

by:F-J-K
ID: 22909120
This is a rough code, i did not do error proper error checkings, etc ...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22909161
>> 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
 
LVL 1

Author Comment

by:F-J-K
ID: 22909176
Thanks for the tip....I will double check it out later & let you know what the problem is.
0
 
LVL 1

Author Comment

by:F-J-K
ID: 22909324
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22909337
When your server is running, do a 'netstat -an' to see if it's really listening on that port.
0
 
LVL 1

Author Comment

by:F-J-K
ID: 22909461
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22909480
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
 
LVL 1

Author Comment

by:F-J-K
ID: 22909513
I changed it to 4432 ...

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

I paused the firewall. Error 10061 occurs
0
 
LVL 1

Author Comment

by:F-J-K
ID: 22909522
If server listens on a specific port, does it mean this port is open?
0
 
LVL 1

Author Comment

by:F-J-K
ID: 22909559
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 22909563
>> 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
 
LVL 1

Author Comment

by:F-J-K
ID: 22909570
Alright....THANKS for help....I think such a problem is not a big deal....You get the credits :-) as usual
0
 
LVL 1

Author Closing Comment

by:F-J-K
ID: 31514550
Best Member Surely....
0
 
LVL 1

Author Comment

by:F-J-K
ID: 22910650
By the way, it didn't work as i was inserting IPv4....
I grabbed my local ipv6 & connected to it. It worked.....
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question