Page 1 of 1

Usenet

Posted: Tue Jan 01, 2008 12:20 am
by Xombie
Could someone take a look at this and tell me why it seems to stall after the "VERSION" send network string call? No huge hurry as this is just a curiosity/hobby thing.

It just seems that anything I throw at this after the very first ReceiveNetworkData() freezes. I haven't messed with the networking stuff so I figure it's something on my end.

Code: Select all

InitNetwork()
*HoldData.l = AllocateMemory(1000)
lResult.l = OpenNetworkConnection("news.giganews.com", 119, #PB_Network_TCP)
If lResult
   ;
   lMax.l = ReceiveNetworkData(lResult, *HoldData, 1000)
   Debug lMax
   Debug PeekS(*HoldData, lMax)
   ;
   SendNetworkString(lResult, "VERSION")
   lMax.l = ReceiveNetworkData(lResult, *HoldData, 1000)
   Debug lMax
   Debug PeekS(*HoldData, lMax)
   ;
   CloseNetworkConnection(lResult)
   ;
EndIf
FreeMemory(*HoldData)
Also - when/if it does work, do I just send a "AUTHINFO USER XXXXX" and then "AUTHINFO PASS YYYYYY" to authenticate? I've tried both and get the same behavior as above with VERSION.

Thanks!

Posted: Tue Jan 01, 2008 12:46 am
by Thalius
Could someone take a look at this and tell me why it seems to stall after the "VERSION" send network string call?
On the first look at it i noticed you dont check for events. The responding data on the second call might not yet have been received when you check the buffer. Loop thru it until ReceiveData is 0 or you get to an end of an expected answer, returncode, whatever. And check for events and this might work. Also on the loop its kinda safe to put a delay in there so you dont lock the Receivebuffer too often and free some cputime, as the network usually is slower in delivering Data anyway ( Delay(1) -> up to 70 MB/s - using 16kb chunkbuffers).

Thalius

Posted: Tue Jan 01, 2008 3:04 am
by Xombie
Hey - thanks for the reply.

I should have mentioned that a different version I did check for a NetworkClientEvent() and that just returns 0 with and without a loop.

I can't put the ReceiveNetworkData() into an event as it locks up the program as soon as it's called. Well, I *say* locks up but I have no clue what's actually happening. I just know the program stops responding for a very loooong time when I call ReceiveNetworkData() after sending the VERSION query.

Posted: Tue Jan 01, 2008 3:10 am
by Rook Zimbabwe
SWell... you aren't clearing the buffer either, there is a way to check for a recieve event but buffer clearing will have to wait till after the party. SWill post what code I use in my sr ver tomorrow! :)

Posted: Tue Jan 01, 2008 3:52 am
by Rook Zimbabwe
BASIC SERVER CODE:
I used this as the basis of my server.

Code: Select all


Enumeration
  #Window_0
EndEnumeration

Enumeration
  #String_INPUT
  #Button_SEND
  #Text_1
  #Text_SERVADDRESS
  #Listview_TEXT
  #Listview_ACTIVITY
  #Server
  #Font1
 
EndEnumeration

;- DATABASE CRAP
; I use a DBASE in my server
; Cut out for simplicity


Declare AddConnection(Driver.s,ConnectString.s) 
Declare RemoveConnection(Driver.s,DSN.s) 



Structure VisualDesignerGadgets
  Gadget.l
  EventFunction.l
EndStructure

Global ClientID
Global NewList EventProcedures.VisualDesignerGadgets()
Global FontID1
FontID1 = LoadFont(#Font1, "Courier", 9)
Global String$
Global buffet

Global *buffer = AllocateMemory(2000)


Declare ClearMemory(*memory.b, length.l)

Procedure getnumb()
        String$ = ""
                numb = Random(1200)+1
                  String$ = Str(numb)
                    SendNetworkString(ClientID, String$) 
                      AddGadgetItem(#Listview_ACTIVITY,-1,"SENT: "+String$)
                        ClearMemory(*buffer,2000)
EndProcedure

Procedure sendfile()
Filename$ = "menu.pref"
  Result = SendNetworkFile(ClientID, FileName$)

EndProcedure


Procedure ClearMemory(*memory.b, length.l) ; from tinman

fillmemory_(*memory.b , length.l , 0) 

 EndProcedure 


Procedure String_INPUT_Event(Window, Event, Gadget, Type)
  Debug "#String_INPUT"
EndProcedure

Procedure Button_SEND_Event(Window, Event, Gadget, Type)
  Debug "#Button_SEND"
     String$ = GetGadgetText(#String_INPUT) ; to enter something to send
        SendNetworkData(ClientID, @String$, Len(String$)) 
          SetGadgetText(#String_INPUT, "") ; clear the text from memory
          AddGadgetItem(#Listview_TEXT, -1,"YOU: "+String$) ; throw the text in the window
              String$ = ""
                SetActiveGadget(#String_INPUT) ; reset the input string
                ClearMemory(*buffer,2000)
                
EndProcedure

Procedure Text_1_Event(Window, Event, Gadget, Type)
  Debug "#Text_1"
EndProcedure

Procedure Text_SERVADDRESS_Event(Window, Event, Gadget, Type)
  Debug "#Text_SERVADDRESS"
EndProcedure

Procedure Listview_TEXT_Event(Window, Event, Gadget, Type)
  Debug "#Listview_TEXT"
EndProcedure

Procedure Listview_ACTIVITY_Event(Window, Event, Gadget, Type)
  Debug "#Listview_ACTIVITY"
EndProcedure

Procedure RegisterGadgetEvent(Gadget, *Function)
  
  If IsGadget(Gadget)
    AddElement(EventProcedures())
      EventProcedures()\Gadget        = Gadget
        EventProcedures()\EventFunction = *Function
  EndIf
  
EndProcedure

Procedure CallEventFunction(Window, Event, Gadget, Type)
  
  ForEach EventProcedures()
    If EventProcedures()\Gadget = Gadget
      CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
          LastElement(EventProcedures())
    EndIf
  Next
  
EndProcedure

Procedure Open_Window_0()
  
  If OpenWindow(#Window_0, 99, 332, 510, 380, "SERVER",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
    If CreateGadgetList(WindowID(#Window_0))
      ListViewGadget(#Listview_ACTIVITY, 324, 42, 174, 246) ; what happens on outside
      SetGadgetFont(#Listview_ACTIVITY, FontID1)
      RegisterGadgetEvent(#Listview_ACTIVITY, @Listview_ACTIVITY_Event())
      ListViewGadget(#Listview_TEXT, 6, 42, 312, 246) ; what happens internally inside
      RegisterGadgetEvent(#Listview_TEXT, @Listview_TEXT_Event())
      TextGadget(#Text_SERVADDRESS, 6, 12, 120, 18, "0.0.0.0", #PB_Text_Center) ; server address
      RegisterGadgetEvent(#Text_SERVADDRESS, @Text_SERVADDRESS_Event())
      TextGadget(#Text_1, 132, 12, 132, 18, "SERVER ADDRESS")
      RegisterGadgetEvent(#Text_1, @Text_1_Event())
      ButtonGadget(#Button_SEND, 6, 336, 312, 36, "SEND") ; send out from server
      RegisterGadgetEvent(#Button_SEND, @Button_SEND_Event())
      StringGadget(#String_INPUT, 6, 294, 312, 42, "") ; what we send from server
      RegisterGadgetEvent(#String_INPUT, @String_INPUT_Event()) 
      
    EndIf
  EndIf
EndProcedure


InitNetwork() 
If ExamineIPAddresses() 
  IP.l = NextIPAddress() 
EndIf 

*buffer = AllocateMemory(2000) ; you might want to increase this size here

CreateNetworkServer(#Server, 6654) ; You can easily change the port

Open_Window_0()

SetActiveGadget(#String_INPUT) ; reset the input string

AddGadgetItem(#Listview_ACTIVITY,-1,"Server Online") ;  Listening on IP (" + IPString(IP) + ")") 
AddGadgetItem(#Listview_ACTIVITY,-1,"IP: (" + IPString(IP) + ")")
SetGadgetText(#Text_SERVADDRESS, ""+IPString(IP))

Repeat
  
  Event  = WaitWindowEvent()
  Gadget = EventGadget()
  Type   = EventType()
  Window = EventWindow()
  
  Select Event
    Case #PB_Event_Gadget
      CallEventFunction(Window, Event, Gadget, Type)     
  EndSelect
  
    If NetworkServerEvent() = 1 ; someone connected to us
            AddGadgetItem(#Listview_ACTIVITY,-1,"A new connection") 
              ClientID.l = EventClient() 
                IP = GetClientIP(ClientID) 
                  Cl$ = Str(IP)
                  AddGadgetItem(#Listview_ACTIVITY,-1,"CLIENT: "+Cl$)
     EndIf
     If NetworkServerEvent() = 2 ; SOMETHING CAME IN TO THE SERVER
     ClearMemory(*buffer,2000)
        length.l = ReceiveNetworkData(ClientID, *buffer, 2000) 
          String$ = PeekS(*buffer, 2000) 
            AddGadgetItem(#Listview_TEXT, -1,"THEM: "+String$) 
              If String$ = "GETN"
                String$ = "0"
                
                getnumb()
                  ClearMemory(*buffer,2000)
              EndIf
              ; If String$ = "GETFIL"
                    ; sendfile()
                ; ClearMemory(*buffer,2000)
              ; EndIf
              
     EndIf 
       
Until Event = #PB_Event_CloseWindow

End
You should be able to monkey that around to do what you want. 8)

Posted: Tue Jan 01, 2008 5:59 pm
by Xombie
I'm afraid I made you do way more work than was needed.

I've done a little bit of programming in a custom server/client program and that worked well. I understand the idea behind clients and servers quite well.

My problem is that I don't understand why the short bit of code in my first post doesn't work. If I strip down the code to just connect to the news server, it works...

Code: Select all

InitNetwork()
*HoldData.l = AllocateMemory(1000)
lResult.l = OpenNetworkConnection("news.giganews.com", 119, #PB_Network_TCP)
If lResult
   ;
   lMax.l = ReceiveNetworkData(lResult, *HoldData, 1000)
   Debug lMax
   Debug PeekS(*HoldData, lMax)
   ;
   CloseNetworkConnection(lResult)
   ;
EndIf
FreeMemory(*HoldData)
The ReceiveNetworkData() call receives 23 bytes of data and returns "200 News.GigaNews.Com.."

However, when I add in another SendNetworkString()/ReceiveNetworkData() the program locks up on the Receive part. It shouldn't right? Shouldn't it just return 0 or something to signify an error? NetworkClientEvent after a SendNetworkString() with VERSION returns 0.

So I apologize again. I understand the basic ideas in networking and I have created simple client/servers in the past that worked. I'm mainly concerned over the code in my first post as it seems like it should work.

Thanks!

Posted: Tue Jan 01, 2008 6:47 pm
by Rook Zimbabwe
Well have you peeked into the buffer to see what is coming back? That may be where you lock up.

Throw the reply to debug.

FreeMemory() may also be having issues... I know my clearmemory function was giving me pains! 8)

{{EDIT}}

WAIT... I CAUGHT THIS... tweaking code give me a second!!!

{{UPDATE}}

Crud no!!! I thought I had it with the ConnectionID...

OK on my system two things are happening:

1. The Avast Mail Scanner is running when I run this program, but there is no activity.
2. Nothing is coming back from the Connection which is why your program appears to be locking up... I don't think it is you... I am now of the opinion that it may be the site.

Posted: Wed Jan 02, 2008 6:51 am
by Demivec
@Xombie: use a timeout value for excessively slow responses.

Posted: Wed Jan 02, 2008 11:05 pm
by Xombie
@Demivec,

The problem is that the ReceiveNetworkData() call never returns. I'd have to put it in it's own thread and kill the thread if it never gets anywhere.

Is it valid to check NetworkClientEvent() for non-zero when I send out the VERSION request? The only problem is that when I do that, it always returns zero. I've even tried waiting for NCE() to return non-zero but it never does.

There has to be some ridiculously simple mistake I'm making somewhere.

I've also tried freeing/reallocating the memory between calls but that didn't work either.

Posted: Thu Jan 03, 2008 4:08 pm
by Thalius
lol ok - am gonna fiddle around here myself now from scratch and see if theres a bug or simply a logic mistake. Since looking at the baseadresses the second ReceieveNetworkData() jumps to a wierd adress here.

But i think its some logic error - as my network game engine runs fine - where i at least for chats also use a string based datastream.

Thalius

Posted: Fri Jan 11, 2008 7:53 pm
by Xombie
Any luck, Thalius?

Posted: Fri Jan 11, 2008 10:11 pm
by waldschrath
Each command has to be terminated by CR-LF. The Code below returns a response from the Server, even if it is only "500 syntax error or unknown command".

Code: Select all

InitNetwork()
*HoldData.l = AllocateMemory(1000)
lResult.l = OpenNetworkConnection("news.giganews.com", 119, #PB_Network_TCP)
If lResult
   ;
   lMax.l = ReceiveNetworkData(lResult, *HoldData, 1000)
   Debug lMax
   Debug PeekS(*HoldData, lMax)
   ;
   SendNetworkString(lResult, "VERSION" + Chr(13) + Chr(10))
   lMax.l = ReceiveNetworkData(lResult, *HoldData, 1000)
   Debug lMax
   Debug PeekS(*HoldData, lMax)
   ;
   CloseNetworkConnection(lResult)
   ;
EndIf
FreeMemory(*HoldData)

Posted: Sat Jan 12, 2008 12:13 am
by Xombie
...... that's what I meant by "ridiculously simple mistake". I'm going to test that when I get home. I have a feeling that that's exactly what the problem was.

sigh =_=

Thanks (in advance!), waldschrath!