Socket问题(100分)

  • 主题发起人 主题发起人 zbr
  • 开始时间 开始时间
Z

zbr

Unregistered / Unconfirmed
GUEST, unregistred user!
type
pRUserInfo = ^RUserInfo;
RUserInfo = record
AUserName: string;
SignSocket: TSocket;
SignInfo: string;
end;

const
SignRegister = 'AAAA';
//申请注册
SignSuccess = 'BBBB';//注册成功
SignRefuse = 'CCCC';//注册拒绝
procedure TForm1.Button2Click(Sender: TObject);
var
SocketInfo: pRUserInfo;
begin
New(SocketInfo);
try
SocketInfo.AUserName:= 'Socket1';
SocketInfo.SignSocket:= ClientSocket1.Socket.Handle;
SocketInfo.SignInfo:= SignRegister;
ClientSocket1.Socket.SendBuf(SocketInfo^,SizeOf(SocketInfo) + 1);
finally
Dispose(SocketInfo);
end;
end;

注:ClientSocket1为TClientSocket
这样写的代码,在服务端接收时,AUserName为乱码,SignInfo没有数据,请问代码哪里有问题,该如何写?谢谢! 顶者有分 。
 
pRUserInfo = ^RUserInfo;
RUserInfo = record
AUserName: string[255];
// 就是这儿了,必须有数字,不一定是255
SignSocket: TSocket;
SignInfo: string[255];
// 就是这儿了,必须有数字,不一定是255
end;
 
wql: 请问为什么 ?
 
因为Record中定义string类型只是定义了一个指针而已。[:D]
路过。。。
 
楼上说的对,string只是字符指针。按楼主的要求传字符内容,得定义成字符数组才行。
 
对,例如记录里面是PChar,那么接受时有可能不仅仅是乱吗,而可能是
Access viasa Address Error!(累世提示,呵呵)了!
因为PChar根本没有分配内存空间!
哈哈!
 
type
pRUserInfo = ^RUserInfo;
RUserInfo = record
AUserName: string[255];
SignSocket: TSocket;
SignInfo: string[255];
end;

const
SignRegister = 'AAAA';
//申请注册
SignSuccess = 'BBBB';//注册成功
SignRefuse = 'CCCC';//注册拒绝
procedure TForm1.Button2Click(Sender: TObject);
var
SocketInfo: pRUserInfo;
begin
New(SocketInfo);
try
SocketInfo.AUserName:= 'Socket1';
SocketInfo.SignSocket:= ClientSocket1.Socket.Handle;
SocketInfo.SignInfo:= SignRegister;
ClientSocket1.Socket.SendBuf(SocketInfo^,SizeOf(SocketInfo) + 1);
finally
Dispose(SocketInfo);
end;
end;

注:ClientSocket1为TClientSocket
那么,请问,我这样写是否就可以了呢?Button2Click里的代码是否有问题?
好像服务端还是接收乱码。
 
请各位高手多多帮忙。谢谢。
 
既然
var
SocketInfo: pRUserInfo
那么
SocketInfo.AUserName:= 'Socket1';
SocketInfo.SignSocket:= ClientSocket1.Socket.Handle;
SocketInfo.SignInfo:= SignRegister;
上面的句子是否该写成
SocketInfo^.AUserName:= 'Socket1';
SocketInfo^.SignSocket:= ClientSocket1.Socket.Handle;
SocketInfo^.SignInfo:= SignRegister;
 
而且:
ClientSocket1.Socket.SendBuf(SocketInfo^,SizeOf(SocketInfo));
ServerSocket1.Socket.ReadBuf(SocketInfo^,SizeOf(SocketInfo));
 
AUserName: string[255];
String 这种类型有问题,应用ARRAY[0..254] OF CHAR
 
在网上传输大多用array[0..MaxBuf] of char
还有!我不知为什么你发送那儿。长度为什么长度要加一个1呢?
你可以在没有发送之关你检查一下。那发的那个结构内数据是否正确!
 
内存中
AUserName: string[255];
255+1个字节(长度:字符)
ARRAY[0..254] OF CHAR : 255个字节
指定了长度,用String和Array无任何区别!
 
客户端:
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
Temp: pRUserInfo;
ALength: Integer;
begin
New(Temp);
try
ALength:= Socket.ReceiveLength;
GetMem(Temp,ALength);
Socket.ReceiveBuf(Temp^,SizeOf(Temp));
if Temp^.SignInfo = SignSuccess then
Memo1.Lines.Add(Temp^.AUserName + ':申请注册成功!')
else
if Temp^.SignInfo = SignRefuse then
Memo1.Lines.Add(Temp^.AUserName + ':申请注册被拒绝!')
finally
Dispose(Temp)
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
SocketInfo: pRUserInfo;
begin
New(SocketInfo);
try
SocketInfo^.AUserName:= 'Socket1';
SocketInfo^.SignSocket:= ClientSocket1.Socket.Handle;
SocketInfo^.SignInfo:= SignRegister;
ClientSocket1.Socket.SendBuf(SocketInfo^,SizeOf(SocketInfo));
finally
Dispose(SocketInfo);
end;
end;
服务端:
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
pUser,Temp: PRUserInfo;
ILength: Integer;
begin

New(Temp);
try
ILength:= Socket.ReceiveLength;
GetMem(pUser,ILength);
Socket.ReceiveBuf(pUser^,SizeOf(pUser));
if pUser^.SignInfo = SignRegister then
begin
if pUser^.AUserName = 'Socket1' then
begin
Temp^.AUserName:= pUser^.AUserName;
Temp^.SignInfo:= SignSuccess;
Temp^.SignSocket:= pUser^.SignSocket;
Memo1.Lines.Add(pUser^.AUserName + ':申请注册通过!')
end
else
begin
Temp^.AUserName:= pUser^.AUserName;
Temp^.SignInfo:= SignRefuse;
Temp^.SignSocket:= pUser^.SignSocket;
Memo1.Lines.Add(pUser^.AUserName + ':申请注册被拒绝!')
end;
send(pUser.SignSocket,Temp,SizeOf(Temp),0)
end;
finally
// FreeMem(pUser,ILength);
end;
end;

这样写接收到的数据还是不正确 。
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, WinSock, StdCtrls;
type
TSignType = (SignRegister, SignSuccess, SignRefuse);
type
PUserInfo = packed record
AUserName: array[0..254] of Char;
SignSocket: HWND;
SignInfo: TSignType;
end;

type
TForm1 = class(TForm)
Button1: TButton;
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Memo1: TMemo;
procedure ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
Temp: PUserInfo;
ALength: Integer;
begin
if Socket.ReceiveLength = Sizeof(Temp) then
begin
Socket.ReceiveBuf(Temp, ALength);
if Temp.SignInfo = SignSuccess then
Memo1.Lines.Add(Temp.AUserName + ':申请注册成功!')
else
if Temp.SignInfo = SignRefuse then
Memo1.Lines.Add(Temp.AUserName + ':申请注册被拒绝!')
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
temp: PUserInfo;
SocketName: string;
begin
temp.AUserName := 'Socket1';
temp.SignSocket := ClientSocket1.Socket.Handle;
temp.SignInfo := SignRegister;
ClientSocket1.Socket.SendBuf(temp, SizeOf(temp));
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
pUser, temp: PUserInfo;
i: Integer;
begin
if Socket.ReceiveBuf(pUser, SizeOf(pUser)) = Sizeof(pUser) then
begin
if pUser.SignInfo = SignRegister then
begin
if pUser.AUserName = 'Socket1' then
begin
Temp.AUserName := pUser.AUserName;
Temp.SignInfo := SignSuccess;
Temp.SignSocket := pUser.SignSocket;
Memo1.Lines.Add(pUser.AUserName + ':申请注册通过!');
end else
begin
Temp.AUserName := pUser.AUserName;
Temp.SignInfo := SignRefuse;
Temp.SignSocket := pUser.SignSocket;
Memo1.Lines.Add(pUser.AUserName + ':申请注册被拒绝!')
end;
Socket.SendBuf(Temp, SizeOf(Temp));
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ClientSocket1.Active := True;
ServerSocket1.Active := True;
end;

end.
 
多人接受答案了。
 
后退
顶部