SocketAPI编程改错 ( 积分: 100 )

  • 主题发起人 主题发起人 水刃
  • 开始时间 开始时间

水刃

Unregistered / Unconfirmed
GUEST, unregistred user!
程序虽然能运行,但是访问本地192.168.101.2:2000连接失败,请帮忙修改,多谢!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
memo1: TMemo;
Button3: TButton;
Button4: TButton;
Memo2: TMemo;
Edit2: TEdit;
Label2: TLabel;

procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
aWSAData:TWSAData;
wVersion:word;
SockRaw:Tsocket;
addr:u_long;
saDest:Tsockaddr;
remote:Tsockaddr;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
wVersion:=makeword(2,2);
memo1.Text:='';
memo2.Text:='';
if WSAStartup(wVersion,aWSAData)<>0 then
begin
memo1.Lines.Add('无法建立WINSOCK动态链接库');
exit;
end
else
begin
if WSAStartup(wVersion,aWSAData)=0 then
memo1.Lines.Add('初始化成功');
exit;

end;
end;
//创建套接字,绑定端口并连接
procedure TForm1.Button1Click(Sender: TObject);
begin
SockRaw:=socket(PF_INET,SOCK_RAW,IPPROTO_iP);
SaDest.sin_family :=PF_INET;
if (SockRaw=INVALID_SOCKET) then
begin
memo1.Lines.Add(' 套接字创建失败') ;
end
else memo1.Lines.Add(' 套接字创建成功') ;
begin
SaDest.sin_port:=2000;//SaDest.sin_port:=htons(strtoint(trim(edit2.Text)));
//addr:=inet_addr(pchar(trim(edit1.Text)));
SaDest.sin_addr.S_addr:=inaddr_any;
end;
if( bind(SockRaw,SaDest, sizeof(SaDest))<>0) and (listen(SockRaw,somaxconn)<>0)then
begin
memo1.Lines.Add('地址绑定失败');
end
else
begin memo1.Lines.Add('地址绑定成功,建立连接') ;
remote.sin_port:=htons(strtoint(trim(edit2.Text)));
addr:=inet_addr(pchar(trim(edit1.Text)));
remote.sin_addr.S_addr:=addr;
end;
if connect(SockRaw,remote, sizeof(remote))<>0 then
begin
memo1.Lines.Add('建立连接失败');
closesocket(SockRaw);
end
else begin memo1.Lines.Add('建立连接成功') ;
end;
end;
end.
 
程序虽然能运行,但是访问本地192.168.101.2:2000连接失败,请帮忙修改,多谢!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
memo1: TMemo;
Button3: TButton;
Button4: TButton;
Memo2: TMemo;
Edit2: TEdit;
Label2: TLabel;

procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
aWSAData:TWSAData;
wVersion:word;
SockRaw:Tsocket;
addr:u_long;
saDest:Tsockaddr;
remote:Tsockaddr;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
wVersion:=makeword(2,2);
memo1.Text:='';
memo2.Text:='';
if WSAStartup(wVersion,aWSAData)<>0 then
begin
memo1.Lines.Add('无法建立WINSOCK动态链接库');
exit;
end
else
begin
if WSAStartup(wVersion,aWSAData)=0 then
memo1.Lines.Add('初始化成功');
exit;

end;
end;
//创建套接字,绑定端口并连接
procedure TForm1.Button1Click(Sender: TObject);
begin
SockRaw:=socket(PF_INET,SOCK_RAW,IPPROTO_iP);
SaDest.sin_family :=PF_INET;
if (SockRaw=INVALID_SOCKET) then
begin
memo1.Lines.Add(' 套接字创建失败') ;
end
else memo1.Lines.Add(' 套接字创建成功') ;
begin
SaDest.sin_port:=2000;//SaDest.sin_port:=htons(strtoint(trim(edit2.Text)));
//addr:=inet_addr(pchar(trim(edit1.Text)));
SaDest.sin_addr.S_addr:=inaddr_any;
end;
if( bind(SockRaw,SaDest, sizeof(SaDest))<>0) and (listen(SockRaw,somaxconn)<>0)then
begin
memo1.Lines.Add('地址绑定失败');
end
else
begin memo1.Lines.Add('地址绑定成功,建立连接') ;
remote.sin_port:=htons(strtoint(trim(edit2.Text)));
addr:=inet_addr(pchar(trim(edit1.Text)));
remote.sin_addr.S_addr:=addr;
end;
if connect(SockRaw,remote, sizeof(remote))<>0 then
begin
memo1.Lines.Add('建立连接失败');
closesocket(SockRaw);
end
else begin memo1.Lines.Add('建立连接成功') ;
end;
end;
end.
 
SOCK_RAW,原始套接口。你使用了原始套接口,使用原始套接口的时候是不支持listen函数的,其实你在:
if( bind(SockRaw,SaDest, sizeof(SaDest))<>0) and (listen(SockRaw,somaxconn)<>0)then
这里的时候就肯定失败了,但是你故意把这两个函数放在一起,中间用and,当然是忽略了这个错误,最终这个错误到了connect的时候终于纸包不住火,还是显示出来了!
SOCK_RAW和SOCK_DGRAM不支持listen!
 
以下是我写好的程序,你看看吧:
unit ClientUnit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ComCtrls, WinSock, ExtCtrls, ScktComp;

type
TFormMain = class(TForm)
StatusBar: TStatusBar;
Panel1: TPanel;
EditPort: TEdit;
Label2: TLabel;
Label1: TLabel;
EditIP: TEdit;
Panel2: TPanel;
BtnConnent: TButton;
BtnSend: TButton;
BtnStop: TButton;
BtnExit: TButton;
ClientSocket: TClientSocket;
ServerSocket: TServerSocket;
Btn1: TButton;
Btn2: TButton;
procedure BtnExitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BtnConnentClick(Sender: TObject);
procedure BtnSendClick(Sender: TObject);
procedure BtnStopClick(Sender: TObject);
procedure ClientSocketWrite(Sender: TObject;
Socket: TCustomWinSocket);
procedure Btn1Click(Sender: TObject);
private
Client:TSocket;
{ Private declarations }
public
StopTrans:Boolean; //停止发送
InTrans:Boolean; //正在发送
{ Public declarations }
procedure TransData(DataNum:String);
protected
//IsServer:Boolean;
IP:string;
end;
const BlockLen=1024*4; //每次发送的数据量

var
FormMain: TFormMain;
Server : String;

implementation

{$R *.dfm}

procedure TFormMain.BtnExitClick(Sender: TObject);
begin
close;
end;
//创建窗体初始化数据
procedure TFormMain.FormCreate(Sender: TObject);
var
aWsaData:TWsaData;
begin
if WsaStartUp($0101,aWsaData)<>0 then
MessageBox(Handle, aWSAData.szDescription,'WinSock 态链接库版本', MB_OK);
ClientSocket.Active:=false;
ServerSocket.Active:=true;
if ServerSocket.Active then
ServerSocket.Active:=false;
end;
//关闭窗体时检测数据传输
procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
tim:TdateTime;
begin
if Intrans then
MessageBox(Handle,'正在传输,是否停止?','提示',MB_YESNO);
if WsaCleanUp<>0 then
MessageBox(Handle,'清除数据','提示!',MB_OK)
else
//MessageBox(Handle,' 清除成功','提示',MB_OK);
end;
//连接服务器
procedure TFormMain.BtnConnentClick(Sender: TObject);
var
ca:SockAddR_IN;
Hostaddr:u_long;
begin
//创建
Client := Socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
if Client=INVALID_SOCKET then
begin
StatusBar.Panels[0].Text:='连接服务器失败!';
exit;
end;
ca.sin_family:=PF_INET;
ca.sin_port:=htons(StrToInt(Trim(editPort.Text)));
HostAddr:=inet_addr(Pchar(Trim(editIp.Text)));
//判断IP合法性
if (hostaddr=-1) then
begin
StatusBar.Panels[0].Text:='服务器IP地址'+editIp.Text+'错误,请核实后重新输入';
exit
end
else
ca.sin_addr.S_addr:=HostAddr;
//连接
if connect(Client,ca,SizeOf(ca))<>0 then
begin
StatusBar.Panels[0].Text:='连接服务器端Socket失败...';
exit;
end
else
StatusBar.Panels[0].Text:='连接服务器端成功';
end;
//传输数据过程
procedure TFormMain.TransData(DataNum:String);
var
Ftrans:file of byte;
Flen:Integer;
BlockNum,RemainLen:integer;
BlockBuf:array[0..BlockLen-1] of byte; //
i:integer;
SendLen:integer; //数据长度
begin
AssignFile(Ftrans,DataNum);
Reset(Ftrans);
Flen:=Filesize(Ftrans);
BlockNum:=Flen div BlockLen;
//ProgressBar.Max:=1+BlockNum;
RemainLen:=Flen mod BlockLen;
StopTrans:=false;
Intrans:=true;
SendLen:=1;
for i:=0 to BlockNum-1 do
begin
if (StopTrans) or (SendLen<=0) then
Break;
BlockRead(Ftrans,BlockBuf[0],BlockLen);
SendLen:=Send(Client,BlockBuf,BlockLen,0);
//ProgressBar.Position:=i;
Application.ProcessMessages;
end;
if StopTrans then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.Panels[0].Text:= '传输停止!';
MessageBox(Handle, '停止传输!', '提示', MB_OK);
//ProgressBar.Position := 0;
Exit;
end;
if (SendLen<=0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.Panels[0].Text:= '传输异常';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
//ProgressBar.Position := 0;
Exit;
end;
if RemainLen>0 then
begin
BlockRead(Ftrans, BlockBuf[0], RemainLen);
SendLen := send(Client, BlockBuf, RemainLen, 0);
if (SendLen <= 0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.Panels[0].Text:= '传输异常';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
//ProgressBar.Position := 0;
Exit;
end;
end;
//ProgressBar.Position := ProgressBar.Max;
CloseFile(Ftrans);
InTrans := False;
StatusBar.Panels[0].Text:= '传输完成';
MessageBox(Handle, '传输完成!','提示',MB_OK);
//ProgressBar.Position := 0;
end;
procedure TFormMain.BtnSendClick(Sender: TObject);
begin
{if (OpenDfile.Execute) and (FileExists(OpenDfile.FileName)) then}
//TransData(OpenDfile.FileName);
end;
//停止传送
procedure TFormMain.BtnStopClick(Sender: TObject);
begin
//StopTrans := True;
end;
procedure TFormMain.ClientSocketWrite(Sender: TObject;
Socket: TCustomWinSocket);
var
St:TmemoryStream;
begin
st:=Tmemorystream.Create;
clientsocket.Socket.SendStream(st);
end;

procedure TFormMain.Btn1Click(Sender: TObject);
begin
try
if ClientSocket.Active then ClientSocket.Active:=false; //如果是true则设为false
if InputQuery('目的地址','目的IP地址:',server) then
if length(server)<0 then
begin
MessageBox(handle,'输入不合法,请重新输入...','提示',MB_ICONERROR);
exit;
end
else
begin
if length(server)>0 then
with ClientSocket do
begin
Host:=server;
Active:=true;
end;
end;
except
//
end;
end;
end.
 

Similar threads

I
回复
0
查看
599
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
536
import
I
I
回复
0
查看
414
import
I
后退
顶部