Recent

Author Topic: How to create a TCP socket for android?  (Read 6221 times)

Jonathan

  • New Member
  • *
  • Posts: 40
How to create a TCP socket for android?
« on: February 10, 2012, 08:35:40 am »
I try to create a tcp socket using the lnet's TLTCP, but it doesn't work :(

I use the newest android lcl example from felipemdc.

add some code in the Button1Click   (define a selfSocket within the Form's private )

...
address := '192.168.1.7';
port := 40100;
selfSocket := TLTCP.Create(nil);
if selfSocket.Connect(address,port) then
 ......

fastblade

  • New Member
  • *
  • Posts: 17
Re: How to create a TCP socket for android?
« Reply #1 on: February 12, 2012, 02:13:13 pm »
haven't test the lnet, but add following code to the button1click, it works :)
Code: [Select]
selfsocket := fpSocket(AF_INET,SOCK_STREAM,IPPROTO_IP);
  if selfsocket = -1 then
   begin
    Application.OnMessageDialogFinished := @HandleMessageDialogFinished;
    Application.MessageBox('SOCKET ERROR', 'Title', MB_ABORTRETRYIGNORE);
   end else
   begin
    ProgressBar1.Position := ProgressBar1.Position + 10;
    saddr.sin_family := AF_INET;
    saddr.sin_port := htons(4001);
    sAddr.sin_addr.s_addr := HostToNet((192 shl 24) or (168 shl 16) or (1 shl 8) or 7);
    if fpConnect(selfSocket,@saddr,SizeOf(TSockAddr)) <> 0 then
     begin
      ProgressBar1.Position := ProgressBar1.Position + 50;
     end else
     begin
      fpShutDown(selfSocket,2);
     end;
   end;

If want those code work, must modify the AndroidManifest.xml, add line: <uses-permission android:name="android.permission.INTERNET"></uses-permission>
Don't forget to setup your emulator's network proxy (-proxy) on command line startup