Click to See Complete Forum and Search --> : Newbie question


dullboy
February 20th, 2006, 02:31 PM
I am trying to create a socket and then connect to a IP address. But I got an error "WSAECONNREFUSED" after I called funtion WSAGetLastError(). Here is part of the code.


main()
{
int sock;
WORD wVersionRequested;
WSADATA wsaData;

wVersionRequested = MAKEWORD(2,2);

WSAStartup(wVersionRequested,&wsaData);
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

if(sock == -1)
{
printf("Invalid socket\n");
exit(1);
}
struct sockaddr_in sa;
memset(&sa,0,sizeof(sa));

sa.sin_addr.s_addr = inet_addr(argv[1]);
sa.sin_family = AF_INET;
sa.sin_port = 7;

connect(sock,(struct sockaddr*)&sa,sizeof(sa));
int err = WSAGetLastError();

switch(err){
case WSANOTINITIALISED:
printf("WSANOTINITIALISED\n");
break;
case WSAENETDOWN:
printf("WSAENETDOWN\n");
break;
case WSAEADDRINUSE:
printf("WSAEADDRINUSE\n");
break;
case WSAEINTR:
printf("WSAEINTR\n");
break;
case WSAEINPROGRESS:
printf("WSAEINPROGRESS\n");
break;
case WSAEALREADY:
printf("WSAEALREADY\n");
break;
case WSAEADDRNOTAVAIL:
printf("WSAEADDRNOTAVAIL\n");
break;
case WSAEAFNOSUPPORT:
printf("WSAEAFNOSUPPORT\n");
break;
case WSAECONNREFUSED:
printf("WSAECONNREFUSED\n");
break;
case WSAEFAULT:
printf("WSAEFAULT\n");
break;
case WSAEINVAL:
printf("WSAEINVAL\n");
break;
case WSAEISCONN:
printf("WSAEISCONN\n");
break;
case WSAENETUNREACH:
printf("WSAENETUNREACH\n");
break;
case WSAENOBUFS:
printf("WSAENOBUFS\n");
break;
case WSAENOTSOCK:
printf("WSAENOTSOCK\n");
break;
case WSAETIMEDOUT:
printf("WSAETIMEDOUT\n");
break;
case WSAEWOULDBLOCK:
printf("WSAEWOULDBLOCK\n");
break;
case WSAEACCES:
printf("WSAEACCES\n");
break;
default:
break;
}

if(err!=0)
{
printf("Connection failed\n");
exit(1);
}

...

}


Can any networking GURU here points out what could be the reason? Thanks for your inputs.

wildfrog
February 20th, 2006, 02:47 PM
You should convert the port number from 'host byte order' to 'network byte order':

sa.sin_port = htons(7);

Instead of that error reporting switch statement you can use FormatMessage (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp).

- petter

dullboy
February 20th, 2006, 03:06 PM
Thanks for your response! But after I use htons to convert the port number from 'host byte order' to 'network byte order', I still got the same error. Do you have any further idea? Thanks for your inputs.
You should convert the port number from 'host byte order' to 'network byte order':

sa.sin_port = htons(7);

Instead of that error reporting switch statement you can use FormatMessage (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp).

- petter

dullboy
February 20th, 2006, 03:43 PM
Could you also tell me how to use FormatMessage? Here is the way I use FormatMessage but it seems it doesn't work.

int err = WSAGetLastError();

LPTSTR s;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,err, 0,(LPTSTR)&s,0,NULL);



I checked s and s is actually "The requested address is not valid in its context.". Sorry for so many questions. :(
You should convert the port number from 'host byte order' to 'network byte order':

sa.sin_port = htons(7);

Instead of that error reporting switch statement you can use FormatMessage (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp).

- petter

Nedals
February 20th, 2006, 04:45 PM
Try this....

//int sock;
//WORD wVersionRequested;
WSADATA wsaData;

//wVersionRequested = MAKEWORD(2,2);
//WSAStartup(wVersionRequested,&wsaData);
//sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

WSAStartup(MAKEWORD(1,1),&wsaData);
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(sock == -1)
{
printf("Invalid socket\n");
exit(1);
}

//struct sockaddr_in sa;
//memset(&sa,0,sizeof(sa));

sockaddr_in sa;
sa.sin_addr.s_addr = inet_addr(argv[1]);
sa.sin_family = AF_INET;
sa.sin_port = 7;

//connect(sock,(struct sockaddr*)&sa,sizeof(sa));
connect(sock,(sockaddr*)&sa,sizeof(sa));
....

wildfrog
February 20th, 2006, 06:27 PM
Could you also tell me how to use FormatMessage?Check out Retrieving the Last-Error Code (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/retrieving_the_last_error_code.asp).

- petter