Reputation: 1883
I have char* MESSAGE = new char[256];
and char* DISCONNECT = new char[256];
however when I use winsock to send them from the client and recieve them in the server (the server has the same char names) for some reason char* MESSAGE
intercepts char* DISCONNECT
any help on why this is would be great!.
Cient:
private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
char* Disconnect = new char[256];
ZeroMemory(Disconnect, sizeof(Disconnect));
Disconnect = "DC";
send(sConnect, "DC", 256, NULL);
}
private: System::Void txtMessage_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if(e->KeyCode == Keys::Enter && txtMessage->Text != "")
{
char* MESSAGE = new char[sizeof(txtMessage->Text->Length)];
ZeroMemory(MESSAGE, sizeof(MESSAGE));
string strMESSAGE = "";
MarshalString(txtMessage->Text, strMESSAGE);
send(sConnect, strMESSAGE.c_str(), strMESSAGE.length(), NULL);
txtMessage->Clear();
}
}
Server:
int RecieveThread()
{
ZeroMemory(MESSAGE, sizeof(MESSAGE));
for (;; Sleep(50))
{
if(recv(sConnect, MESSAGE, 256, NULL) != SOCKET_ERROR)
{
printf("<%s:> %s\n", NAME, MESSAGE);
}
}
return 0;
}
int DisconnectThread()
{
ZeroMemory(Disconnect, sizeof(Disconnect));
for(;; Sleep(50))
{
if(recv(sConnect, Disconnect, 256, NULL) != SOCKET_ERROR)
{
if (Disconnect == "DC")
{
printf("has disconnected.");
}
}
}
return 0;
}
Upvotes: 1
Views: 592
Reputation: 182883
Among the other errors mentioned in the comments, your use of recv
is a problem waiting to happen. The recv
function reads up to the number of bytes you specify, not exactly that number. If you get fewer bytes, you need to call recv
again.
Also, never throw away the return value from recv
. It's the only way to know how many bytes of data you actually got.
The TCP layer has no idea that you consider 256 bytes an application-layer message. It does not glue those bytes together. Only your code knows that, so it's your code's responsibility to put the messages back together when they are received.
Upvotes: 2