自己研究一下,可能有用
procedure TForm1.FormCreate(Sender: TObject);
begin
Listen1click(nil);
//i:='0';
end;
procedure TForm1.Listen1Click(Sender: TObject);
begin
Listen1.checked:=not listen1.Checked;
if listen1.Checked then
begin
clientsocket1.Active:=false;
serversocket1.active:=true;
statusbar1.Panels[0].text:='监听中……';
end
else
begin
if serversocket1.Active then
serversocket1.Active:=false;
statusbar1.Panels[0].text:='早就开始了!';
end;
end;
procedure TForm1.Connect1Click(Sender: TObject);
begin
if clientsocket1.Active then clientsocket1.active:=false;
if inputquery('连接到服务器','网络地址',server)
then
if length(server)>0 then
with clientsocket1 do begin
host:=server;
active:=true;
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
statusbar1.Panels[0].text:='连接到服务器 :'+socket.remotehost;
memo1.lines.clear;
memo2.lines.Clear;
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.lines.Clear;
memo2.lines.Clear;
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage('成功连接到服务器!');
isserver:=true;
statusbar1.Panels[0].text:='服务器IP:'+socket.remoteaddress;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
//buf1
char;
// data1:integer;
begin
if key=VK_return then
begin
if isserver then
begin
data1:=memo1.Lines[memo1.Lines.count-1];
//buf1:=pointer(str);
//len1:=length(str);
//if messagebox(handle,'',pchar('查询窗口'),mb_IconInformation+mb_OKCancel)=mrOK then
serversocket1.socket.connections[0].SendBuf
(pointer(data1)^,length(data1));
//showmessage(data);
end
else
clientsocket1.Socket.sendbuf(pointer(data1)^,length(data1));
//showmessage('数据已经发送成功!!');
//(memo1.lines[memo1.lines.count-1]);
end;
end;
procedure TForm1.Memo2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
//var str:string;
// buf2
char;
// len2:integer;
begin
if key=VK_return then
begin
if isserver then
begin
data2:= memo2.Lines[memo2.Lines.count-1];
//buf2:=pchar(str);
//len2:=length(str);
clientsocket1.Socket.sendbuf
(pointer(data2)^,length(data2));
//showmessage('数据已经发送成功!!');
//memo2.Lines[memo2.Lines.count-1])
//serversocket1.socket.connections[0].sendtext(
end
else
serversocket1.Socket.connections[0].sendbuf
(pointer(data2)^,length(data2));
//showmessage('数据已经发送成功!!');
//memo2.lines[memo2.lines.count-1]);
end;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
i:integer;
tem: string;
// buf1
char;
// len1:integer;
// p
ointer;
begin
//if messagebox(handle,buf1,pchar('查询窗口'),mb_IconInformation+mb_OKCancel)=mrOK then
//temstr:=buf1;showmessage(temstr);
//setlength(aa,Length(data1));
//socket.ReceiveLength );
socket.Receivebuf
(ClintRecData,Length(data1));
//socket.Receivebuf(pointer(aa)^,length(data1));
//data1:=socket.ReceiveText ;
//showmessage(temstr+'@@@@');
//str:=buf1; // 转为字符串
//showmessage(temstr); //字符指针类型即PChar数据类型,
// memo2.Lines.add(data);
//是一个指向以NULL(不是零)字符结尾的字符(Char)串的指针。
//类型T2的值与类型T1的值赋值兼容是指T1和T2允许赋值操作,即:T1:=T2;
//T1是长字符串类型,T2是PChar类型。
for i:=1 to length(data1) do
begin
//log:=ord(data1
);// 转为ASICII字符
tem:=tem+IntToHex(ClintRecData,2)+#20;
//aa:=0;
end;
memo2.Lines.add(tem);
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var //str:string;
i:integer;
tem:string;
// buf2char;
// len2:integer;
begin
socket.receivebuf
(ServerRecData,length(data2));
//memo1.Lines.add(data);
for i:=1 to length(data2) do
begin
tem:=tem+IntToHex(ServerRecData,2)+#20;
end;
memo1.Lines.add(tem);
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
memo2.Lines.add('不能连接到服务器 :'+server);
Errorcode:=0;
end;