开发者

Java - freepascal communication

开发者 https://www.devze.com 2023-02-16 01:02 出处:网络
I have a problem with a communication between java and freepascal(lazarus) app. I use sockets. They connect properly. Everything is going smooth until I want to send something from one app to another.

I have a problem with a communication between java and freepascal(lazarus) app. I use sockets. They connect properly. Everything is going smooth until I want to send something from one app to another. In java instead of "abc" i have random chars. Heres the code:

Java:

ServerSocket serverSocket = new ServerSocket(1025);  
Socket client = serverSocket.accept();  
InputStream is = client.getInputStream();  
byte bytesTest[] = new byte[10000];  
byte bytes[] = new byte[is.read(bytesTest)];  
is.read(bytes);  
String textReceived = new String(bytes, "UTF-8");  
System.out.println(textReceived);

FP:

connect(socket, socket_info, SizeOf(socket_info));  
S := 'abcd';  
res := UTF8Bytes(S);  
send(socket, res, sizeof(res), 0);  
send(socket, res, sizeof(res), 0);

function UTF8Bytes(const s: UTF8String): TArray;  
begin  
  SetLength(Result, Length开发者_StackOverflow中文版(s)+1);  
  if Length(Result)>0 then  
    Move(s[1], Result[0], Length(s));  
  Result[high(Result)] := 0;  
end;       


On the pascal side, assuming 'TArray' is a byte arrray, you are not sending the contents of your buffer, you're sending it's address. Also, instead of specifying the length of the data, you're specifying size of a pointer. Try this:

send(socket, res[0], Length(res), 0);


Without really knowing anything about Pascal (I used it last about 12 years ago), your UTF8Byte function looks quite suspicious.

function UTF8Bytes(const s: UTF8String): TArray;  
begin  
  SetLength(Result, Length(s)+1);  
  if Length(Result)>0 then  
    Move(s[1], Result[0], Length(s));  
  Result[high(Result)] := 0;  
end;

What a type is TArray, it is really an array of bytes? Are you trying to add a 0 byte at the end? Why?

Additionally, what are you doing here?

S := 'abcd';
res := UTF8Bytes(S);
send(socket, res, sizeof(res), 0);
send(socket, res, sizeof(res), 0);

Is S of type UTF8String? And why send the same string twice?

Now for your Java side, it looks strange, too.

byte bytesTest[] = new byte[10000];
byte bytes[] = new byte[is.read(bytesTest)];
is.read(bytes);

First you read some number of bytes (up to 10000) into your bytesTest array, then you create a new array bytes with the length being the number of bytes you just have read. And then you try to read again (this time not counting how much you have) in your bytes array.

If this works with some other Java peer, I would like to see this.


The way to go would be to first send the length of your byte[], and then send this much bytes. On the reading side, make sure you read the right number of bytes.


Is this a network byte ordering issue? I do not know much about FP, but a quick search on the topic turns up some conventions on this page:

http://www.indyproject.org/sockets/teams/core/docs/standards/FreePascal.EN.aspx

You may need to do host-to-network byte ordering before sending, and then network-to-host byte ordering when receiving using FP.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号