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

Author Topic: Received Packets are blank  (Read 2595 times)

0 Members and 1 Guest are viewing this topic.

phillid

  • Newbie
  • *
  • Posts: 1
    • View Profile
Received Packets are blank
« 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
Code: [Select]

#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
Code: [Select]

#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

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Received Packets are blank
« Reply #1 on: January 12, 2011, 01:51:47 am »
I can't see a problem with the code. Can anyone else help with this???

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Received Packets are blank
« Reply #2 on: January 12, 2011, 10:12:57 am »
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 >>).
Laurent Gomila - SFML developer

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Received Packets are blank
« Reply #3 on: January 14, 2011, 05:33:13 am »
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 :(

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Received Packets are blank
« Reply #4 on: January 17, 2011, 08:58:49 am »
Can anyone help?

 

anything