北
北京男人
Unregistered / Unconfirmed
GUEST, unregistred user!
[] 我想通过Socket在局域网传输数据,而且这个数据可能是命令可能是字符串或者其他类型数据,
所以我定义了一个如下记录类型:
TSocketMessage = record
Msg: string[255]
end;
(其他数据域暂时忽略不谈,反正我觉得数据和命令同时传输时用记录类型方便一些)
然后在客户端把要传输的字符串装到这个记录里面,用ClientSocket1.Socket.SendBuf方法发出去,然后在
服务器端用ServerSocket1.Socket.ReceiveBuf方法接收这个记录,并将记录里的字符串显示出来一切正常,以下程序
能正确传输信息。
问题来了,因为我传输的数据量可能很大,所以记录域Msg string[255]太小了,所以我改类型为string或者
PChar类型,结果出现什么都发不过来或者是乱码的问题,请问为什么?以下是程序源码,unit1是服务器端,unit2是客户端,
unit3是公共单元大家需要将'192.168.100.148'这个改成自己机器的IP地址。
unit1程序窗口上放了ServerSocket1,Button1,memo1
unit2程序窗口上放了ClientSocket1,edit1,button1,button2,button3
--------------------------------
unit Unit3;
interface
type
TSocketMessage = record
Msg: string[255]
//我想把此行改成Msg: PChar;结果什么消息都不能发送了
end;
implementation
end.
--------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
Button1: TButton;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit3;
{$R *.DFM}
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
pack: TSocketMessage;
begin
Socket.ReceiveBuf(Pack, SizeOf(Pack));
memo1.lines.add( Pack.Msg );
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Port:= 8090;
ServerSocket1.Open
end;
end.
------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm2 = class(TForm)
ClientSocket1: TClientSocket;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit3;
{$R *.DFM}
procedure TForm2.Button1Click(Sender: TObject);
begin
ClientSocket1.Port:= 8090;
ClientSocket1.Address:= '192.168.100.148';
ClientSocket1.Active:= true;
end;
procedure TForm2.Button2Click(Sender: TObject);
var
pack: TSocketMessage;
begin
pack.Msg := Edit1.Text
//此行改成pack.Msg := PChar(Edit1.Text);
ClientSocket1.Socket.SendBuf(Pack, SizeOf(Pack));
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
if ClientSocket1.Active then
ShowMessage('true')
else
ShowMessage('False');
end;
end.
所以我定义了一个如下记录类型:
TSocketMessage = record
Msg: string[255]
end;
(其他数据域暂时忽略不谈,反正我觉得数据和命令同时传输时用记录类型方便一些)
然后在客户端把要传输的字符串装到这个记录里面,用ClientSocket1.Socket.SendBuf方法发出去,然后在
服务器端用ServerSocket1.Socket.ReceiveBuf方法接收这个记录,并将记录里的字符串显示出来一切正常,以下程序
能正确传输信息。
问题来了,因为我传输的数据量可能很大,所以记录域Msg string[255]太小了,所以我改类型为string或者
PChar类型,结果出现什么都发不过来或者是乱码的问题,请问为什么?以下是程序源码,unit1是服务器端,unit2是客户端,
unit3是公共单元大家需要将'192.168.100.148'这个改成自己机器的IP地址。
unit1程序窗口上放了ServerSocket1,Button1,memo1
unit2程序窗口上放了ClientSocket1,edit1,button1,button2,button3
--------------------------------
unit Unit3;
interface
type
TSocketMessage = record
Msg: string[255]
//我想把此行改成Msg: PChar;结果什么消息都不能发送了
end;
implementation
end.
--------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
Button1: TButton;
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit3;
{$R *.DFM}
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
pack: TSocketMessage;
begin
Socket.ReceiveBuf(Pack, SizeOf(Pack));
memo1.lines.add( Pack.Msg );
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Port:= 8090;
ServerSocket1.Open
end;
end.
------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm2 = class(TForm)
ClientSocket1: TClientSocket;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit3;
{$R *.DFM}
procedure TForm2.Button1Click(Sender: TObject);
begin
ClientSocket1.Port:= 8090;
ClientSocket1.Address:= '192.168.100.148';
ClientSocket1.Active:= true;
end;
procedure TForm2.Button2Click(Sender: TObject);
var
pack: TSocketMessage;
begin
pack.Msg := Edit1.Text
//此行改成pack.Msg := PChar(Edit1.Text);
ClientSocket1.Socket.SendBuf(Pack, SizeOf(Pack));
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
if ClientSocket1.Active then
ShowMessage('true')
else
ShowMessage('False');
end;
end.