Recent

Author Topic: Networking for a newbie  (Read 9016 times)

jonek

  • Newbie
  • Posts: 5
Networking for a newbie
« on: February 04, 2010, 06:05:20 pm »
Hi.  It's relatively early days for me programming. I've written a simple little game that I would like to make network playable.

Ideally, 2 players connect themselves over VPN, each runs their program, clicks a button to connect to each other, then start the game. The only information that needs sending is what keys have been pressed.

I'm only asking for pointers here. Can someone tell me where to look / what to look for?

I'm very grateful for your time.

Jon

Leledumbo

  • Hero Member
  • *****
  • Posts: 8764
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Networking for a newbie
« Reply #1 on: February 05, 2010, 07:19:35 am »
Quote
Can someone tell me where to look / what to look for?
Surely, you need networking components. Search the wiki, lazarus have synapse, lnet, and indy. I use lnet, but it hasn't released a version that works with current version of lazarus except in svn. And you have to understand how the communication is done (read some networking article or tutorial).

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Networking for a newbie
« Reply #2 on: February 05, 2010, 10:00:32 am »
Synapse has improved FPC compatibility lately.
You may download trunk.tar.gz from here:
http://synapse.ararat.cz/doku.php/download

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Networking for a newbie
« Reply #3 on: February 11, 2010, 10:51:27 pm »
I couldn't get the echo sample of Synapse working for some reason. I mean it compiles but i can't connect to it by any means. HTTP server worked fine but all i want is TCP or possibly UDP sockets for gaming purposes. An easy example of TCP client that sends a packet and receives would be nice aswell.

Edit: Making progress finally, could it have been simply wrong port number  :-[
« Last Edit: February 11, 2010, 11:23:39 pm by User137 »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Networking for a newbie
« Reply #4 on: February 13, 2010, 12:22:54 am »
So.. i managed to make a fully working multithreaded TCP sockets wrapper for synapse. All but to 1 weird error i can reproduce every time:

If i have GNU debugger enabled and i have server listening when i close application i get SIGSEGV (same as "access violation" i guess). If i disable debugger or just run executable there are no errors during run or exit.

Any ideas?

Pasted whole unit here: http://pastebin.com/d1566a8db
« Last Edit: February 13, 2010, 01:53:28 am by User137 »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Networking for a newbie
« Reply #5 on: February 14, 2010, 12:27:47 pm »
Ok, above pasted code is now much out of date but i've gotten rid of all problems, and it works for UDP aswell  :D So i might write few tips for using synapse that is basically multithreaded:

1) The most clean way to kill a connection is send a small exitcode (say a string "#Exit#) for counterpart. This will quickly end your thread without waiting for an error, which in UDP case will never come.

2) If you provide threads with pointer to a main controller class, be sure to handle cases when it is destroyed. Each thread = open socket, if you keep socket objects in main class (like i do) make sure that threads don't have pointers to null objects. What i did was copied over the pointers to socket objects and if thread was alive on main class.Destroy, just nil them in main class, Terminate thread and tell them to free sockets.
Basically avoid bruteforcing the threads, just give them Terminate code and if socket is listening or on long timeout; AbortSocket.

 

TinyPortal © 2005-2018