Page 1 of 1

Network tutorials?

Posted: Sat Jun 04, 2005 10:20 pm
by Afr0
Ok, I know that this might give a bad first impression and all, but I'll be frank. I'm making an MMORPG. Deal with it. Ok, to the questions:

Today, I got PureBasic, and being an old and sturdy Blitz3D programmer, I had a basic windowed network application going within 30 minutes. Now I'm about to convert my B3D code to PB code, but I've run into some problems. I can't seem to figure out how to use the ReceiveNetworkData() and SendNetworkData commands. Ok, so here's my current login code on the serverside:

Code: Select all

Procedure LogIn()
  msg.s ;Used to store our recieved string data.
  
  jp.JPlayer
  a.Account
  
  jp\id = NetworkClientID()
  
  a\Name = ReceiveNetworkData(jp\id, a\Name, SizeOf(a\Name))
EndProcedure
I know that the first parameter is ok, and I think that the second one is too, as long as I add a @ character at the beginning. But, I'm totally unsure about the third parameter. How can I know the size of the data being sent before I send it? I tried investigation SizeOf(), but it seems that it can only be used on Structures. I'm at a loss. Does anyone have any tutorials or some tips? I'll manage on my own, I only need to figure out how to send and recieve data properly.

Posted: Sat Jun 04, 2005 10:35 pm
by Gansta93
Hello,

The function ReceiveNetworkData() return the specified number in the 3rd parameter if there are steal some datas to receive. If there are no data, u have received every datas.
U can make a loop which make a receivenetworkdata() until it return 0 for example.
I know, it is hard, I have understood that after a long time and I am not sure of my explanation.

Posted: Sat Jun 04, 2005 11:00 pm
by Joakim Christiansen
I once tryed a to make a network game in PureBasic, but I did'nt finish it.
It's only two dot's moving, but it might help you.

Code: Select all

Buffer.l = AllocateMemory(1000)
IP.s
Port.w
Event.l
ConnID.l
ClientID.l
Quit.b
IsServer.b

PreviousX.w
PreviousY.w
Player1X.w = 320
Player1Y.w = 120
Player2X.w = 320
Player2Y.w = 360

Procedure DrawTextXY(x.w, y.w, string.s)
  Locate(x,y)
  DrawText(string)
EndProcedure

OpenConsole()

ConsoleTitle("Game")
Print("Initializing the network... ")
If InitNetwork() = #False
  PrintN("Error: Can't initialize the network.")
  PrintN("Press enter to quit.")
  Input()
  End
EndIf
Print("Done")
PrintN("")
Print("Press 1 for server or 2 for client: ")
If Input() = "1" ;Server
  PrintN("")
  IsServer = #True
  Print("Enter the network port: ")
  Port = Val(Input())
  PrintN("")
  If CreateNetworkServer(Port)
    PrintN("Server created.")
  Else
    PrintN("Error: Can't use that port.")
    PrintN("Press enter to quit.")
    Input()
    End
  EndIf
Else ;Client
  PrintN("")
  IsServer = #False
  Print("Enter the network port: ")
  Port = Val(Input())
  PrintN("")
  Print("Enter the network adress: ")
  IP = Input()
  PrintN("")
  PrintN("Connecting to: "+IP+":"+Str(Port))
  ConnID = OpenNetworkConnection(IP,Port)
  If ConnID
    PrintN("Client connected.")
  Else
    PrintN("Error: Can't find the server.")
    PrintN("Press enter to quit.")
    Input()
    End
  EndIf
EndIf

CloseConsole()

If InitKeyboard()=0 Or InitSprite()=0
  MessageRequester("Error","Can't initialize the game, try updating DirectX.")
  Quit = #True
ElseIf OpenScreen(640,480,16,"Game")=0
  MessageRequester("Error","Can't open a 640x480 16bit screen.")
  Quit = #True
EndIf
SetFrameRate(60)

While Quit = #False
  ;{/User input
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape): Quit = #True: EndIf
  If IsServer
    PreviousX = Player1X
    PreviousY = Player1Y
    If KeyboardPushed(#PB_Key_Up)
      Player1Y - 1
    EndIf
    If KeyboardPushed(#PB_Key_Down)
      Player1Y + 1
    EndIf
    If KeyboardPushed(#PB_Key_Left)
      Player1X - 1
    EndIf
    If KeyboardPushed(#PB_Key_Right)
      Player1X + 1
    EndIf
    If Player1X <> PreviousX Or Player1Y <> PreviousY
      SendNetworkString(ClientID,Str(Player1X)+","+Str(Player1Y)+"|")
    EndIf
  Else
    PreviousX = Player2X
    PreviousY = Player2Y
    If KeyboardPushed(#PB_Key_Up)
      Player2Y - 1
    EndIf
    If KeyboardPushed(#PB_Key_Down)
      Player2Y + 1
    EndIf
    If KeyboardPushed(#PB_Key_Left)
      Player2X - 1
    EndIf
    If KeyboardPushed(#PB_Key_Right)
      Player2X + 1
    EndIf
    If Player2X <> PreviousX Or Player2Y <> PreviousY
      SendNetworkString(ConnID,Str(Player2X)+","+Str(Player2Y)+"|")
    EndIf
  EndIf
  ;}
  ;{/Game logic
  If IsServer
    Event = NetworkServerEvent()
    If Event
      ClientID = NetworkClientID()
      Select Event
        Case 1: ;New client connected
        Case 2: ;Raw data
          ReceiveNetworkData(ClientID,Buffer,1000)
          Player2X = Val( StringField(PeekS(Buffer),1,",") )
          Player2Y = Val( StringField(PeekS(Buffer),2,",") )
        Case 3: ;A file has been recieved
        Case 4: ;Client deconnection
      EndSelect
    EndIf
  Else
    Event = NetworkClientEvent(ConnID)
    If Event
      Select Event
        Case 2: ;Raw data
          ReceiveNetworkData(ConnID,Buffer,1000)
          Player1X = Val( StringField(PeekS(Buffer),1,",") )
          Player1Y = Val( StringField(PeekS(Buffer),2,",") )
        Case 3: ;A file has been recieved
        Case 5: ;A string has been recieved
      EndSelect
    EndIf
  EndIf
  ;}
  ;{/Draw screen
  FlipBuffers(): Delay(2)
  ClearScreen(0,0,0)
  
  StartDrawing(ScreenOutput())
    DrawingMode(1)
    FrontColor(255,255,255)
    DrawTextXY(0,16,"P1x: "+Str(Player1X)+" P1y: "+Str(Player1Y))
    DrawTextXY(0,32,"P2x: "+Str(Player2X)+" P2y: "+Str(Player2Y))
    FrontColor(128,0,0)
    Circle(Player1X,Player1Y,10)
    FrontColor(0,0,128)
    Circle(Player2X,Player2Y,10)
  StopDrawing()
  ;}
Wend

If IsServer
  CloseNetworkServer()
Else
  CloseNetworkConnection(ConnID)
EndIf

Posted: Sun Jun 05, 2005 5:05 am
by dagcrack
Hi afro, is it me or you blocked me on messenger?
bad impressions again...

Posted: Sun Jun 05, 2005 9:43 am
by Afr0
Thanks for the replies dudes:)

@dagcrack: w00t?! I haven't done that? :\

Posted: Sun Jun 05, 2005 8:50 pm
by dagcrack
We wont be saying "you cant make an MMORPG, go make pong clone" because we aint that salad of losers you had on that forum you used to go that now closed and ehm I said too much already :lol: :lol:

Then I think I stopped using my second account, I'll start again. but having 2 messengers pisses me off.