multiple concurrent socket connections
i've found quite a bit of articles on sockets, particularly on msdn, but one thing i haven't been able to ascertain, is how multiple concurrent connections are implemented on the server.
i've set up my own one-to-one client/server socket implementation and it works. however, if i close the client application, restart it and attempt to connect, the accept function fails with a WSAEINVAL error. According to msdn, WSAEINVAL - means the listen function was not invoked prior to accept.
this is not true though - the listen function was invoked prior to accept, which is how the first connection attempt was successfull.
any high-level thoughts as to why i might be having this problem? if needs be, i'll post some code, but i don't think it's much different that any other socket code out there..
Re: multiple concurrent socket connections
Actually, it'd be good if you could show us the server and client code.
Re: multiple concurrent socket connections
alright, i stripped this down to the bare essentials, to make getting this up and running as easy as possible if you want to try it out:
Code:
SOCKET socketx;
#define WM_SOCKET WM_USER+10
long CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_CREATE:{
SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1), &wsaData);
socketx = socket(AF_INET,SOCK_STREAM,0);
WSAAsyncSelect(socketx,hwnd,WM_SOCKET,FD_CONNECT|FD_READ|FD_CLOSE|FD_ACCEPT);
unsigned short port = 7;
struct sockaddr_in addy;
addy.sin_family = AF_INET;
addy.sin_port = htons(port);
addy.sin_addr.s_addr = INADDR_ANY;
bind(socketx,(struct sockaddr*)&addy,sizeof(addy));
listen(socketx,SOMAXCONN);
}
break;
case WM_SOCKET:{
switch(LOWORD(lParam)){
case FD_ACCEPT: {
struct sockaddr cli_addr;
int clilen = sizeof(cli_addr);
socketx = accept(socketx,(struct sockaddr*)&cli_addr,&clilen);
if(socketx==INVALID_SOCKET){
MessageBox(NULL,"Invalid Socket","",MB_OK);
}
}
break;
case FD_READ:{
char data[1024];
recv(socketx,data,1024,0);
send(socketx,"server: hello",strlen("server: hello"),0);
}
break;
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
obviously, this is a win32 app and i'm using the wMsg param in WSAAsyncSelect to pump socket message through my callback proc.
to repro the error, connect to this app, note the "server: hello" response, close the client app, then restart it and accept will fail as mentioned in my previous post.