Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm trying to run a client/server on the same machine, with two (.CPP) files, one for the server and the other for the client.But what happens is that the server works up until the listen command, and the client code cannot be reached. Eventhough, i'm sure that the code is correct.
The SERVER code is:
C++
#include <winsock2.h>
#include <iostream>

using namespace std;

int main()
{
    cout <<"Server: Part 2a\n";
    cout <<"CelestialKey's Tutorial\n\n";

    int error;
    WSAData wsaData;

    error = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (error == SOCKET_ERROR)
    {
        cout << "Server: Winsock Failed to begin!\n";
        return 1;
    }

    cout << "Server: WinSocket Started Correctly!\n";
    int mySocket = socket(AF_INET, SOCK_STREAM, 0);

    if (mySocket == SOCKET_ERROR)
    {
        cout << "Server: Socket Failed to initialize!\n";
    }

    cout << "Server: Socket Opened Properly!\n";

    struct sockaddr_in server; // New! Our network socket structure

    server.sin_family = AF_INET; // Always have ti as this for internet
    server.sin_port = htons(7654); // Dont forget the byte ordering
    server.sin_addr.s_addr = INADDR_ANY; // Lets us accept a conenction on ANY internet

    error = bind(mySocket, (sockaddr*)&server, sizeof(server)); // Attempt to bind the socket

    if (error == SOCKET_ERROR)
    {
        cout << "Server: Ah ****... Somethings wrong with binding\n";
    }


    error = listen(mySocket, 5); // Listen for connections

    if (error == SOCKET_ERROR)
    {
        cout << "Server: Too deaf to listen...\n"; // Did we error?!?!
    }

    cout << "Server: Waiting for a client to connect ...\n"; // Just to keep us up to date - ish

    int clientSocket; // Used for the client

    clientSocket = accept(mySocket, 0, 0); // Accepts the client

    if (clientSocket == SOCKET_ERROR)
    {
        cout << "I DUN WANNA!\n"; // did we Fu** up again?
    }

    cout << "Server: Client Connected!\n";

    closesocket(mySocket);

    cout << "Server: Socket Closed\n";

    WSACleanup();

    cout << "Server: WinSocket Shutdown\n";

    cout << "Server: Press any key to shutdown server ...";
    getchar();
    
    return 0;
}


The Client code is:
C++
#include <winsock2.h>
#include <iostream>

using namespace std;


int main()
{
    cout << "Client: Part 2b!\n";
    cout << "Client: CelestialKey's Tutorial\n\n";

    int error;
    WSAData wsaData;

    error = WSAStartup(MAKEWORD(2, 2), &wsaData);

    if (error == SOCKET_ERROR)
    {
        cout << "Client: WinSock Hates me!!!\n";
        return 1;
    }

    cout << "Client: WinSocket Loaded.\n";

    int mySocket = socket(AF_INET, SOCK_STREAM, 0);

    if (mySocket == SOCKET_ERROR)
    {
        cout << "Client: Socket Door is stuck.. Wont open.. -.-;\n";
    }

    cout << "Client: Socket Opened Successfully!\n";


    char server_name[40] = "localhost"; // The server name we will connect to

    // If you are on a network and you want to run the client on another computer, you can get the IP from
    // running "ipconfig" from the command line, and enter that IP address instead of the word "localhost".

    struct hostent *host_entry; // Translates into something the computer can understand

    // The hostent struct contains the ip and anme of the host (Among other things). We can fill it by calling the gethostbyname();

    host_entry = gethostbyname(server_name); // Gather out information

    if (host_entry == NULL)
    {
        cout << "Client: Ah ****, im lost, wheres the host at?\n";
    }

    
    struct sockaddr_in server;

    server.sin_family = AF_INET;
    server.sin_port = htons((unsigned short) 7654);
    server.sin_addr.s_addr = *(unsigned long*) host_entry->h_addr;


    error = connect(mySocket, (sockaddr*)&server, sizeof(server)); // Lets attempt to connect now

    if (error == SOCKET_ERROR)
    {
        cout << "Client: Damn server wont let me connect!\n";
    }

    

    cout << "Client: We are connected!!!\n";
    closesocket(mySocket);

    cout << "Client: Closing Socket.\n";
    WSACleanup();

    cout << "Client: Winsock has been shut down.\n";

    cout << "Press any key to close down Client ...\n";
    getchar();
    
    return 0;
}
I think it's the way i'm attaching the files, or creating the project!!
I'm using C++ Builder 6, using the console wizard. can somebody please tell me what are the EXACT STEPS for making this program work??

Best regards.
Rania
Posted
Updated 5-Mar-13 0:55am
v2
Comments
Michael Haephrati 5-Mar-13 7:44am    
Aren't you creating two projects? each executable should be ran simultaneously.
Richard MacCutchan 5-Mar-13 7:46am    
Try using the official name, or IP address, of the PC in the client code. The localhost name resolves to the address 127.0.0.1 which is the loopback, and I am not sure if that works correctly for client/server.

I have just tested on my system and it does work using the localhost IP address. I suggest using your debugger in your client code to see exactly what is happening. Also check your firewall settings.
raniam 5-Mar-13 8:27am    
HI,,

I've tried your advice and disabled the firewall, but it didnt work either,
Did you add the two files in a single project!!!!, can you please tell me exactly how you did it, because i'm a beginner in socket programming???
How can i use the localhost IP address?

best regards.
Rania
Richard MacCutchan 5-Mar-13 9:23am    
No the two programs are two separate projects. Start the server program first and when you get the message about "waiting for client..." you start the client program. You should also add calls to GetLastError() after you receive a SOCKET_ERROR in order to get the actual details of what caused the errors.
raniam 6-Mar-13 6:08am    
Thank you, i've tried it on C++ Builder XE3, and it worked.
But i dont know why it was not working on C++ Builder6

Regards.

1 solution

Try to make two different projects, one for client and another one for server.

for client check this code:
http://www.win32developer.com/tutorial/winsock/winsock_tutorial_3.shtm[^]

and for server:
http://www.win32developer.com/tutorial/winsock/winsock_tutorial_4.shtm[^]

I hope this will tell you the basic of connectivity of client and server.

Good Luck!
 
Share this answer
 

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