Forum > Android

How to create a TCP socket for android?

(1/1)

Jonathan:
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:
haven't test the lnet, but add following code to the button1click, it works :)

--- Code: ---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;

--- End code ---

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

Navigation

[0] Message Index

Go to full version