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:
#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;
server.sin_family = AF_INET; server.sin_port = htons(7654); server.sin_addr.s_addr = INADDR_ANY;
error = bind(mySocket, (sockaddr*)&server, sizeof(server));
if (error == SOCKET_ERROR)
{
cout << "Server: Ah ****... Somethings wrong with binding\n";
}
error = listen(mySocket, 5);
if (error == SOCKET_ERROR)
{
cout << "Server: Too deaf to listen...\n"; }
cout << "Server: Waiting for a client to connect ...\n";
int clientSocket;
clientSocket = accept(mySocket, 0, 0);
if (clientSocket == SOCKET_ERROR)
{
cout << "I DUN WANNA!\n"; }
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:
#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";
struct hostent *host_entry;
host_entry = gethostbyname(server_name);
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));
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