使用Delphi Xe5为Android设置tcp服务器套接字类

时间:2013-11-28 23:44:35

标签: android delphi sockets delphi-xe5

项目文件:https://drive.google.com/file/d/0BxODt2IJsmR8cUQ4cVltbWFzYUk/edit?usp=sharing 这是我的运行代码

    ...........
try
soket1:=TJServerSoket.JavaClass.init((8090));
//adres:=soket1.iNetAdres.ip;
memo1.Lines.Add('Port: '+inttostr(soket1.Port));
except
  on E:Exception do memo1.Lines.Add('Port Açılamadı: '+E.Message);
end;
try
  memo1.Lines.Add('ip: '+jstringtostring((soket1.iNetAdres.Host)));
except
  on E:Exception do memo1.Lines.Add('ip bigisi alınamadı: '+E.Message);

end;
.............

............

try
  soketC:=soket1.accept;
  //o1.writeChars(stringtojstring('merhaba'));
  memo1.Lines.Add('accept' );
except
  on E:Exception do memo1.Lines.Add('accept Hata: '+E.Message);

end;

....................

try


  //i1:=soketC.getInputStream;
  memo1.Lines.Add('i1: '+inttostr(soketC.getInputStream.read) );
  //memo1.Lines.Add('i1: '+jstringtostring(soketC.getInputStream.readUTF) );
except
  on E:Exception do memo1.Lines.Add('i1 Hata: '+E.Message);
end;

try
  //o1:=TJoutStream.JavaClass.init(soketC.getOutputStream);
  //o1.writeChars(stringtojstring('merhaba') );
  soketC.getOutputStream.write(65);
  memo1.Lines.Add('o1: merhaba' );
except
  on E:Exception do memo1.Lines.Add('o1 Hata: '+E.Message);
end;

//soketC.getOutputStream.close;
//soketC.getInputStream.close;
//soketC.close;

请帮忙...... 我正在尝试“写”/“读”(输入流/输出流)到套接字但我得到错误...

这是我的坏套接字(socket'“s”)api:

.............
.............

  ////////////////////////////soket/////////////////////////////


  [javaSignature('java/net/InetAddress')]
  JAdres=interface(JObject)
  ['{7A525333-38BC-4D2A-8160-D164D1CAA30E}']
    function getHostName:Jstring;cdecl;

    property Host:Jstring read getHostName;

  end;

  JAdresClass=interface(JObjectClass)
  ['{B34C47C9-7FC8-4120-B087-60F517A9323D}']

  end;

  TJAdres = class(TJavaGenericImport<JAdresClass, JAdres>) end;


 ////////////////out buffer////////////////////
 ///

     [javaSignature('java/io/OutputStream')]
  Jout=interface(jobject)
    ['{AC9EEC99-FF3A-46E4-AFC3-C4D3309D8C8E}']
    //procedure bind(localAddr:integer);cdecl;//overload;
    //property Port: Integer read getLocalPort;
    procedure write(buffer:array of integer;offset,count:integer);cdecl;overload;
    procedure write(buffer:array of integer);cdecl;overload;
    procedure write(buffer:integer);cdecl;overload; //tek bit yaz
    //procedure writeChars(str:jstring);cdecl;
    procedure close;cdecl;
    procedure flush;cdecl;
  end;
  JoutClass = interface(JObjectClass)
    ['{46FBBEEE-22C7-45EA-BED3-A41BED01E500}']
    procedure init(); cdecl;//Deprecated
    end;

  TJout = class(TJavaGenericImport<JoutClass, Jout>) end;

 ////////////////////////////////////////////////

    [javaSignature('java/io/ObjectOutputStream')]
  JoutStream=interface(jobject)
    ['{A6421CC3-B76C-4124-AB2E-AEE0C4749E71}']
    //procedure bind(localAddr:integer);cdecl;//overload;
    //property Port: Integer read getLocalPort;
    procedure write(buffer:array of integer;offset,count:integer);cdecl;overload;
    procedure write(buffer:array of integer);cdecl;overload;
    procedure write(buffer:integer);cdecl;overload; //tek bit yaz
    procedure writeChars(str:jstring);cdecl;
    procedure close;cdecl;
    procedure flush;cdecl;
  end;
  JoutStreamClass = interface(JObjectClass)
    ['{83D69F8F-8833-4EF5-B00D-2EA45F58AC46}']
    function init(obj:Jout):JoutStream; cdecl;overload;//Deprecated
    end;

  TJoutStream = class(TJavaGenericImport<JoutStreamClass, JoutStream>) end;
  ///////////////////////input buffer/////////////////

       [javaSignature('java/io/InputStream')]
  Jin=interface(jobject)
   ['{42549EE4-B69C-4491-BEC1-5626C4E7D717}']
    //procedure bind(localAddr:integer);cdecl;//overload;
    //property Port: Integer read getLocalPort;
    function read(buffer:array of jbyte;offset,count:integer):integer;cdecl;overload;
    function read(buffer:array of jbyte):integer;cdecl;overload;
    function read():integer;cdecl;overload; //tek bit yaz
    //function readUTF():jstring;cdecl;
    procedure close;cdecl;
    procedure reset;cdecl;
  end;
  JinClass = interface(JObjectClass)
    ['{AA965CF8-ED96-4EFE-B5F3-B02DAEDA8979}']
    function init():Jin; cdecl;overload;//Deprecated
    //procedure init(); cdecl;overload;
    end;

  TJin = class(TJavaGenericImport<JinClass, Jin>) end;
  /////////////////////////////////////////////////////
     [javaSignature('java/io/ObjectInputStream')]
  JinStream=interface(jobject)
   ['{839ACAEC-229A-4261-88B4-136173B6FCBF}']
    //procedure bind(localAddr:integer);cdecl;//overload;
    //property Port: Integer read getLocalPort;
    function read(buffer:array of jbyte;offset,count:integer):integer;cdecl;overload;
    function read(buffer:array of jbyte):integer;cdecl;overload;
    function read():integer;cdecl;overload; //tek bit yaz
    function readUTF():jstring;cdecl;
    procedure close;cdecl;
    procedure reset;cdecl;
  end;
  JinStreamClass = interface(JObjectClass)
    ['{27B4071A-5BC8-4594-A701-DBB92DBEE45A}']
    procedure init(); cdecl;overload; Deprecated;
    //procedure init(); cdecl;overload;
    end;

  TJinStream = class(TJavaGenericImport<JinStreamClass, JinStream>) end;




 ////////////////client socket////////////////////
  [javaSignature('java/net/Socket')]
  JSoket=interface(jobject)
   ['{3120C92A-DA6B-4584-B6FB-DEDEB36CA6FB}']
      procedure close;cdecl;
      function getOutputStream:Jout;cdecl;
      function getInputStream:Jin;cdecl;


  end;

    JSoketClass = interface(JObjectClass)
    ['{F1C713D1-9A17-48EF-BD98-60FD859A2B3E}']
    function init:JSoket; //cdecl;overload;//Deprecated
  end;

  TJSoket = class(TJavaGenericImport<JSoketClass, JSoket>) end;

///

  [javaSignature('java/net/ServerSocket')]
  JServerSoket=interface(jobject)
    ['{FB896B05-0F70-4BF7-A69D-4655D173A303}']
    function getLocalPort:integer;cdecl;//overload;
    procedure bind(localAddr:integer);cdecl;//overload;
    procedure close;cdecl;//overload;
    function getInetAddress:JAdres;cdecl;
    function accept():JSoket;cdecl;

    property Port: Integer read getLocalPort;
    property iNetAdres: JAdres read getInetAddress;
    //function getLocalPort: Integer; cdecl;
    //procedure ServerSocket(port:integer);cdecl;

  end;

    JServerSoketClass = interface(JObjectClass)
    ['{5D578C73-1315-4143-B14C-8E26BC1975BC}']
    function init(port:integer):JServerSoket; //cdecl;overload;//Deprecated
  end;

  TJServerSoket = class(TJavaGenericImport<JServerSoketClass, JServerSoket>) end;

..........

1 个答案:

答案 0 :(得分:0)

需要修复;

[javaSignature('java/io/OutputStream')]
Jout=interface(jobject)
['{AC9EEC99-FF3A-46E4-AFC3-C4D3309D8C8E}']
procedure write(buffer:TJavaArray<byte>;offset,count:integer);cdecl;overload;
procedure write(buffer:TJavaArray<byte>);cdecl;overload;
...........

写缓冲区;

编码(用于ışİçö... ...)功能;

function stringtoJbyte(metin:String):TJavaArray<byte>;
var
  buffer: TJavaArray<byte>;
  bufferByte:TArray<byte>;
  i,len:integer;
begin
  bufferByte:=TEncoding.UTF8.GetBytes(metin);
  //len:=ByteLength(metin);
  len:=length(bufferByte);
  buffer := TJavaArray<byte>.Create(len+1);
  for i := 0 to len do
        buffer.Items[i+1]:=bufferByte[i] ;
  result:=buffer;
end;

call api func;

soketC.getOutputStream.write(stringtoJbyte('kesinlikle yazar'));