TCP Socket Problem

Hello,

I have client/server app in which I am getting random timeouts. The client will sometimes freeze for a second or two then return a timeout. I have this code in which it waits for the server to send either send a response or return a timeout:

[code]//start time out clock
time1= Ticks/60

While TCPSocket1.IsConnected

TCPSocket1.Poll

if TCPSocket1.BytesAvailable > 0 then
SolverData = TCPSocket1.ReadAll
SolverData = DecodeBase64(SolverData)
TCPSocket1.Close
end

time2=Ticks/60
speed = time2-time1

if speed > 10 then
SolverData = “TIMEOUT”
TCPSocket1.Close
end

wend[/code]

Since this while loop is not in a thread I cannot sleep the app to yield to other threads. Using the Poll method was the only solution I could find. What would be the best way to do this? Any help would be appreciated.

Thanks

Polling sockets in a tight loop like this is going to make your hair turn grey prematurely.

Is there any reason you cannot use the DataAvailable event in TCPSocket1?

Hi Kimball. Thanks for the reply. I just removed all that and I am currently trying a DataAvailable event as you suggested. So far it is not working at all…

Where do you create the socket? You need to be aware of scope with sockets…

If you put a breakpoint in the DataAvailable event, does it ever fire?

Hey. Yeah DataAvailable never fires now. I have it set as Public. I have never had problems with DataAvailable before. I just added the TCPSocket to my project like normal.

I always create a subclass of TCPSocket so I can a) reuse the socket wherever I need to, and b) implement all the events in one place so that all my instances are consistent.

As for why yours is not working: can you share some code? How are you connecting the socket in the first place? How are you sending a request?

Here is all the basic code:

[code]//Connect to Server
Window1.TCPSocket1.Connect

// while the socket isn’t connected
While Not Window1.TCPSocket1.IsConnected
If Window1.TCPSocket1.LastErrorCode <> 0 Then exit
Window1.TCPSocket1.Poll
Wend

//Encrypt Message with some random chars
TheMessage = str(ticks)+“LOG:”+UserName+"*"+Password
GameBlock = EncodeBase64(TheMessage)

If Window1.TCPSocket1.IsConnected then
Window1.TCPSocket1.Write GameBlock
else
MsgBox “Sorry. Server DNR”
end

//start time out clock
time1= Ticks/60

While Window1.TCPSocket1.IsConnected

//wait for dataavailable to fire and return response
if app.GameString <> “” then
SolverData = app.GameString
Window1.TCPSocket1.Close
end

time2=Ticks/60
speed = time2-time1

if speed > 10 then
SolverData = “TIMEOUT”
Window1.TCPSocket1.Close
end
wend[/code]

In the DataAvailable event:

[code]
Dim Data as string

Data = me.ReadAll
Data = DecodeBase64(Data)
if Data <> “” then app.GameString = Data else app.GameString = “”[/code]

Ok, off the top of my head:

Get rid of the tight while loop that is polling incessantly while waiting for isConnected to be true. Just comment that whole loop.

Ok, so you are trying to use the socket in a synchronous manner… which is not the way it is intended to be used. DataAvailable should be where you handle whatever you need to do with the data.

But the bigger question I have is: What are you connecting to? Are you certain you should be using a TCP socket rather than the more generic HTTPSocket or HTTPSecureSocket? Are you certain that the server is listening? Are you certain the server should be providing a response? Do you have access to the server to monitor the incoming connection from your client?

Finally: have you checked out the socket examples that ship with Xojo?

The biggest thing to note: Sockets should be used asynchronously. You are trying to use them synchronously, and you’re gonna have a bad time.