SFML community forums
Help => Network => Topic started by: phillid on January 05, 2011, 01:49:23 am
-
Hello. This is probably a noob question, but I have a sender program and a receiver program that send or receive Packets through TCP Sockets. The code for each is as follows:
Send
#include <SFML/Network.hpp>
#include <iostream>
//////////////////////////////////////////////
// STICS - Simple Two-Way Internet Chat System
// Server Program - Version 0.3
// Uses the SFML network library
// By David Phillips (2010-2011)
//////////////////////////////////////////////
using namespace std;
int main()
{
cout << "STICS - Simple Two-Way Internet Chat System" << endl << "By David Phillips (2010-2011)" << endl << endl;
cout << "Enter the server port:" << endl;
int ServerPort;
cin >> ServerPort;
cout << "Awaiting connection..." << endl;
sf::SocketTCP Listener;
if (!Listener.Listen(ServerPort))
{
cout << "Could not create socket" << endl;
return 0;
}
sf::IPAddress ServerAddress;
sf::SocketTCP ServerRec;
if (Listener.Accept(ServerRec, &ServerAddress) != sf::Socket::Done)
{
cout << "Error with server connection." << endl;
return 0;
}
cout << "Ready" << endl;
sf::Packet Received;
string RecTemp;
//Receiving loop
do
{
ServerRec.Receive(Received);
Received >> RecTemp;
cout << RecTemp << endl;
} while (true);
}
Receiver
#include <SFML/Network.hpp>
#include <iostream>
//////////////////////////////////////////////
// STICS - Simple Two-Way Internet Chat System
// Client Program - Version 0.4
// Uses the SFML network library
// By David Phillips (2010-2011)
//////////////////////////////////////////////
using namespace std;
int main()
{
cout << "STICS - Simple Two-Way Internet Chat System" << endl << "By David Phillips (2010-2011)" << endl << endl;
char ServerIP[64];
int ServerPort;
cout << "Enter server IP:" << endl;
cin >> ServerIP;
cout << endl << "Enter server port:" << endl;
cin >> ServerPort;
sf::SocketTCP ServerSend;
connect:
cout << "Connecting to " << ServerIP << " on port " << ServerPort << "..." << endl;
if (ServerSend.Connect(ServerPort, ServerIP) != sf::Socket::Done)
{
cout << "Connection timed out" << endl;
goto connect;
}
sf::Packet ToSend;
string SendTemp;
cout << "Connection to " << ServerIP << " made. STICS is now ready to chat!" << endl;
do
{
getline(cin,SendTemp);
ToSend << SendTemp;
ServerSend.Send(ToSend);
} while (true);
}
(Sorry for the inefficient coding, I'm just starting C++ and using the SFML Libraries)
When I send something to the receiver, there is just a blank line put on the console window for the Receiver - the packet's empty!!
Could I please get some help on this??
Thanks a lot!!!
:D :D
-
I can't see a problem with the code. Can anyone else help with this???
-
Have you checked what you send before it is sent (SendTemp)? Sometimes there are \n in the input stream and getline returns an empty string.
You should also check the return value of every function that returns a potential error code (Receive, operator >>).
-
When testing I got the program to say back what I had typed in, and I saw nothing out of the ordinary. The return value from both Send() and Receive() are both 0. Still not working :(
-
Can anyone help?