block a socket

Mar 17, 2009 at 2:47pm
Hi everybody,

I have a TCP server application which receives blocks of dummy data. The first time I use the application, the socket is self blocked before the accept function, that is, it waits for the incoming first lock of data packets, when there are no more data I thought it would be block at the begining of the accept function waiting for another blocks of packets (I have programmed it in a loop) but I doesn't work so. Thus I want to block the socket on my self. Do you know any options for that?I read about the O_NONBLOCK flag, but I don't know if I can change it from Visual Studio. Can you help me?

Thank very much you for your help
Mar 17, 2009 at 5:54pm
on the accept call what you can do is check the socket. if this is the socket you want you can continue with the rest of the code or else you can put a continue statement which will again start the while loop and block on accept for the next socket request.
I hope this is what you want?
Mar 17, 2009 at 5:56pm
something like this:

while(1)
{
socket sock = accept();
if(sock is not needed)
continue;
else
execute the rest of the code
}//while
This is what i've understood from your problem!!
Mar 18, 2009 at 7:50am
Hi again,

The code is:

while (1)
{
AcceptSocket = SOCKET_ERROR;
AcceptSocket = accept(socketInTCP, NULL, NULL);
if (AcceptSocket == INVALID_SOCKET){
printf("Server: WSA:\n",WSAGetLastError());
}else{
printf("Server: Client Connected!\n");
ofstream outfile(outputfile);
//Receive Packages from Client
while(1){
bytesRecv = recv(AcceptSocket, recvbuf, sizeof(recvbuf), 0);
if (bytesRecv <= 0){//Client hat Verbindung beendet
cout << "Nichts empfangen!" << endl;
break;
}else{

outfile.write (recvbuf, bytesRecv);
}
memset(recvbuf,0,sizeof(recvbuf));
}
//Close OutputFile
outfile.close();

}
}

After the first block data, I thought the socket would be blocked in the accept function in order to wait to the next block but it doesn't work so. I read that I can use the ioctlsocket function with the FIONBIO flag. I did it after the accept function to give the socket this feature but it doesn't work.

Can you help me? do you have any ideas?

thank you very much
Mar 18, 2009 at 1:47pm
see.. you dont have to accept again and again for each block of data.
accept will be called once for each connection(for every client) then you only have to use a combination of recv-send only.
client will send you recv and then again client will send and you recv. the process will keep on running till the client disconnects.
its something like this:

client called connect---
server accept--- (only once for each client)
client send data
server recv and block for next data
client send again
server now recv again
.
.
.
client disconnect.
server disconnect.

got it??

Topic archived. No new replies allowed.