Networking question

Just starting out? Need help? Post your questions and find answers here.
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Networking question

Post by sirrab »

Hi All,

I hope someone can help me as this is driving me nuts :)

I am writing a program that accesses a server. when I use telnet and enter the commands no worries it works. But if I send the commands using sendnetworkstring or sendnetworkdata with chr(10) and chr(13) on the end nothing. is there something about pb that could be causing this? I cant really ask the creater of the server as I can't get into there yahoo group and there docs are a joke !

Any ideas very welcome :)

Craig
swhite
Addict
Addict
Posts: 805
Joined: Thu May 21, 2009 6:56 pm

Re: Networking question

Post by swhite »

Perhaps you could post your code so we can see what you are doing.

Simon
Simon White
dCipher Computing
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: Networking question

Post by sirrab »

My code as requested :)



Code: Select all

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf

dccserver =  OpenNetworkConnection("192.168.1.23",3246,#PB_Network_TCP)

Procedure receivedata(dccserver,waitfor$)
  *Buffer = AllocateMemory(1000)
  dsize = 100
  If NetworkClientEvent(dccserver) = #PB_NetworkEvent_Data      
    
    FreeMemory(*Buffer)
    *Buffer = AllocateMemory(1000)
  dsize = ReceiveNetworkData(dccserver, *Buffer,1000)
  ;dsize = Len(PeekS(*Buffer))
  
  ;Debug dsize
  ;Debug PeekS(*Buffer)
 
 EndIf
  FreeMemory(*Buffer)
EndProcedure




Procedure senddata(dccserver,datatosend$)
  
    *sendbuffer = AllocateMemory(Len(datatosend$) + 1)
    PokeS(*sendbuffer,datatosend$)
    ; SendNetworkData(dccserver,*sendbuffer  ,Len(datatosend$))  
    SendNetworkString(dccserver,datatosend$)
    FreeMemory(*sendbuffer)
    
EndProcedure

ce$ = Chr(13) + Chr(10)

dccserver =  OpenNetworkConnection("192.168.1.23",3246,#PB_Network_TCP)
If dccserver
  
  receivedata(dccserver,"PW12080")
  
    senddata(dccserver,"N1234" + ce$) 
    
    
    senddata(dccserver,"TL2836")
  

  CloseNetworkConnection(dccserver) 
  
  EndIf
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Networking question

Post by netmaestro »

Call NetworkClientEvent() in a loop to wait until you receive something. Calling it just the once won't work.
BERESHEIT
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: Networking question

Post by sirrab »

Thanks for the reply netmaestro,

I tried that but no luck. Its like the enter key needs to be pressed. I have tried it with chr(13) added as well as chr(10) and combinations of both. Anyone know what pressing enter in telnet sends?

Thanks Craig
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Networking question

Post by infratec »

Hi,

try this:

Code: Select all

EnableExplicit


Procedure.i receivedata(dccserver.i, waitfor$)
  
  Protected Result.i, dsize.i, *Buffer, Rcv$
  
  
  *Buffer = AllocateMemory(1000)
  If *Buffer
    
    PokeS(*Buffer, waitfor$, -1, #PB_UTF8)
    waitfor$ = PeekS(*Buffer, -1, #PB_UTF8)
    
    Repeat
      If NetworkClientEvent(dccserver) = #PB_NetworkEvent_Data     
        
        dsize = ReceiveNetworkData(dccserver, *Buffer, 1000)
        If dsize
          Debug dsize
          Rcv$ = PeekS(*Buffer, dsize, #PB_UTF8)
          Debug Rcv$
          If Rcv$ = waitfor$
            Result = #True
          EndIf
        EndIf
      Else
        Delay(10)
      EndIf
    Until Result
    
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure



Define.i dccserver

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf

dccserver =  OpenNetworkConnection("192.168.1.23", 3246, #PB_Network_TCP)
If dccserver
  
  receivedata(dccserver, "PW12080")
  
  SendNetworkString(dccserver,"N1234" + #CRLF$, #PB_UTF8)
  SendNetworkString(dccserver,"TL2836", #PB_UTF8)
  
  CloseNetworkConnection(dccserver)
Else
  MessageRequester("Error", "Was not able to connect.")
EndIf
Bernd
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: Networking question

Post by sirrab »

Hi Al, sorry about not replaying sooner.

Thanks for all the ideas, no luck with though :)

Craig
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Networking question

Post by RichAlgeni »

Did you ever get this working?
User avatar
sirrab
Enthusiast
Enthusiast
Posts: 106
Joined: Thu Dec 07, 2006 8:52 am
Location: New Zealand

Re: Networking question

Post by sirrab »

Sorry about the delay in replying. No I haven't got it working. Been a bit busy of late. will have to try and have a play with it this week ! :)

Craig
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Networking question

Post by RichAlgeni »

In your procedure below, you have the #PB_NetworkEvent_Data check, but you only do it once. If there isn't data in the network receive buffer, the #PB_NetworkEvent_Data check will fail. Your code will continue after the EndIf statement, where the procedure will end. You need loop statement to check #PB_NetworkEvent_Data again, and a short delay to wait until your next attempt. You also need some sort of loop counter, so that if nothing is received with a reasonable amount of time, your loop can end with some sort of error message.

Code: Select all

Procedure receivedata(dccserver,waitfor$)
  *Buffer = AllocateMemory(1000)
  dsize = 100
  If NetworkClientEvent(dccserver) = #PB_NetworkEvent_Data
    FreeMemory(*Buffer)
    *Buffer = AllocateMemory(1000)
  dsize = ReceiveNetworkData(dccserver, *Buffer,1000)
  ;dsize = Len(PeekS(*Buffer))
    ;Debug dsize
  ;Debug PeekS(*Buffer)
  EndIf
  FreeMemory(*Buffer)
EndProcedure
There are many ways to accomplish what you need, with Infratec's being a good example. If you are serious about learning to code, I along with others will be glad to help you. The first thing I'd recommend is to download and read Kale's book! The second thing is to always use EnableExplicit.
Post Reply