|
-
February 20th, 2006, 03:31 PM
#1
Newbie question
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.
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.
-
February 20th, 2006, 03:47 PM
#2
Re: Newbie question
You should convert the port number from 'host byte order' to 'network byte order':
Code:
sa.sin_port = htons(7);
Instead of that error reporting switch statement you can use FormatMessage.
- petter
-
February 20th, 2006, 04:06 PM
#3
Re: Newbie question
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.
 Originally Posted by wildfrog
You should convert the port number from 'host byte order' to 'network byte order':
Code:
sa.sin_port = htons(7);
Instead of that error reporting switch statement you can use FormatMessage.
- petter
-
February 20th, 2006, 04:43 PM
#4
Re: Newbie question
Could you also tell me how to use FormatMessage? Here is the way I use FormatMessage but it seems it doesn't work.
Code:
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. 
 Originally Posted by wildfrog
You should convert the port number from 'host byte order' to 'network byte order':
Code:
sa.sin_port = htons(7);
Instead of that error reporting switch statement you can use FormatMessage.
- petter
-
February 20th, 2006, 05:45 PM
#5
Re: Newbie question
Try this....
Code:
//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));
....
-
February 20th, 2006, 07:27 PM
#6
Re: Newbie question
 Originally Posted by dullboy
Could you also tell me how to use FormatMessage?
Check out Retrieving the Last-Error Code.
- petter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|