Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Writing to a socket (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Syncing tables with no unique identifiers...?
Next thread: Thread and Serial.DataAvailable


Re: File Suggestions?   -   Rubber Chicken Software Co.
  Writing to a socket   -   Bart Silverstrim
   Re: Writing to a socket   -   Joe Strout
    Re: Writing to a socket   -   Bart Silverstrim
     Re: Writing to a socket   -   Joe Strout
      Re: Writing to a socket   -   Bart Silverstrim
   Re: Writing to a socket   -   Walter Purvis
    Re: Writing to a socket   -   Bart Silverstrim
   Re: Writing to a socket   -   James Sentman
   Re: Writing to a socket   -   Charles Yeomans
    Re: Writing to a socket   -   Bart Silverstrim
     Re: Writing to a socket   -   Joe Strout
   Re: Writing to a socket   -   James Sentman
    Re: Writing to a socket   -   Bart Silverstrim
   Re: Writing to a socket   -   Charles Yeomans

Writing to a socket
Date: 23.04.09 19:44 (Thu, 23 Apr 2009 14:44:11 -0400)
From: Bart Silverstrim
Experimenting with some simple socket operations on my Linux system...

I set up netcat to listen to localhost on port 2555.

In the applicaton I set up a lot of information from the reference, but
the reference does everything "client side", giving status to a text
box. It doesn't actually post any data to the socket connection.

Here's a quick snip where I'm trying to write:
*********
' The previous loop is broken if we're connected.
If TCPSocket1.IsConnected then

txtStatus.text = txtStatus.Text + chr(13) + "Socket Connected in "
+ Str(stop - start) + " ticks"
tcpsocket1.Write("Hello")

Else

' The socket broke out of the loop due to an error
txtStatus.text = txtStatus.Text + chr(13) + "Socket failed to
connect. It took " + Str(stop - start) + _
" ticks to figure that out"

End If

' Close the socket
TCPSocket1.Close

' Tell user we're done with the connection
txtStatus.text = txtStatus.Text + chr(13)+ "Socket closed"
**************

Compiles, but doesn't work. I'm going through the reference and a couple
books on RB, but I don't see examples how to do this, so I'm probably
being dense but what am I missing here?

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 23.04.09 22:06 (Thu, 23 Apr 2009 15:06:38 -0600)
From: Joe Strout
Bart Silverstrim wrote:

> Compiles, but doesn't work. I'm going through the reference and a couple
> books on RB, but I don't see examples how to do this, so I'm probably
> being dense but what am I missing here?

Where are you telling the socket to connect?

Best,
- Joe

Re: Writing to a socket
Date: 23.04.09 22:37 (Thu, 23 Apr 2009 17:37:36 -0400)
From: Bart Silverstrim


Joe Strout wrote:
> Bart Silverstrim wrote:
>
>> Compiles, but doesn't work. I'm going through the reference and a
>> couple books on RB, but I don't see examples how to do this, so I'm
>> probably being dense but what am I missing here?
>
> Where are you telling the socket to connect?

Before the code I posted :-)
***********
' Create the connection
Dim start, stop as integer
dim TCPSocket1 as TCPSocket
TCPSocket1 = New TCPSocket

' Get address from text fields
TCPSocket1.Address = txtAddress.Text
TCPSocket1.Port = CDbl(txtPort.Text)

'Create some defaults if nothing's in the text boxes
if txtAddress.Text = "" then
tcpsocket1.Address = "localhost"
end if
if txtPort.Text = "" then
tcpsocket1.port = 2555
end if

' Make Connection
txtStatus.Text = "Beginning Synchronous Connection..."
tcpsocket1.Connect

' Checking to see connection status..
start = Ticks

' Is there an error while trying to connect?
While Not TCPSocket1.IsConnected
if TCPSocket1.LastErrorCode <> 0 Then
txtStatus.Text = txtStatus.Text + chr(13) + "Socket Error: " +
str(TCPSocket1.LastErrorCode)
exit
end if

' Poll the socket
TCPSocket1.Poll
wend

' This combined with start gets a count of how long it took to connect
stop = ticks

' The previous loop is broken if we're connected.
If TCPSocket1.IsConnected then

txtStatus.text = txtStatus.Text + chr(13) + "Socket Connected in "
+ Str(stop - start) + " ticks"
tcpsocket1.Write("Hello")

Else

' The socket broke out of the loop due to an error
txtStatus.text = txtStatus.Text + chr(13) + "Socket failed to
connect. It took " + Str(stop - start) + _
" ticks to figure that out"

End If

' Close the socket
TCPSocket1.Close

' Tell user we're done with the connection
txtStatus.text = txtStatus.Text + chr(13)+ "Socket closed"


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 04:22 (Thu, 23 Apr 2009 21:22:30 -0600)
From: Joe Strout
Bart Silverstrim wrote:

> ' Create the connection
> Dim start, stop as integer
> dim TCPSocket1 as TCPSocket
> TCPSocket1 = New TCPSocket

This might work but is rather unusual. More typical is to add a
TCPSocket instance to the window. That way, you can (among other
things) implement its events (such as Error) and have a clue what's
going on. (You could also do that with a subclass, of course.)

That synchronous sitting-in-a-loop-polling stuff is pretty unusual too,
though not as much so; I've done it on occasion and it should work.

Other than that, I can't see anything wrong with your code. Try
implementing the events to see what's happening.

Best,
- Joe

Re: Writing to a socket
Date: 24.04.09 12:03 (Fri, 24 Apr 2009 07:03:43 -0400)
From: Bart Silverstrim


Joe Strout wrote:
> Bart Silverstrim wrote:
>
>> ' Create the connection
>> Dim start, stop as integer
>> dim TCPSocket1 as TCPSocket
>> TCPSocket1 = New TCPSocket
>
> This might work but is rather unusual. More typical is to add a
> TCPSocket instance to the window. That way, you can (among other
> things) implement its events (such as Error) and have a clue what's
> going on. (You could also do that with a subclass, of course.)
>
> That synchronous sitting-in-a-loop-polling stuff is pretty unusual too,
> though not as much so; I've done it on occasion and it should work.
>
> Other than that, I can't see anything wrong with your code. Try
> implementing the events to see what's happening.

What's interesting (and a little disheartening) is that about 98% of the
code is straight from the tcpsocket example in the language reference
from the help menu, and experienced RB users appear to be kind of
stumped with what's happening in a simple example (or so I thought)...
:-( At least I don't feel quite so stupid now.

So the tcpsocket1.write("string") should be working here? I chose to
initially test with netcat (netcat localhost -l -p 2555) because I know
that I can telnet into port 2555 and any text I send will appear. Maybe
I need to try sending a carriage return or something at the end of the
string, something stuck in a buffer?...

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 23.04.09 23:43 (Thu, 23 Apr 2009 18:43:53 -0400)
From: Walter Purvis
As a quick guess, that While Not TCPSocket1.IsConnected loop has the smell
of a likely culprit. Have you tried commenting that part out?
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 12:30 (Fri, 24 Apr 2009 07:30:23 -0400)
From: Bart Silverstrim


Walter Purvis wrote:
> As a quick guess, that While Not TCPSocket1.IsConnected loop has the smell
> of a likely culprit. Have you tried commenting that part out?

No I didn't because I had taken it from the language reference. The next
thing I'm going to try is seeing if I need to insert some chr() for the
ascii code for linefeed/carriage return and see if it's something with
signaling netcat to flush out a buffer or something like that.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 13:11 (Fri, 24 Apr 2009 08:11:03 -0400)
From: James Sentman

On Apr 23, 2009, at 2:44 PM, Bart Silverstrim wrote:

> Here's a quick snip where I'm trying to write:
> *********
> ' The previous loop is broken if we're connected.
> If TCPSocket1.IsConnected then
>
> txtStatus.text = txtStatus.Text + chr(13) + "Socket Connected in
> " + Str(stop - start) + " ticks"
> tcpsocket1.Write("Hello")
>
> Else
>
> ' The socket broke out of the loop due to an error
> txtStatus.text = txtStatus.Text + chr(13) + "Socket failed to
> connect. It took " + Str(stop - start) + _
> " ticks to figure that out"
>
> End If
>
> ' Close the socket
> TCPSocket1.Close
>
> ' Tell user we're done with the connection
> txtStatus.text = txtStatus.Text + chr(13)+ "Socket closed"

I have done looping and polling on occasion as well and it does work
(but it not how you want to design a program if you can help it)

Perhaps it is actually necessary that you call tcpSocket1.poll during
your loop in order that it can make it's connection and send the data.
I have noticed sometimes that the data doesn't actually go anywhere
until you either return from your method and allow the main loop to
continue, or until you call poll on the socket..

It may also be that you're closing the socket while the data is still
in the buffer and so it doesn't get sent. A call to tcpSocket1.poll
before you close might let it get out. HAve you done enough debugging
to see if you're actually getting a connected event? I dont think you
will while looping without a poll..

But what you really want to do is subclass the tcpsocket, and add the
write to the connected event and not loop.

Thanks,
James

James Sentman http://sentman.com http://MacHomeAutomation.com



_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 14:11 (Fri, 24 Apr 2009 09:11:46 -0400)
From: Charles Yeomans

On Apr 24, 2009, at 7:03 AM, Bart Silverstrim wrote:

>
> Joe Strout wrote:
>> Bart Silverstrim wrote:
>>> ' Create the connection
>>> Dim start, stop as integer
>>> dim TCPSocket1 as TCPSocket
>>> TCPSocket1 = New TCPSocket
>> This might work but is rather unusual. More typical is to add a
>> TCPSocket instance to the window. That way, you can (among other
>> things) implement its events (such as Error) and have a clue what's
>> going on. (You could also do that with a subclass, of course.)
>> That synchronous sitting-in-a-loop-polling stuff is pretty unusual
>> too,
>> though not as much so; I've done it on occasion and it should work.
>> Other than that, I can't see anything wrong with your code. Try
>> implementing the events to see what's happening.
>
> What's interesting (and a little disheartening) is that about 98% of
> the code is straight from the tcpsocket example in the language
> reference from the help menu, and experienced RB users appear to be
> kind of stumped with what's happening in a simple example (or so I
> thought)... :-( At least I don't feel quite so stupid now.

I consider much of the LR example code to be not very good.

>
> So the tcpsocket1.write("string") should be working here? I chose to
> initially test with netcat (netcat localhost -l -p 2555) because I
> know that I can telnet into port 2555 and any text I send will
> appear. Maybe I need to try sending a carriage return or something
> at the end of the string, something stuck in a buffer?...
>

Try removing the Poll call, and instead write

do until TCPSocket1.IsConnected
if TCPSocket1.LastErrorCode = 0 then
app.DoEvents
else
txtStatus.Text = txtStatus.Text + chr(13) + "Socket Error: " +
str(TCPSocket1.LastErrorCode)
exit
end if
loop

Or perhaps you could describe what you want -- a simple example,
perhaps?

Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 14:27 (Fri, 24 Apr 2009 09:27:08 -0400)
From: Bart Silverstrim


Charles Yeomans wrote:
> Try removing the Poll call, and instead write
>
> do until TCPSocket1.IsConnected
> if TCPSocket1.LastErrorCode = 0 then
> app.DoEvents

This is okay in GUI applications?

> else
> txtStatus.Text = txtStatus.Text + chr(13) + "Socket Error: " +
> str(TCPSocket1.LastErrorCode)
> exit
> end if
> loop
>
> Or perhaps you could describe what you want -- a simple example, perhaps?

I was trying to create a type of messaging client that would connect to
a server and store data if the other user you're trying to get a message
to isn't available. Tons of steps still to do but the first step was to
get the client to connect and send data reliably, then work on the
server side that can store information to a file or database. I was
simulating a server using netcat at this point so I can see what is
being sent by the client but nothing appeared. At this point I was
thinking it's an issue with netcat where it's waiting for a linefeed or
carriage return character before dumping it's own buffer, but because it
was getting disconnected from the socket netcat just exits without
flushing anything to stdio.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 15:59 (Fri, 24 Apr 2009 08:59:29 -0600)
From: Joe Strout
Bart Silverstrim wrote:
> Charles Yeomans wrote:
>> Try removing the Poll call, and instead write
>>
>> do until TCPSocket1.IsConnected
>> if TCPSocket1.LastErrorCode = 0 then
>> app.DoEvents
>
> This is okay in GUI applications?

Though it's rare for me to disagree with Charles, I would say no, this
is not OK in a GUI app.

Instead I would encourage you to abandon your attempt to make this a
blocking method, and instead embrace the event-driven approach -- just
tell your socket to connect, return from your method, and send your data
in response to other events (such as Connected).

Best,
- Joe

Re: Writing to a socket
Date: 24.04.09 14:50 (Fri, 24 Apr 2009 09:50:51 -0400)
From: James Sentman

On Apr 23, 2009, at 2:44 PM, Bart Silverstrim wrote:

> tcpsocket1.Write("Hello")

So I downloaded and compiled and installed netcat. It is as I thought
I remembered, you cannot send text and then immediately close the
socket. The text will never leave the buffer. You need to call
tcpsocket1.poll once after you write before you close and the text
will be sent.

But better of course is to convert to a state machine and use the
events to manage the conversation. Then you can close the socket when
you get the send complete event and you know it's buffer is empty.

Thanks,
James

James Sentman http://sentman.com http://MacHomeAutomation.com



_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 15:25 (Fri, 24 Apr 2009 10:25:24 -0400)
From: Bart Silverstrim


James Sentman wrote:
>
> On Apr 23, 2009, at 2:44 PM, Bart Silverstrim wrote:
>
>> tcpsocket1.Write("Hello")
>
> So I downloaded and compiled and installed netcat. It is as I thought I
> remembered, you cannot send text and then immediately close the socket.
> The text will never leave the buffer. You need to call tcpsocket1.poll
> once after you write before you close and the text will be sent.
>
> But better of course is to convert to a state machine and use the events
> to manage the conversation. Then you can close the socket when you get
> the send complete event and you know it's buffer is empty.

You're right...

I just tried a few combinations of chr(12) and chr(13) and they didn't
work, but adding a tcpsocket1.poll just after the write is what's needed
to get it to actually send the buffer's contents (I'm assuming) and get
netcat to print the data.

I wouldn't have thought of that...I don't know if it's entirely my
ignorance or something that's a little counter intuitive. I would have
thought that at least closing the socket would empty the buffer first.
Apparently not!

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Writing to a socket
Date: 24.04.09 16:21 (Fri, 24 Apr 2009 11:21:31 -0400)
From: Charles Yeomans

On Apr 24, 2009, at 10:59 AM, Joe Strout wrote:

> Bart Silverstrim wrote:
>> Charles Yeomans wrote:
>>> Try removing the Poll call, and instead write
>>>
>>> do until TCPSocket1.IsConnected
>>> if TCPSocket1.LastErrorCode = 0 then
>>> app.DoEvents
>> This is okay in GUI applications?
>
> Though it's rare for me to disagree with Charles, I would say no,
> this is not OK in a GUI app.
>
> Instead I would encourage you to abandon your attempt to make this a
> blocking method, and instead embrace the event-driven approach --
> just tell your socket to connect, return from your method, and send
> your data in response to other events (such as Connected).

As is so often the case, I agree with Joe. You're better off with an
event-driven design.

Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>