// JP opened flex table

Click to See Complete Forum and Search --> : MFC Socket Send doesnt work ???


yoni1993
December 1st, 2007, 07:10 AM
i didnt worked on socket for about 5 months and now i tried to build a server and client but i mistook something on the base:

I tried to send a byte to the server but the server act like that is nothing received;

Client:

SocketA = new Socket();
SocketA->Create();
SocketA->Connect("127.0.0.1", 2157);
byte f = 0x01;
SocketA->Send(&f, 1);


Server Socket Class:

void Socket::OnReceive(int nErrorCode)
{
byte g;
MessageBox(hwnd, "OnReceive", "Info" , MB_OK);
Receive(&g, 1);
::SendMessage(hwnd, WM_RECV, 0, 0);

CSocket::OnReceive(nErrorCode);
}

void Socket::OnAccept(int nErrorCode)
{
MessageBox(hwnd, "Accepted", "Info" , MB_OK);
CSocket::OnAccept(nErrorCode);
}


Server Listen:

SocketA.hwnd = GetSafeHwnd();
SocketA.Create(2157);
SocketA.Listen();


So, when im connecting to the server, i get a messagebox about accept but for OnReceive nothing happen !

Why ?

thank you for any help.

VictorN
December 3rd, 2007, 05:51 AM
1. What is "Socket"? Where and how it is declared?
2. Why are you sure all these methods will succeed?:SocketA->Create();
SocketA->Connect("127.0.0.1", 2157);
...
SocketA->Send(&f, 1);

Skizmo
December 3rd, 2007, 06:20 AM
What does this do ??
SocketA.hwnd = GetSafeHwnd();
I use the CSocket too, but as far as I know, the CSocket creates it's own 'hidden' window for the messagepump.

Edders
December 3rd, 2007, 08:26 AM
You can use a packet sniffer like Ethereal to see if the packet is actually sent or not. This can help you in determining which side you need to concentrate on; the sending or the receiving side of the communication.

Since you are using address 127.0.0.1 I presume that sender and receiver are running on the same machine?

MikeAThon
December 3rd, 2007, 11:13 AM
On the client side, put some error-checking into your code and you will see what's happening.

(Actually, put error-checking into the server too.)

Basically, and assuming you have non-blocking async sockets, the client side fails to Connect() immediately, so it returns with an error. The immediately subsequent call to send() thus also fails, since the socket is not yet connected. Meanwhile, the connection attempt eventually succeeds, which is why the server's OnAccept() handler is called. But it's already too late for the client.

A few things: first, for simpler coding on the client side, convert to blocking socket (ioctrlsocket), or use CSocket. If you maintain the blocking nature of the socket (which will result in a better program, and is recommended), then you must also include OnXxx handlers for the client, just like the server.

Second, never use MessageBox while debugging socket code. Messagebox (or the display of any modal dialog) interferes with the message loop, and which is fundamental to correct operation of the socket's asynchronous handlers. Using messageBox for debugging while always come back to bite you, because of this. Use TRACE instead.

Mike

Marc G
December 3rd, 2007, 12:13 PM
[ moved thread ]

//JP added flex table