Guidance
指路人
g.yi.org
Guidance Forums / Rapid-Q Basic / socket questions.

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. socket questions.
#592
Posted by: 2002-10-23 19:33:45
Hi..

I'm now in the stage where I need to implement and understand how qsocket works.

The app I'm writing (non comersial) is going to handle a lot of clients and will act as a client itself towards other servers. I'm setting up timers with state loops so that im not hogging the cpu. But this will only work if the commands im using are non blocking.

The questions.

is SockNum = Socket.Connect(Edit1.Text, 5000) blocking the code execution? (where socket = qsocket)

I've been looking at RSOCKET.INC it's seams like something I need. But I dont know if the already built in socket functions are non blocking.

The other thing I would like to ask is:

What is the best way of transfering data. Well let me get more specific. If i have a timer function every second that sends 32Kb/s data over a socket my max speed is 32Kb/s and this is not good enough since i need up to gigabit speeds.

And what happens if a client is sending data then the transfer is interrupted is the server waiting and waiting and waiting.....

will

Whatever=socket.read(socketnumber,data,number_o_bytes) hang the code if not all data is getting trough?

Thanx for any answer.. (If u know a pointer to code handeling multipple connections and/or servers please let me know)

/A
Message2. Re: socket questions.
#594
Posted by: guidance 2002-10-24 07:37:19
Does RSocket (non-block mode) work well?
Message3. Re: socket questions.
#595
Posted by: 2002-10-24 12:30:22
Hi, I wrote Rsocket.  Your many questions all have answers in RSocket.zip.  Be sure to get the latest version (http://www.angelfire.com/space/netcensus/), in case Guidance has not posted it on his site.  Your questions are not displayed in this reply page so I will try to answer what I remember of your questions.  You can also email me directly if I miss (or forgot) something.
1. RSocket allows you to have multiple sockets and connections open at the same time if you want because it allows you to create a socket independent of the .Connect function in Qsocket, which is replaced in any case with a different .Connect function in RSocket.
2. You may make any socket blocking or non-blocking at any time.  If a socket is non-blocking, any other socket call will return immediately (e.g., for read, write) with success or error (error=the socket is not yet ready).  This may deal with the questions about timing and speed.
3. In the RandScan and WebHogs programs on my site (above), I use a loop through each "active" socket spaced by a SLEEP 0.40 so that CPU time is not hogged, as you say.  This is much simpler and easier to program than setting up upteen timers and events triggered by the timers.  The method just described is called polling and is considered to be less sophistocated by some, but it works.  You should probably get your program running with the simpler design and then add event-triggering timers if you really need to.  My method, you don't have to write the event handlers (saves time).  If you have an array of sockets like socket(n) and time(n) and state(n) or whatever you need, you can as you loop through the sockets simply select case state(n) to determine what to do next with socket n.  Timer - time(n) always gives you how much time has passed since you last did something with that socket to control program flow depending on state(n).
4. You definitely cannot do what you plan with blocking sockets (QSocket), because .Connect and .Read, etc will block until a system determined timeout, etc.  So look at the example code in RSocket.zip and see if you have more questions or ones I didn't address.  Good luck!
Message4. Re: socket questions.
#596
Posted by: 2002-10-24 12:35:54
Whoops!  I correct my reply.  It is SLEEP 0.040 or just 40 milliseconds not 0.4 seconds.  You can used any CPU non-hogging value you find works in your application.  BTW, this message board looks great!!
Take care,
Message5. Re: socket questions.
#597
Posted by: 2002-10-24 13:04:26
Thank you Guidance for a one line question!  I can remember that.  The RandScan and WebHogs applications on my site which can be put on your site too, if you want, are extremely stable and reliable and use non-blocking socket programming in RSocket.zip.  I have run even multiple instances of these programs 24/7 for over a week without any failure or hangup whatsoever.  Internet applications need to be stable and reliable.  This proves that non-blocking sockets in RSocket meet that criterion. 
While we are talking, let me explain what non-blocking is, for those who are new to socket programming.  Think of a socket connecting you to another computer is like a file stream.  Blocking (QSocket) means that your program will not operate at all while the process of establishing a connection is taking place.  Most of the time, this takes less than one second, so it may not be an issue.  But if the connection is not going through, the timeout for this failure may be about a minute on many systems.  If you are trying to connect to many computers at about the same time, you cannot afford to delay control and decisions by your program for a minute.  A remote computer on another socket may simply disconnect if you are inactive for that long.  In short, for multiple socket applications, using blocking sockets is unfeasible (QSocket) if reliability is important.
In non-blocking mode, you (1) request a connection on socket(n) and set time(n)=Timer, (2) you go about your business in your program doing whatever else you need to do while the connection process is underway -- maybe you want to request other connections on other sockets or do something with sockets that are already connected, (3) when you feel like it, you come back and check if socket(n) is connected (.IsConnected function), (4) If yes, do your thing; If no, decide if you want to wait more time comparing that with Timer - time(n).  If too much time has passed, then .Close the socket, create another socket and try another connection or whatever.
Note:  don't forget to roll over your time(n) values at midnight.  How?
If Timer < time(n) THEN time(n)=0 or time(n)=Timer or however you want to roll it over at midnight.
Note2: As you loop through sockets n, be sure there are DoEvents in there to give the system a chance to process all the requests you are queing up.  In GUI apps, they will not be stable unless there is a DoEvents after each and every .AddItems to ListBoxes and such.
Message6. Re: socket questions.
#612
Posted by: 2002-10-25 20:12:49
Thanx for all valuable answers... I'll talk to you via e-mail since there are some outstanding questions that still keeps me puzzled.

/A
Message7. Re: socket questions.
#632
Posted by: kohaistyle 2002-11-03 01:22:09
Woaowww ..

Non blocking sockets ! Just what i needed ...

Thx !
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Fri 2024-3-29  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0