Server Client Problem

Hello
I am developing a client server problem and facing a problem.
I am running multiple clients from a single machine.Here, server accepts data from only one client and does not receive data from other clients while they are connected and sending data.
I am puzzled here why server is not receiving data from other clients.Can anybody help?

regards
Nipun
Usually the server forks a new process or thread for each client connection. Is your server doing that?
You might want to post some code.
However, let me take a guess simply from your description: you're not forking another process/generating another thread for an incoming client.
Here is my server code:

WORD m_wVersionRequested;
WSADATA m_wsaData;
m_wVersionRequested = MAKEWORD( 2, 2 );

WSAStartup(m_wVersionRequested, &m_wsaData);

SOCKET m_socketAccept = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP );
if (m_socketAccept == INVALID_SOCKET)
return false;

DWORD m_dwInetAddrServer = inet_addr("127.0.0.1");
if(m_dwInetAddrServer == INADDR_NONE)
return 0;
WORD m_wPortNoServer = 2001;

SOCKADDR_IN oSockAddr;
::ZeroMemory(&oSockAddr, sizeof(SOCKADDR_IN));
oSockAddr.sin_family = AF_INET;
oSockAddr.sin_port = htons(m_wPortNoServer);
oSockAddr.sin_addr.s_addr = m_dwInetAddrServer;

int ret = bind(m_socketAccept, (const sockaddr*) &oSockAddr, sizeof(SOCKADDR_IN));

int error;
if (ret == SOCKET_ERROR)
{
closesocket(m_socketAccept);
error = WSAGetLastError();
return false;
}

error = listen(m_socketAccept, 1);
DWORD temp = GetLastError();
if (error == SOCKET_ERROR)
return false;


SOCKET socket;
int clientAddressLen = sizeof(struct sockaddr_in);
struct sockaddr_in *clientAddress = NULL;
while(1)
{
//socket = accept(m_socketAccept, (struct sockaddr *)clientAddress, &clientAddressLen);
socket = accept(m_socketAccept, NULL, NULL);
if( socket == INVALID_SOCKET )
{
continue;//or exit from thread
}
_beginthread(ClientServerCommunication, 0, (void *)&socket);
}

For each accept call, a thread is created to receive the data.
What I found is since I am running all the clients on same machine, same socket number is used for all the clients and if I disconnect one, all client gets disconnected.
This is because threads (and forked processes for that matter) inherit the file handles of their parents. The parent process should close the socket from the accept() call, and the child process should close the listen socket.
read this guide.
"beej guide" pdf.
www.silicontao.com/ProgrammingGuide/other/beejnet.pdf

I take it this is a Windows app posted in the Unix section.

You're passing the address of the socket to the thread, but then overwritting that value each time accept() returns. So the socket used in the thread changes without the thread realising it. Why not pass it by value?

You shoud declare and initialise socket, clientAddressLen and clientAddres within the while loop.
Topic archived. No new replies allowed.