-
Sockets switching data?
Hello, I've made a simple program that connects to an IRC server. I'm now working on the ident server bit, that listens on port 113. I've come across something rather strange though.
I have two sockets, one that connects to the server on port 6667 and another that listens on port 113. When the socket on port 113 recieves data, it has the correct data at the beginning, but after that it seems to have data from the other socket also..
I'm using asynchronous sockets, here's the problem code:
Code:
case WM_IDENTSOCK:
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
{
if (WSAGETSELECTERROR(lParam) != 0)
{
return(FALSE);
}
IdentSocket= accept(wParam,0,0);
return 0 ;
}
case FD_READ:
{
char iData[1000];
if (WSAGETSELECTERROR(lParam) != 0)
{
int error = WSAGetLastError();
wsprintf(iData,"%d",error);
MessageBox(0,iData,"",0);
return false;
}
int bytes = recv(IdentSocket, iData, sizeof(iData)-1, 0);
if(bytes == SOCKET_ERROR)MessageBox(hwnd,"Error in FD_READ, bytes read == SOCKET_ERROR","Error",0);
else{
MessageBox(0,iData,"",0); //this string seems to have data from the other socket after the correct data
iData[bytes] = '\0';
MessageBox(0,iData,"",0); //this data is the correct data
}
return 0;
}
case FD_CLOSE:
{
if (WSAGETSELECTERROR(lParam) != 0)
return(FALSE);
MessageBox(0,"Connection Closed","Ident Server",0);
shutdown(IdentSocket,0);
WSACleanup();
return 0 ;
}
}
return 0;
}
case WM_CLIENTSOCK:
{
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ:
{
char data[1000];
if (WSAGETSELECTERROR(lParam) != 0)
{
int error = WSAGetLastError();
wsprintf(data,"%d",error);
MessageBox(0,data,"",0);
return false;
}
int bytes = recv(wParam, data, sizeof(data)-1, 0);
if(bytes == SOCKET_ERROR)MessageBox(hwnd,"Error in FD_READ, bytes read == SOCKET_ERROR","Error",0);
else{
data[bytes] = '\0';
if(string(data).substr(0,4) == "PING")
{
strncpy(data,"PONG",4);
AddData("Ping?");
bytes = send(wParam,data, strlen(data),0);
if(bytes == SOCKET_ERROR)
{
int error = WSAGetLastError();
wsprintf(data,"%d",error);
MessageBox(0,data,"send() Error",0);
}
else AddData(" Pong!\r\n");
}
else AddData(data);
}
return 0;
}
case FD_CLOSE:
{
if (!WSAGETSELECTERROR(lParam))
return(FALSE);
MessageBox(0,"Connection Closed","",0);
shutdown(wParam,0);
WSACleanup();
return 0 ;
}
}
}
These are my two messages that handle each socket. Any explanation would be good.
Thank you.
-
MessageBox(0,iData,"",0); //this string seems to have data from the other socket after the correct data
iData[bytes] = '\0';
MessageBox(0,iData,"",0); //this data is the correct data
That's because recv() doesn't terminate your buffer with a \0 - you have to do that (which you do).
Just delete the first MessageBox() call, and you're good :)
-
But what confuses me is where has that other data come from. I'm not using the same buffer or socket. where is that data coming from?
-
> char iData[1000];
> char data[1000];
Since these are in different blocks of code (non-overlapping scope), my guess is your compiler assigned them the same part of stack memory.
So writing data into one array will appear as uninitialised data in the other.
-
Thanks Salem. If when I declare iData, I do char iData[1000] = ""; It solves the problem.
Thank you for your explanation.
-
No, iData[bytes] = '\0'; solves the problem.
iData[1000] = ""; just covers up the problem by filling the array with \0, but you still need the iData[bytes] = '\0'; to be correct.