Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm working on a client program, and I've got it connecting to the server fine when it's on my own computer (when the code is as-is seen below). The problem comes when I change the IP address in the code to my real IP and try running the client on another machine. I get the "Error initializing socket" code, and it doesn't run any further. Does anyone know how to get around this? I've been stuck here for a couple of days and couldn't find the answer through Googling.

Here's the client code:
C++
#include <winsock2.h>
#include <windows.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <conio.h>
#include <iostream> 
int main(int argv, char** argc)
{
    //The port and address you want to connect to
    int host_port= 1101;
    char* host_name="127.0.0.1"; // here's where I'm putting my IP address and trying to connect
   

    // Initialize socket support WINDOWS ONLY!
    unsigned short wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 2 );
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 ||
      HIBYTE( wsaData.wVersion ) != 2 )) {
      fprintf(stderr, "Could not find useable sock dll %d\n",WSAGetLastError());
      return 0;
    }

    // Initialize sockets and set any options
    int hsock;
    int * p_int ;
    hsock = socket(AF_INET, SOCK_STREAM, 0);
    if(hsock == -1){
       printf("Error initializing socket %d\n",WSAGetLastError());
       return 0;
    }
    p_int = new int;
    *p_int = 1;
    if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
      (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
      printf("Error setting options %d\n", WSAGetLastError());
      delete p_int;
      return 0;
   }
   delete p_int;

   //Connect to the server
   struct sockaddr_in my_addr;
   my_addr.sin_family = AF_INET ;
   my_addr.sin_port = htons(host_port);
   
   memset(&(my_addr.sin_zero), 0, 8);
   my_addr.sin_addr.s_addr = inet_addr(host_name);
   if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
     fprintf(stderr, "*Error connecting* (Socket %d)\n", WSAGetLastError());
     return 0;
   }

   // Now let's do the client related stuff
   char buffer[1024];
   int buffer_len = 1024;
   int bytecount;
   int c;
   memset(buffer, '\0', buffer_len);
   for(char* p=buffer ; (c=_getch())!=13 ; p++){
      printf("%c", c);
      *p = c;
   }
   if( (bytecount=send(hsock, buffer, strlen(buffer),0))==SOCKET_ERROR){
       fprintf(stderr, "*Error sending data* (%d)\n", WSAGetLastError());
       return 0;
   }
   if((bytecount = recv(hsock, buffer, buffer_len, 0))==SOCKET_ERROR){
       fprintf(stderr, "*Error receiving data* (%d)\n", WSAGetLastError());
       return 0;
   }
   printf("\nReceived string \"%s\"\n", buffer);
   closesocket(hsock);
;
}
Posted
Comments
enhzflep 5-May-12 14:13pm    
Just had a quick look - If I consider only the part of the code before it fails, I can see just a couple of small differences.

1) I'm starting up the WSA system with version 0x101 (you used 0x202)
2) Where you have: hsock = socket(AF_INET, SOCK_STREAM, 0); I have the same, though the 0 is replaced with IPPROTO_TCP (I understand this to likely be a conscious choice)

Here's the whole function I use to establish a connection, if it's of any use.


// return the socket number for a TCP connection
SOCKET connectToServer(char *szServerName, WORD portNum)
{
struct hostent *hp;
unsigned int addr;
struct sockaddr_in server;
SOCKET conn;

conn = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (conn == INVALID_SOCKET)
return NULL;

if(inet_addr(szServerName)==INADDR_NONE)
{
hp=gethostbyname(szServerName);
}
else
{
addr=inet_addr(szServerName);
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
}

if(hp==NULL)
{
closesocket(conn);
return NULL;
}

server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
server.sin_family=AF_INET;
server.sin_port=htons(portNum);
if(connect(conn,(struct sockaddr*)&server,sizeof(server)))
{
closesocket(conn);
return NULL;
}
return conn;
}
Code-o-mat 5-May-12 14:35pm    
Might help if you also tell us what the last error code was.
Pikky86 5-May-12 14:43pm    
There is no error code since it compiles fine. The only error is that it won't connect to the server when I actually try using the client.
Code-o-mat 5-May-12 14:51pm    
You said you get "Error initializing socket", so this means, this code runs:

printf("Error initializing socket %d\n",WSAGetLastError());

See the call to WSAGetLastError() in there? I'm talking about what that returns. What's the number you get after the text "Error initializing socket"?
Pikky86 5-May-12 15:31pm    
Oh sorry I misunderstood. The error I get for
[CODE]
if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
fprintf(stderr, "*Error connecting* (Socket %d)\n", WSAGetLastError());
[/CODE]

is '10061'

1 solution

I solved the problem by downloading the Portforward Static IP program, and changed my dynamic IP to a static IP (I also did port forwarding on my router, and left the IP in my code at my regular, home IP address), and everything works! I tried connecting with another computer and it's fine now. Woohoo!
 
Share this answer
 
Comments
JackDingler 7-May-12 11:41am    
So your issue, is with NAT then. Are you sure you gave us the right error string?
michaelmel 7-May-12 20:00pm    
Yes it probably was the right code - router without port forwarding would refuse connection on a requested port - error code sounds about right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900