flat assembler
Message board for the users of flat assembler.

Index > Windows > Winsock - blocking question

Author
Thread Post new topic Reply to topic
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 14 Jul 2007, 21:24
Hello,
I am programming a small server/client pair. The client freezes (blocks) when it does not receive the proper data from the server.
Is there a way of preventing this, like setting error timeouts or so?
Thank you in advance.
Post 14 Jul 2007, 21:24
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 15 Jul 2007, 08:19
which API do you use for recieving data in client?
Post 15 Jul 2007, 08:19
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Furby



Joined: 01 May 2007
Posts: 74
Location: Kraków, Poland
Furby 15 Jul 2007, 08:33
somebody deleted someones post and i couldn't reply... so

read this http://beej.us/guide/bgnet/

its in C but you should get the information you want.
Post 15 Jul 2007, 08:33
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 16 Jul 2007, 08:09
@vid: I'm using the usual recv, why? Is there any other way to do it?

@Furby: thanks for the link, but I already have these in Microsoft's Winsock API reference .hlp file.
Post 16 Jul 2007, 08:09
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 16 Jul 2007, 10:34
ManOfSteel: you can use overlapped file I/O to read socket, or you can perform reading in separate thread, or you can use select() to see which handles contain some data to read.
Post 16 Jul 2007, 10:34
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov 17 Jul 2007, 20:35
Post 17 Jul 2007, 20:35
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 19 Jul 2007, 14:34
Hello,

@vid:
I'm sorry for not replying earlier, I've been quite busy lately.
Even if you use threads, you should still use recv, don't you?
In fact, I've already tried using threads but something seemed wrong: the thread terminated before it had finished its work. I had to add a sleep API to slow it down. Is there any better way to do it?
Code:
invoke CreateThread,0,0,RecvProc,0,0,Temp
invoke CloseHandle,eax
invoke Sleep,5000
jmp finish
...
finish:
invoke closesocket,[hSocket]
invoke WSACleanup
...
proc RecvProc
invoke recv,[hSocket],Buffer,10,0
or eax,eax
jz disconnected
cmp eax,-1
jne success
invoke MessageBox,0,'No response from server','Client',0
jmp finish
endp
    

I usually don't use threads much, so please bear with me.

Also, what about select? I've read about it, but didn't understand much. Could you give me some pseudo-code instructions on how to use it?


@Nikolay Petrov: thanks for the link. It seems interesting, I've downloaded everything.
Post 19 Jul 2007, 14:34
View user's profile Send private message Reply with quote
Gizmo



Joined: 19 Jul 2007
Posts: 25
Gizmo 19 Jul 2007, 22:06
to use nonblocking berkely sockets you must use the select function to see if there is data to recv() yet and then recv() it if it says there is

in c this is
Code:
//defines

typedef struct timeval {  long tv_sec;  long tv_usec;
} timeval;

typedef struct fd_set {  u_int fd_count;  SOCKET fd_array[FD_SETSIZE];
} fd_set;

int select(
  int nfds,
  fd_set* readfds,
  fd_set* writefds,
  fd_set* exceptfds,
  const struct timeval* timeout
);


//how to use it

timeval ReadWait;
ReadWait.tv_sec=0;
ReadWait.tv_usec=0;   //no wait and no blocking

fd_set ReadSet;
ReadSet.fd_count=1;
ReadSet.fd_array[0]=ReadSocket;     //socket(s) to listen to

if(select(0,&ReadSet,NULL,NULL,&ReadWait)>0)
{
recv(ReadSocket,buffer, len,0);
}
    
Post 19 Jul 2007, 22:06
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 20 Jul 2007, 18:43
@Gizmo:
Thank you. I tried it, but it seems to work only when I set tv_sec to at least 1 second.
By the way, 1)when tv_usec is used, is it added to tv_sec? 2)Also, what's the purpose of having microsecond-precision network operations?
Post 20 Jul 2007, 18:43
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.