TcpServer1.Close;
TcpServer1.Free;
TcpServer1:= TTcpServer.Create(nil);
TcpServer1.LocalHost :='192.168.0.177';
TcpServer1.LocalPort:= edit7.Text;
TcpServer1.Active :=True;
TcpServer1.Open;这样子也不行:(
TcpServer1.Free;
TcpServer1:= TTcpServer.Create(nil);
TcpServer1.LocalHost :='192.168.0.177';
TcpServer1.LocalPort:= edit7.Text;
TcpServer1.Active :=True;
TcpServer1.Open;这样子也不行:(
什么也没提示!
就是我使用自编的代码测试该端口,显示端口连不上。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Sockets;type
TForm1 = class(TForm)
TcpClient1: TTcpClient;
TcpServer1: TTcpServer;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit4: TEdit;
Edit5: TEdit;
Button2: TButton;
Memo1: TMemo;
Button3: TButton;
Label4: TLabel;
Button4: TButton;
Edit6: TEdit;
Button5: TButton;
Label5: TLabel;
GroupBox1: TGroupBox;
Button6: TButton;
Edit7: TEdit;
Button7: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure Button4Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
btest: boolean;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
label3.Caption :='正在检测中.....';
TcpClient1.RemoteHost := edit1.Text;
TcpClient1.RemotePort := edit2.Text;
try
if TcpClient1.Connect then
begin
edit3.Text :='端口打开';
end
else
edit3.Text :='端口关闭';
finally
TcpClient1.Disconnect;
end;
label3.Caption :='检测完毕';
end;procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
btest:=true;
memo1.Lines.Clear;
for i:=strtoint(edit4.Text) to strtoint(edit5.Text) do
begin
if not btest then exit;
TcpClient1.RemoteHost := edit1.Text;
TcpClient1.RemotePort := inttostr(i);
try
//showmessage(TcpClient1.LookupHostName(edit1.Text));
if TcpClient1.Connect then
begin
memo1.Lines.Add(inttostr(i));
end;
finally
TcpClient1.Disconnect;
end;
edit3.Text := inttostr(i);
application.ProcessMessages;
end;end;procedure TForm1.Button3Click(Sender: TObject);
begin
btest:=False;
end;procedure TForm1.Button5Click(Sender: TObject);
var
i:integer;
begin
btest:=true;
for i:=0 to memo1.Lines.Count-1 do
begin
if not btest then exit;
TcpClient1.RemoteHost := edit1.Text;
TcpClient1.RemotePort := memo1.Lines.Strings[i];
try
if TcpClient1.Connect then
begin
TcpClient1.Sendln(edit6.Text);
end;
finally
TcpClient1.Disconnect;
end;
edit3.Text := inttostr(i);
application.ProcessMessages;
end;end;procedure TForm1.Button6Click(Sender: TObject);
begin
TcpServer1.LocalPort:= edit7.Text;
TcpServer1.Open;
end;procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
begin
memo1.Lines.Add(ClientSocket.RemoteHost + '说:'+ ClientSocket.Receiveln());
end;procedure TForm1.Button4Click(Sender: TObject);
begin
TcpClient1.RemoteHost := edit1.Text;
TcpClient1.RemotePort := edit2.Text;
try
if TcpClient1.Connect then
begin
memo1.Lines.Add('你说:'+edit6.Text);
TcpClient1.Sendln(edit6.Text);
end;
finally
TcpClient1.Disconnect;
end;
end;procedure TForm1.Button7Click(Sender: TObject);
begin
TcpServer1.Close;
end;end.
Left = 192
Top = 107
Width = 548
Height = 407
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 36
Top = 56
Width = 24
Height = 13
Caption = #31471#21475
end
object Label2: TLabel
Left = 48
Top = 28
Width = 10
Height = 13
Caption = 'IP'
end
object Label3: TLabel
Left = 34
Top = 160
Width = 3
Height = 13
end
object Label4: TLabel
Left = 108
Top = 236
Width = 18
Height = 13
Caption = '___'
end
object Label5: TLabel
Left = 8
Top = 92
Width = 48
Height = 13
Caption = #21457#36865#20869#23481
end
object Edit1: TEdit
Left = 80
Top = 20
Width = 121
Height = 21
TabOrder = 0
Text = '192.168.0.132'
end
object Edit2: TEdit
Left = 80
Top = 52
Width = 121
Height = 21
TabOrder = 1
Text = '62136'
end
object Button1: TButton
Left = 220
Top = 48
Width = 75
Height = 25
Caption = #26816#27979
TabOrder = 2
OnClick = Button1Click
end
object Edit3: TEdit
Left = 36
Top = 308
Width = 121
Height = 21
TabOrder = 3
end
object Edit4: TEdit
Left = 36
Top = 236
Width = 69
Height = 21
TabOrder = 4
Text = '1'
end
object Edit5: TEdit
Left = 132
Top = 236
Width = 69
Height = 21
TabOrder = 5
Text = '60000'
end
object Button2: TButton
Left = 216
Top = 236
Width = 75
Height = 25
Caption = #24320#22987
TabOrder = 6
OnClick = Button2Click
end
object Memo1: TMemo
Left = 316
Top = 12
Width = 185
Height = 177
Lines.Strings = (
'Memo1')
TabOrder = 7
end
object Button3: TButton
Left = 308
Top = 236
Width = 75
Height = 25
Caption = #20572#27490
TabOrder = 8
OnClick = Button3Click
end
object Button4: TButton
Left = 220
Top = 88
Width = 75
Height = 25
Caption = #21457#36865
TabOrder = 9
OnClick = Button4Click
end
object Edit6: TEdit
Left = 80
Top = 88
Width = 121
Height = 21
TabOrder = 10
Text = 'Edit6'
end
object Button5: TButton
Left = 220
Top = 132
Width = 75
Height = 25
Caption = #32676#21457
TabOrder = 11
OnClick = Button5Click
end
object GroupBox1: TGroupBox
Left = 208
Top = 296
Width = 325
Height = 65
Caption = #32842#22825#19987#29992
TabOrder = 12
object Button6: TButton
Left = 132
Top = 24
Width = 89
Height = 25
Caption = #25171#24320#31471#21475
TabOrder = 0
OnClick = Button6Click
end
object Edit7: TEdit
Left = 24
Top = 24
Width = 93
Height = 21
TabOrder = 1
Text = '61111'
end
object Button7: TButton
Left = 228
Top = 24
Width = 89
Height = 25
Caption = #20851#38381#31471#21475
TabOrder = 2
OnClick = Button7Click
end
end
object TcpClient1: TTcpClient
Left = 4
Top = 76
end
object TcpServer1: TTcpServer
OnAccept = TcpServer1Accept
Left = 4
Top = 120
end
end
不是吧,这问题如何解决啊?叫客户重新关闭、打开一遍应用程序?!
iErrorCode := GetLastError();
先确认一下出错的具体情况.
因为我写socket的程序时一般直接使用Win32 API
所以你的问题不好说,我觉得应该是因为你在上一次关闭端口的时候
是非正常关的,所以当你再次绑定的时候,应该会出现问题,你可以使用setsockopt(sListen,SOL_SOCKET,SO_REUSEADDR,@iActive,sizeof(iActive))
iActive : Integer;
iActive := 1;
如果是因为此问题,那么这样既可
我是使用TcpServer1.Close;关闭的,然后再用
TcpServer1.LocalPort:= edit7.Text;
TcpServer1.Open;
打开的可是这个端口再也打不开了。
TcpServer1.Free;
TcpServer1:= TTcpServer.Create(nil);
TcpServer1.LocalHost :='192.168.0.177';
TcpServer1.LocalPort:= edit7.Text;
TcpServer1.Active :=True;
TcpServer1.Open;Active 就可以了,为什么还要 open 呢
up