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

Author Topic: TCP Listening server doesn't get whats sent to it  (Read 4412 times)

0 Members and 1 Guest are viewing this topic.

EClaesson

  • Newbie
  • *
  • Posts: 13
    • MSN Messenger - emanuel.claesson@gmail.com
    • View Profile
    • Email
TCP Listening server doesn't get whats sent to it
« on: August 27, 2009, 08:01:54 am »
Hi, i'm having some problems. I have a client with a TCP Socket that connects to a server and sends some data to it. But the server doesn't respond.

Code: [Select]
if(sock == listener)
{
sf::IPAddress addr;
sf::SocketTCP client;
listener.Accept(client, &addr);

cout << "Client connected (" << addr << ") ID:" << i << endl;

//Add the connecting client to the selector
selector.Add(client);
}
else
{
//Not the listener, it's a client sendind something. Hooray!
sf::Packet packet;

if(sock.Receive(packet) == sf::Socket::Done)
{
std::string msg;
packet >> msg;

cout << "Incoming data: '" << msg << "'" << endl;
}
else
{
//Something is messed up..
cout << "Client (ID:" << i << ") disconnected" << endl;
selector.Remove(sock); //So long!
}
}


It notices when a client connects, or disconnects. But it never prints out incoming packets.

This is code from the client that sends data to the server:
Code: [Select]
if(socket.Send(data.c_str(), sizeof(data.c_str()) != sf::Socket::Done)
{
cout << "Unable to send packet" << endl;
return false;
}
else
{
cout << "Sent packet: '" << data << "' (" << sizeof(data.c_str()) << " Bytes)" << endl;
return true;
}


"data" is an std::string. I have tested with a char[], but no difference. Something that disturbs me a bit as well, is that sixeof(data.c_str()) seems to always return 4.

Any reason why the server don't get the data, or why the client wouldn't send it?
//Emanuel Claesson

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
TCP Listening server doesn't get whats sent to it
« Reply #1 on: August 27, 2009, 06:00:34 pm »
try
Code: [Select]
sizeof(char) * (data.size() + 1)

i guess you get 4 all the time because you check the size of a pointer.

EClaesson

  • Newbie
  • *
  • Posts: 13
    • MSN Messenger - emanuel.claesson@gmail.com
    • View Profile
    • Email
TCP Listening server doesn't get whats sent to it
« Reply #2 on: August 27, 2009, 07:14:07 pm »
Oh.. :P but why are no data sent?
//Emanuel Claesson

Jaenis

  • Newbie
  • *
  • Posts: 48
    • View Profile
TCP Listening server doesn't get whats sent to it
« Reply #3 on: August 28, 2009, 05:28:00 am »
Hmm
The reason actually seems to be that you read different data than you send...

When you are sending packet let's say you send a string "hello". Which is 5 letters, so sent data will contain only those 5 letters.

But, when you are reading the data with sf::Packet the data is expected to be "\5hello".
When sf::Packet tries to read a string, the first item it reads is the length of the string. Which is number 5 in this case, then it proceeds to read 5 letters, if packet is long enough.
In your case, you sent data "hello", so the length of the string will be 104 (ascii value for letter 'h'). Since the packet doesn't contain enough data the reading will fail and string will be empty.

In short, use sf::Packet for sending if you are reading with sf::Packet, something like:
Code: [Select]
sf::Packet packet;
packet << data;
if (socket.Send(packet) != sf::Socket::Done)
   ... etc