新
新手101010
Unregistered / Unconfirmed
GUEST, unregistred user!
我们开发网络程序都用控件,我想用Socket API来做一个,这样可以知道更多的Socket知道,可我是写的程序服务器不能接收数据,出现的错误是:10053
可我不知道怎么改,请大家指点一下
服务器
…………………………………….
s: TSocket;
Err: Integer;
SockAdd: TSockAddrIn;
Buf: array[0..1023] of Char;
Connected: Boolean;
WSA: WSAData;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);//用API侦听
begin
S := Socket(AF_INET, SOCK_STREAM, 0);
if s < 0 then
begin
Memo1.Lines.Add('不能创建Socket');
Exit;
end;
SockAdd.sin_addr.S_addr:=htonl(INADDR_ANY);
SockAdd.sin_family := AF_INET;
SockAdd.sin_port := htons(7890);
Err := Bind(s, SockAdd, SizeOf(TSockAddrIn));
if Err < 0 then
begin
Memo1.Lines.Add('不能绑定端口');
Exit;
end;
Err := Listen(s, 5);
if Err < 0 then
begin
Memo1.Lines.Add('不能侦听端口');
Exit;
end;
Connected := True;
while Connected do//////////////我知道这么做不好,可现在没办法,还不会用消息
begin
FillChar(Buf, 1024, #0 );
Err := Recv(s, Buf, 1024, 0);
if Err < 0 then
begin
Memo1.Lines.Add('出错:' + IntToStr(GetLastError));
Exit;
end
else if Err = 0 then
begin
Memo1.Lines.Add('客户端已经断开连接');
end
else
begin
Memo1.Lines.Add(Buf);
end;
Application.ProcessMessages;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WSAStartup(MakeWord(2, 0), WSA);
Connected := False;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Connected := False;
CloseSocket(s);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WSACleanup();
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Socket.ReceiveText);/// 收到的是乱码
end;
procedure TForm1.BitBtn1Click(Sender: TObject);///////用ServerSocket实现的,我想看看它数据没,可是收到的是乱码
begin
ServerSocket1.Active := true;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
ServerSocket1.Active := False;
end;
客户端
……………………………………
WSAData: WSAData;
s: TSocket;
SerAdd: sockaddr_in;
hp: PHostent;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
Ver: DWord;
begin
Ver := MakeWord(2, 0);
WSAStartup(Ver, WSAData);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WSACleanUp();
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Err: integer;
D: String;
begin
s := Socket(AF_INET, SOCK_STREAM, 0);
if s < 0 then
begin
ShowMessage('创建Socket出错');
Exit;
end;
SerAdd.sin_family := AF_INET;
hp := GetHostByName(Pchar(Edit1.Text));
if hp = nil then
begin
ShowMessage('获取主机IP错误');
Exit;
end;
SerAdd.sin_addr := TInAddr(plongint(hp^.h_addr_list^)^);
SerAdd.sin_port := htons(StrToInt(Edit2.Text));
Err := Connect(s, SerAdd, SizeOf(SerAdd));
if Err < 0 then
begin
ShowMessage('不能连接主机');
Exit;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
CloseSocket(s)
end;
procedure TForm1.Button3Click(Sender: TObject);
var
d: String;
Err: integer;
begin
d := 'This is a single example.';
Err := Send(S, D, Length(D), 0);
if Err < 0 then
begin
ShowMessage('发送数据失败');
Exit;
end;
end;
大家帮忙
可我不知道怎么改,请大家指点一下
服务器
…………………………………….
s: TSocket;
Err: Integer;
SockAdd: TSockAddrIn;
Buf: array[0..1023] of Char;
Connected: Boolean;
WSA: WSAData;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);//用API侦听
begin
S := Socket(AF_INET, SOCK_STREAM, 0);
if s < 0 then
begin
Memo1.Lines.Add('不能创建Socket');
Exit;
end;
SockAdd.sin_addr.S_addr:=htonl(INADDR_ANY);
SockAdd.sin_family := AF_INET;
SockAdd.sin_port := htons(7890);
Err := Bind(s, SockAdd, SizeOf(TSockAddrIn));
if Err < 0 then
begin
Memo1.Lines.Add('不能绑定端口');
Exit;
end;
Err := Listen(s, 5);
if Err < 0 then
begin
Memo1.Lines.Add('不能侦听端口');
Exit;
end;
Connected := True;
while Connected do//////////////我知道这么做不好,可现在没办法,还不会用消息
begin
FillChar(Buf, 1024, #0 );
Err := Recv(s, Buf, 1024, 0);
if Err < 0 then
begin
Memo1.Lines.Add('出错:' + IntToStr(GetLastError));
Exit;
end
else if Err = 0 then
begin
Memo1.Lines.Add('客户端已经断开连接');
end
else
begin
Memo1.Lines.Add(Buf);
end;
Application.ProcessMessages;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WSAStartup(MakeWord(2, 0), WSA);
Connected := False;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Connected := False;
CloseSocket(s);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WSACleanup();
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Socket.ReceiveText);/// 收到的是乱码
end;
procedure TForm1.BitBtn1Click(Sender: TObject);///////用ServerSocket实现的,我想看看它数据没,可是收到的是乱码
begin
ServerSocket1.Active := true;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
ServerSocket1.Active := False;
end;
客户端
……………………………………
WSAData: WSAData;
s: TSocket;
SerAdd: sockaddr_in;
hp: PHostent;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
Ver: DWord;
begin
Ver := MakeWord(2, 0);
WSAStartup(Ver, WSAData);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WSACleanUp();
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Err: integer;
D: String;
begin
s := Socket(AF_INET, SOCK_STREAM, 0);
if s < 0 then
begin
ShowMessage('创建Socket出错');
Exit;
end;
SerAdd.sin_family := AF_INET;
hp := GetHostByName(Pchar(Edit1.Text));
if hp = nil then
begin
ShowMessage('获取主机IP错误');
Exit;
end;
SerAdd.sin_addr := TInAddr(plongint(hp^.h_addr_list^)^);
SerAdd.sin_port := htons(StrToInt(Edit2.Text));
Err := Connect(s, SerAdd, SizeOf(SerAdd));
if Err < 0 then
begin
ShowMessage('不能连接主机');
Exit;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
CloseSocket(s)
end;
procedure TForm1.Button3Click(Sender: TObject);
var
d: String;
Err: integer;
begin
d := 'This is a single example.';
Err := Send(S, D, Length(D), 0);
if Err < 0 then
begin
ShowMessage('发送数据失败');
Exit;
end;
end;
大家帮忙