CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Newbie question

  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    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.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  3. #3
    Join Date
    Aug 2000
    Posts
    1,471

    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.
    Quote 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

  4. #4
    Join Date
    Aug 2000
    Posts
    1,471

    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.
    Quote 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

  5. #5
    Join Date
    Feb 2006
    Location
    Pleasanton, California
    Posts
    17

    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));
      ....

  6. #6
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Newbie question

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured