Good TCP Cliente Example =)

Just starting out? Need help? Post your questions and find answers here.
User avatar
flaviofornazier
User
User
Posts: 11
Joined: Mon Jan 06, 2014 8:28 pm
Location: Brazil

Good TCP Cliente Example =)

Post by flaviofornazier »

Hi guys!

Happy new year!

I'm a new PB fan from Brazil and I'm still learning about it with the help file , I'm not a lazy, please believe me. =)

I hope be welcome here.

I'm needing a little example of a simple but good TCP Client that will send a single ASCII string to my TCP server and will be answered with another ASCII string with a terminator character.

I still don't know how implement a right line of code to receive the answer and a timer too, like in VB6.

PS: I don't need the server code and sorry for the english, my languages are portuguese (native) and spanish. =)

Than you in advanced!
PB 6.10 LTS x86/64 - WXP - W7/x86-64 - W8/x64 - W10 x86/x64 - W11 - Ubuntu Mate 23 x86/x64 - OSX Yosemite 10.9
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Good TCP Cliente Example =)

Post by infratec »

Hi,

a first blind programmed example:

Code: Select all

EnableExplicit

#Server$ = "127.0.0.1"
#Port = 80

Define.i Exit, Event, Timeout, Ok, ConId
Define Line$

InitNetwork()

OpenWindow(0, 0, 0, 350, 100, "TCP Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

StringGadget(0, 10, 10, 250, 20, "")
ButtonGadget(1, 280, 10, 50, 20, "Send")

StringGadget(2, 10, 40, 250, 20, "")

Repeat
  
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          ConID = OpenNetworkConnection(#Server$, #Port)
          If ConID
            If SendNetworkString(ConID, GetGadgetText(0), #PB_UTF8)
              Timeout = 100
              Line$ = Space(1024)
              Repeat
                If NetworkClientEvent(ConID) = #PB_NetworkEvent_Data
                  If ReceiveNetworkData(ConID, @Line$, Len(Line$))
                    SetGadgetText(2, Line$)
                    Ok = #True
                  EndIf
                Else
                  Delay(10)
                  Timeout - 1
                EndIf
              Until Timeout = 0 Or Ok
              If Timeout = 0
                MessageRequester("Error", "Timeout")
              EndIf
            Else
              MessageRequester("Error", "Was not able to send")
            EndIf
            CloseNetworkConnection(ConId)
          Else
            MessageRequester("Error", "Was not able to connect to " + #Server$)
          EndIf
      EndSelect
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
  
Until Exit
Bernd
Last edited by infratec on Mon Jan 06, 2014 11:57 pm, edited 1 time in total.
User avatar
flaviofornazier
User
User
Posts: 11
Joined: Mon Jan 06, 2014 8:28 pm
Location: Brazil

Thank you Mr. Bernd! =)

Post by flaviofornazier »

Mr.Bernd,

Thank you for your fast reply.

That's exactly what I need.

Cheers!
PB 6.10 LTS x86/64 - WXP - W7/x86-64 - W8/x64 - W10 x86/x64 - W11 - Ubuntu Mate 23 x86/x64 - OSX Yosemite 10.9
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Good TCP Cliente Example =)

Post by infratec »

Hi,

I forgot

Code: Select all

CloseNetworkConnection(ConId)
I modified the listing above.

Bernd

P.S.: Only Bernd, we are all 'only' PB users :mrgreen:
Post Reply