1
Network / Issue with simple Client/Server project
« on: September 07, 2012, 11:45:14 pm »
Hello, my name is Morg. I'm trying to create a very simple, text based application to teach myself networking. So far, it will open up, allow me to input an IP in the client project, say "Connected to Server IP yada yada," and then say the connection fails.
Here is the code for the Server project:
And the Client:
Any help is greatly appreciated .
PS: The purpose of the application is for a server owner to continually send messages to a client.
Here is the code for the Server project:
Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
/* Server Function */
void Server(unsigned short Port)
{
/* Variable Field */
sf::SocketTCP Server;
sf::SocketTCP Client;
sf::IPAddress ClientIP;
bool working=true;
/* Listen to a port */
/* Failure */
if(!Server.Listen(Port))
{
working=false;
cout<<"Error in handling connections"<<endl;
}
/* Success */
else
{
working=true;
cout<<"Listening to port "<<Port<<endl;
}
/* Accept the Client and alert server admin */
Server.Accept(Client,&ClientIP);
cout<<"A client has connected with the IP of"<<ClientIP<<endl;
/* Send messages to client */
char message[128];
/* This loop SHOULD let the server admin continually send new messages to the client */
while(working)
{
cin>>message;
if(Server.Send(message,sizeof(message) != sf::Socket::Done))
{
working=false;
cout<<"Connection Error"<<endl;
}
}
/* Close Sockets */
Client.Close();
Server.Close();
}
int main()
{
/* Port Variable */
int port= 1700;
/* Server Function */
Server(port);
/* Exit */
return 0;
}
And the Client:
Code: [Select]
#include <SFML\Network.hpp>
#include <iostream>
using namespace std;
/* Client Function */
void Client(unsigned short Port)
{
/* Boolean to control our while loops */
bool run=true;
/* Server IP Address */
sf::IPAddress ServerIP;
/* Wait for the user to enter a vaild IP */
do
{
cout<<"Enter a server IP to get started"<<endl;
cin>>ServerIP;
}while(!ServerIP.IsValid());
/* Connecting the Client */
sf::SocketTCP Client;
/* Failure */
if(!Client.Connect(Port,ServerIP))
{
run=false;
cout<<"Failed Connection"<<endl;
}
/* Success */
else
{
run=true;
cout<<"Connected to server with the address of "<<ServerIP<<endl;
}
/* A message to recieve */
char message[128]= "Placer Text";
size_t recieve;
/* While running, constantly receive info from the server */
while(run)
{
/* Connection Failure */
if(Client.Receive(message,sizeof(message),recieve) != sf::Socket::Done)
{
run=false;
cout<<"Connection Error"<<endl;
}
/*Connection Success */
else
{
cout<<message<<endl;
}
}
/* Close up the client */
Client.Close();
}
int main()
{
/* port variable */
int port= 1700;
/* Client Function Call */
Client(port);
/* Pause, then exit */
system("pause");
return 0;
}
Any help is greatly appreciated .
PS: The purpose of the application is for a server owner to continually send messages to a client.