Recent

Author Topic: Connect to another aplication via TCP/UDP  (Read 6408 times)

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Connect to another aplication via TCP/UDP
« on: July 21, 2011, 11:20:38 am »
Hello!

I'd like to make application which is able to connect via TCP or UDP to another one. I'm not sure how to do this but i can make it in PHP. The application is Counter-Strike 2D dedicated server. Look at this.

This function execute command. It doesn't return anything.
Code: PHP  [Select][+][-]
  1. function send_command($ip,$port,$rcon,$command) {
  2.         $fp = @fsockopen("udp://".$ip, $port, $errno, $errstr);
  3.         if ($fp){
  4.                 $request = chr(1).chr(0).chr(242).chr(strlen($rcon)).$rcon.pack("S",strlen($command)).$command;
  5.                 fwrite($fp, $request);
  6.         }
  7. }
  8.  

But this one returns some value (for example list of players etc..).
Code: PHP  [Select][+][-]
  1. function send_command_rtn($ip,$port,$rcon,$command) {
  2.         $fp = @fsockopen("udp://".$ip, $port, $errno, $errstr);
  3.         if ($fp){
  4.                 $request = chr(1).chr(0).chr(242).chr(strlen($rcon)).$rcon.pack("S",strlen($command)).$command;
  5.                 fwrite($fp, $request);
  6.                 $ausgabe = fread($fp, 1000);
  7.                 return $ausgabe;
  8.         }
  9. }

What should i do to make this in Lazarus? Any ideas would be really appreciated!
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

WalterK

  • New Member
  • *
  • Posts: 48
Re: Connect to another aplication via TCP/UDP
« Reply #1 on: July 21, 2011, 03:57:43 pm »
I'm not sure if this is what you're looking for, but there are two small example programs in the rtl documentation under the Connect and fpAccept entries; search for sockcli.pp and socksvr.pp.  The rtl pdf is here: ftp://ftp.freepascal.org/pub/fpc/docs-pdf/rtl.pdf

Leledumbo

  • Hero Member
  • *****
  • Posts: 8833
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Connect to another aplication via TCP/UDP
« Reply #2 on: July 31, 2011, 09:23:51 am »
FPC has basic units for this with sockets unit. However, it's much easier to use 3rd party libraries like synapse or lnet. For games, it's better to use lnet because it has full asynchronous connection support.

 

TinyPortal © 2005-2018