Hello,
It's the first time I use the networking part of SFML and I found out the class "Packet", which looks nice to send and receive data easily.
However, I never receive ANY data when I do something like:
TcpListener tcpListener;
TcpSocket mapControllerSocket;
tcpListener.setBlocking(false);
if (tcpListener.listen(MAP_CONTROLLER_COMMUNICATION_PORT) != Socket::Status::Done)
{
MessageBoxA(window.getSystemHandle(), "Cannot attach to Map Controller, check if port 40235 is not blocked or already in use.\r\n\r\nTerminating...", MAP_VIEWER_WINDOW_TITLE.c_str(), MB_ICONERROR);
return EXIT_FAILURE;
}
time_t startTime = time(NULL);
while (startTime + 10 >= time(NULL))
{
if (tcpListener.accept(mapControllerSocket) == Socket::Status::Done)
break;
}
if (mapControllerSocket.getRemoteAddress().toInteger() == 0)
{
MessageBoxA(window.getSystemHandle(), "Cannot attach to Map Controller, try to restart the program.\r\n\r\nTerminating...", MAP_VIEWER_WINDOW_TITLE.c_str(), MB_ICONERROR);
return EXIT_FAILURE;
}
Packet packet;
mapControllerSocket.receive(packet);
// Stuff...
It stays blocked (no exception, just waiting) at line mapControllerSocket.receive(packet);
However, when I replace the last 2 lines with:
byte *buffer = new byte[100];
size_t rcvLen = 0;
mapControllerSocket.receive(reinterpret_cast<void*>(buffer), 100, rcvLen);
It works fine! I receive my data as expected.
I'd really like to use the Packet class so... do you know what's the problem?
The client is written in C# :
using System.Net.Sockets;namespace Control
.Communication{ class MapViewer
{ private Socket _mapViewerSocket
; public MapViewer
() { _mapViewerSocket
= new Socket
(AddressFamily
.InterNetwork, SocketType
.Stream, ProtocolType
.Tcp); } public bool Attach
(int communicationPort
) { try { _mapViewerSocket
.Connect("127.0.0.1", communicationPort
); } catch { return false; } return true; } public bool SendByte
(byte val
) { try { _mapViewerSocket
.Send(new byte[] {val
}); } catch { return false; } return true; } }}
And then :
private void listBox1_SelectedIndexChanged
(object sender, EventArgs e
){ _mapViewer
= new MapViewer
(); if (!_mapViewer
.Attach(MapViewerCommunicationPort
)) // 40235 { MessageBox
.Show("Cannot attach to Map Viewer, try to restart the program.\r\n\r\nTerminating...",
this.Text, MessageBoxButtons
.OK, MessageBoxIcon
.Error); Application
.Exit(); } _mapViewer
.SendByte((byte)1);}
It's a little bit messy but it's just a test project.
The server (C++/SFML) :
int main(int argc, char *argv[])
{
const string MAP_VIEWER_WINDOW_TITLE = "Map Viewer";
const int MAP_CONTROLLER_COMMUNICATION_PORT = 40235;
TcpListener tcpListener;
TcpSocket mapControllerSocket;
tcpListener.setBlocking(false);
RenderWindow window(VideoMode(1200, 800, 32), MAP_VIEWER_WINDOW_TITLE, Style::Close);
window.setFramerateLimit(60);
/* Connect to Map Controller */
if (tcpListener.listen(MAP_CONTROLLER_COMMUNICATION_PORT) != Socket::Status::Done)
{
MessageBoxA(window.getSystemHandle(), "Cannot attach to Map Controller, check if port 40235 is not blocked or already in use.\r\n\r\nTerminating...", MAP_VIEWER_WINDOW_TITLE.c_str(), MB_ICONERROR);
return EXIT_FAILURE;
}
time_t startTime = time(NULL);
while (startTime + 10 >= time(NULL))
{
if (tcpListener.accept(mapControllerSocket) == Socket::Status::Done)
break;
}
if (mapControllerSocket.getRemoteAddress().toInteger() == 0)
{
MessageBoxA(window.getSystemHandle(), "Cannot attach to Map Controller, try to restart the program.\r\n\r\nTerminating...", MAP_VIEWER_WINDOW_TITLE.c_str(), MB_ICONERROR);
return EXIT_FAILURE;
}
Packet packet;
mapControllerSocket.receive(packet);
while (window.isOpen())
{
...
}
return EXIT_SUCCESS;
}
I think I have the same problem (french): http://fr.sfml-dev.org/forums/index.php?topic=8803.0
Actually, it seems the socket receives the data, but won't copy them to a Packet:
(http://img4.hostingpics.net/pics/254433debuggerscreenshot.png)
I actually sent 4 bytes from my C# client.