Y
yhnlzzg
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠,小弟有个小小小问题?不知哪位哥儿们知道怎么做?
这是一个有关SOCKET的问题(即移植问题),简单代码如下:
(我实在是不知错在哪里?请各位大哥指点指点!可不能用控件!!!)
unit socksc;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, winsock, StdCtrls;
type
TForm1 = class(TForm)
sockserver: TButton;
sockclient: TButton;
Memo1: TMemo;
Memo2: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure sockserverClick(Sender: TObject);
procedure sockclientClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.sockserverClick(Sender: TObject);
var
fsock:Tsocket;
sockfd:Tsocket;
addr:Tsockaddr;
addrcSockAddr;
addrclenInteger;
rb,rl,rlog:integer;
str,str1:string;
begin
fsock:=socket(AF_INET,SOCK_STREAM,0);
memo1.Lines.Add('socket return: '+inttostr(fsock));
addr.sin_family:=AF_INET;
addr.sin_port:=2222;
addr.sin_addr.S_addr:=htonl(INADDR_ANY);
rb:=bind(fsock,addr,sizeof(addr));
memo1.Lines.Add('bind return: '+inttostr(rb));
rl:=listen(fsock,10);
memo1.Lines.Add('listen return: '+inttostr(rl));
addrclen^:=sizeof(addrc);
while (true) do
begin
sockfd:=accept(fsock,addrc,addrclen);
memo1.Lines.Add(inttostr(sockfd));
if sockfd>=0 then
begin
rlog:=recv(sockfd,str,sizeof(str),0);
if rlog<0 then continue;
memo1.Lines.Add(inttostr(rlog));
str1:=str+' ok!';
memo1.Lines.Add(inttostr(send(sockfd,str1,sizeof(str1),0)));
end
else
begin
memo1.Lines.Add('error!');
break;
end;
end;
closesocket(fsock);
end;
procedure TForm1.sockclientClick(Sender: TObject);
var
csock:Tsocket;
sockcd:Tsocket;
addrc:Tsockaddr;
rcon:integer;
strc,strc1:string;
begin
csock:=socket(AF_INET,SOCK_STREAM,0);
memo2.Lines.Add('socket return: '+inttostr(csock));
addrc.sin_family:=AF_INET;
addrc.sin_port:=2222;
addrc.sin_addr.S_addr :=inet_addr('192.168.1.23');
rcon:=connect(csock,addrc,sizeof(addrc));
memo2.Lines.Add('connect :'+inttostr(rcon));
strc:=edit1.text;
send(csock,strc,sizeof(strc),0);
recv(csock,strc1,sizeof(strc1),0);
edit2.text:=strc1;
closesocket(csock);
end;
end.
这是一个有关SOCKET的问题(即移植问题),简单代码如下:
(我实在是不知错在哪里?请各位大哥指点指点!可不能用控件!!!)
unit socksc;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, winsock, StdCtrls;
type
TForm1 = class(TForm)
sockserver: TButton;
sockclient: TButton;
Memo1: TMemo;
Memo2: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure sockserverClick(Sender: TObject);
procedure sockclientClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.sockserverClick(Sender: TObject);
var
fsock:Tsocket;
sockfd:Tsocket;
addr:Tsockaddr;
addrcSockAddr;
addrclenInteger;
rb,rl,rlog:integer;
str,str1:string;
begin
fsock:=socket(AF_INET,SOCK_STREAM,0);
memo1.Lines.Add('socket return: '+inttostr(fsock));
addr.sin_family:=AF_INET;
addr.sin_port:=2222;
addr.sin_addr.S_addr:=htonl(INADDR_ANY);
rb:=bind(fsock,addr,sizeof(addr));
memo1.Lines.Add('bind return: '+inttostr(rb));
rl:=listen(fsock,10);
memo1.Lines.Add('listen return: '+inttostr(rl));
addrclen^:=sizeof(addrc);
while (true) do
begin
sockfd:=accept(fsock,addrc,addrclen);
memo1.Lines.Add(inttostr(sockfd));
if sockfd>=0 then
begin
rlog:=recv(sockfd,str,sizeof(str),0);
if rlog<0 then continue;
memo1.Lines.Add(inttostr(rlog));
str1:=str+' ok!';
memo1.Lines.Add(inttostr(send(sockfd,str1,sizeof(str1),0)));
end
else
begin
memo1.Lines.Add('error!');
break;
end;
end;
closesocket(fsock);
end;
procedure TForm1.sockclientClick(Sender: TObject);
var
csock:Tsocket;
sockcd:Tsocket;
addrc:Tsockaddr;
rcon:integer;
strc,strc1:string;
begin
csock:=socket(AF_INET,SOCK_STREAM,0);
memo2.Lines.Add('socket return: '+inttostr(csock));
addrc.sin_family:=AF_INET;
addrc.sin_port:=2222;
addrc.sin_addr.S_addr :=inet_addr('192.168.1.23');
rcon:=connect(csock,addrc,sizeof(addrc));
memo2.Lines.Add('connect :'+inttostr(rcon));
strc:=edit1.text;
send(csock,strc,sizeof(strc),0);
recv(csock,strc1,sizeof(strc1),0);
edit2.text:=strc1;
closesocket(csock);
end;
end.