如何用delphi socket 传递自定义对象

 我来答
硪丨暧恋
2017-04-15 · TA获得超过8980个赞
知道大有可为答主
回答量:5336
采纳率:93%
帮助的人:2171万
展开全部
参考代码:
(1) delphi客户端发送:

csConnect()中

tmpstr := ipcode + '|setclient|' + ip + '|' + mac + '|' + #13#10;
Socket.SendText(tmpstr);

(2)Java服务接受:

public ServerThread2(Socket s) { // serverThread的构造器
this.request = s;
// 接收receiveServer传来的套接字
try { // 初始化输漏游入、输出流
response = new DataOutputStream(request.getOutputStream());
input = new DataInputStream(request.getInputStream());
} catch (IOException e) {
GlobaTx.LogLn(e.getMessage());
}
}

@Override
public void run() { // 线程返裂销的执行方法
String str = "";
//StringBuilder sb=new StringBuilder();

str = input.readLine();

//下边详细处理

}

(3)java服务端返回:

/**
* 发送报文到客户端
* @param response
* @param pg
* @return
*/
public static boolean writePg(DataOutputStream response, DataPg pg) {
try {
String tmp=pg.getReturnStr()+"*";
System.out.println("sender:"+tmp);
response.writeBytes(tmp);//(s)writeUTF
response.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
}
}

(4)delphi客户端接收:

csRead()中

res := Socket.ReceiveText;
tmpstr := res[length(res)];
if tmpstr <> '*' then //约定的结束字符*,防止传送不完源毕整串
begin
pgcs := pgcs + res;
Exit;
end;
res := pgcs + res;
pgcs := '';

--------------------------------------------------------------------------------------------------------------------

大文件传送:

(1)delphi发送

procedure TForm1.csfileConnect(Sender: TObject; Socket: TCustomWinSocket);
var
Buf: array[1..MAX_BUF + 1] of char;
bytes, iLength, MySendLength: Integer;
res, tmpstr, Msg: string;
bufSend: pointer;
picname: string; //保存的照片名称
ipcode: string; //机器编号
timestr: string; //时间
lt: TSYSTEMTIME;
begin
res := pg;
ipcode := tools.findsegment(res, 1, '|');
GetLocalTime(lt);

picname := ipcode + '-' + inttostr(lt.wYear) +
inttostr(lt.wMonth) + inttostr(lt.wDay) + inttostr(lt.wHour)
+ inttostr(lt.wMinute) + inttostr(lt.wSecond) + '.bmp';
tools.CaptureScreen(picname);
while tools.IsFileInUse(picname) do
begin
end;
fsSend := TFileStream.Create(picname, fmOpenRead);
Socket.SendText(ipcode + '|uppic|' + picname + '|' + inttostr(fsSend.Size) + '|' + #13#10);
if fsSend.Size > 0 then
begin
while fsSend.Position < fsSend.Size do
begin
iLength := fsSend.Size;
// - fsSend.Position;
//将数据分段发送
// if iLength > MAX_BUF then
// iLength := MAX_BUF;
GetMem(bufSend, iLength);
try
//读取文件流数据
fsSend.Read(bufSend^, iLength);
//发送长度为iLength的数据
Socket.SendBuf(bufSend^, iLength);
//进度条显示
// Probar.Position := fsSend.Position;
finally
//释放内存
FreeMem(bufSend, iLength);
end;
end;
//Socket.Close();
Timer1.Enabled := True;
end;
end;

(2)java接收

public static boolean fileWriteIn(File file, DataInputStream downStream,long length) {
FileOutputStream fileWrite = null;
try {
fileWrite = new FileOutputStream(file);

byte[] buffer = new byte[size];
int len = 0;
long n = 0;
System.out.print(" >开始接受数据,写入文件 ");
while ((len = downStream.read(buffer, 0, size)) != -1) {
fileWrite.write(buffer, 0, len);
n += len;
System.out.print(".");
if ((n + size) > length) {
int lastLen = (int) (length - n);
n += lastLen;
len = downStream.read(buffer, 0, lastLen);
fileWrite.write(buffer, 0, len);
System.out.print("-");
break;
}
}

System.out.println(" >完成接受,长度为:" + n);

//接收完成==========================
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
return false;
} finally {

try {
fileWrite.close();
} catch (IOException e) {

}
}
}

----------------------------------------------------------------------------

中文传送:

msg:=UTF8Decode(HttpDecode(msg)) ;
// URLdecode(msg);
showmessage(Msg);

destr = URLEncoder.encode(body, "utf-8");
System.out.println("-URL-----------"+destr+"------------");
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式