Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Issue with simple Client/Server project  (Read 1930 times)

0 Members and 1 Guest are viewing this topic.

Morg

  • Newbie
  • *
  • Posts: 1
    • View Profile
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:

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 :D.

PS: The purpose of the application is for a server owner to continually send messages to a client.

takkai

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Issue with simple Client/Server project
« Reply #1 on: September 08, 2012, 12:24:03 am »
I'm a beginner too with networking.
There's no precise question in your post, but what I can say is about the reserved port:
http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
you're using 1700.
For what I have understood it's better using ports above 49151.
As it's a number difficult to remember, I'm using numbers bewteen 50000 and 65535.