Valhalla Legends Forums Archive | C/C++ Programming | connecting to a server with TCP (help)

AuthorMessageTime
MoNksBaNe_Agahnim
i have two programs, one being a server program and one being the client and im trying to link together and send a file from server to client... them but its not allowing me and i'm not sure why...here are parts of the code. Both programs run with no errors they just won't connect to each other :\\

[code]
Client Version:

int YourIP; // holds your ip to be sent to the server
SOCKET mysocket; //your socket
SOCKET mysocket1; // server's socket
mysocket = socket(AF_INET, SOCK_STREAM, 0); //Sets the properties of our socket
WSADATA wsaDAT; // where the winsock data is stored

system("ipconfig");
cout << endl << endl <<"Your ip is show above " << endl << "Please input that IP address: ";
cin >> YourIP;
SOCKADDR_IN SockAddr; //this keeps track of the socket's address info
SockAddr.sin_port = 50;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = YourIP;



cout << "Initializing connection...";

if (WSAStartup(MAKEWORD(1,1), &wsaDAT) == 0)
{         


if(connect(mysocket, (SOCKADDR *)(&SockAddr), sizeof (SockAddr)) == 0) // connecting to server
{

cout << "Connection established, sending request for IP information..." << endl;
cout << "IP information found and being recieved..." << endl << endl;

send(mysocket, ip_request, sizeof(ip_request), 0); // sending request for IP info
            
recv(mysocket1, "ip.txt", sizeof("ip.txt"), 0); // recieving the IP info
            
cout << "File recieved! "; // verifing the ip.txt file was recv.

}

else // if connection failed restart program
{

cout << "Server connection failed!..." << endl;
cout << "quitting program input 1: " << endl;
cin >> end;

   if(end == 1)
{
    system("exit");
   }
               
}
}
[/code]

[code]
Server Version:

int ip_num; // where you input your ip number
SOCKET mysocket1 // your socket
SOCKET mysocket // clients socket
mysocket1 = socket(AF_INET, SOCK_STREAM, 0); //Sets the properties of our socket
int ip_request_accepted;
WSADATA wsaDAT; // where the winsock data is stored

case 1: system("ipconfig");

time_t rawtime;
   struct tm * timeinfo;

   time ( &rawtime );
   timeinfo = localtime ( &rawtime );


   cout << "Input your IP address now (with periods): ";
   cin >> ip_num;
                  
   fout.open("ip.txt");
                  
   fout << "[" << asctime (timeinfo) << "]" << "The server you are connected to's IP address is: "
   << ip_num << endl << endl;

   goto run;
   break;

run:

SOCKADDR_IN SockAddr;      //this keeps track of the socket's address info
SockAddr.sin_port = 50;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = ip_num;


if (WSAStartup(MAKEWORD(1,1), &wsaDAT) == 0)
{

   if (bind(mysocket1, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == 0)
   {
            
               
    listen(mysocket, 1);
    accept(mysocket, NULL, NULL);


    ip_request_accepted = recv(mysocket, ip_request, sizeof(ip_request), 0); // recieving request for IP info
               
      if ((ip_request_accepted != 0)||(ip_request_accepted != WSAECONNRESET))
      {

       send(mysocket1, "ip.txt", sizeof("ip.txt"), 0); // sending IP info
                  
       cout << "Your ip has been successfully sent to the client!" << endl << endl;                           

         else
         {
           cout << "An error has occured...restarting program!";
           goto startup;
         }
         
      }      

   }
         
            
    else
    {
    cout << "Server connection failed!..." << endl;
    cout << "restarting program..." << endl;

    goto startup;
    }

getch();
}
[/code]
November 20, 2003, 2:35 AM
St0rm.iD
Well...right off the bat, you should bind to 0.0.0.0. That's probably your problem.
November 20, 2003, 9:05 PM
MoNksBaNe_Agahnim
hmm how would i do that?
November 20, 2003, 10:54 PM
St0rm.iD
Set the server ip
November 21, 2003, 12:05 AM
MoNksBaNe_Agahnim
kk thanks...I looked up what ya said and also found a new (and better) tutorial on winsock than what i recently was using and looking at that code i did it a lot harder than it needed to be hehe. Thanks bro appreciate the help --[Topic ended]--
November 21, 2003, 12:08 AM

Search