为什么我编写的局域网聊天程序在单机尚调试通过,而在局网中用户的IP显示不出来?(有代码)(200分)

  • 主题发起人 主题发起人 夏敏
  • 开始时间 开始时间

夏敏

Unregistered / Unconfirmed
GUEST, unregistred user!
请高手解释一下!!
Client端 2004年2月5日星期四
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=13 then //最好加本地IP
begin
if listbox1.itemindex>=0 then
begin
clientsocket1.Socket.SendText('交谈@'+'to'+listBox1.Items.strings[listbox1.itemindex]+'@ '+edit1.Text );
edit1.text:='';
end
else
showmessage('请选择交谈对象');

end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
clientsocket1.Socket.SendText('离开@');
clientsocket1.Active :=false;
clientsocket1.close;
end;

procedure TForm1.Button1Click(Sender: TObject);
var str1:string;
begin
str1:=inputbox('建立连接','请输入IP','');
if trim(str1)<>'' then
begin
clientsocket1.Port:=5555;//server port
clientsocket1.host:=str1;
try
clientsocket1.active:=true;
except
showmessage('连接失败');
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText:='连接成功';
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var str1,name,str2,nei_rong:string;
n,i:integer;
begin
str1:=socket.ReceiveText;
str2:=copy(str1,1,pos('@',str1)-1);
if str2='添加用户' then
begin
name:=copy(str1,pos('@',str1)+1,length(str1)-pos('@',str1));
RichEdit1.Lines.Add(name);
listbox1.Items.Add(name);
end;
if str2='交谈' then //交谈@xmj@hello
begin
str2:=copy(str1,pos('@',str1)+1,length(str1)-pos('@',str1));
name:=copy(str2,1,pos('@',str2)-1);
nei_rong:=copy(str2,pos('@',str2)+1,length(str2)-pos('@',str2));
RichEdit1.Lines.Add(name+'对你说'+nei_rong);
end;
if str2='离开' then
begin
n:= pos('@',str1);
name:=copy(str1,n+1,length(str1)-n);
for i:=0 to listbox1.items.count-1 do
begin
if listbox1.items.strings=name then
listbox1.items.delete(i);
end;
end;
end;

end.
服务器端
procedure TForm1.FormCreate(Sender: TObject);
begin
serversocket1.Active :=true;
RichEdit1.Lines.add(datetostr(date)+' '+timetostr(time)+'==>'+'聊天室服务器启动了');
StatusBar1.SimpleText:='聊天室服务器启动了';
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var s,str,name,nei_rong:string;
i,n:integer;
begin
s:=Socket.ReceiveText; //接收字符串
n:=pos('@',s);
if n>0 then
begin
str:=copy(s,1,n-1);
if str='交谈' then //交谈@toxmj@hello
begin
str:=copy(s,n+1,length(s)-n);
RichEdit1.Lines.add(socket.RemoteHost+str);
name:=copy(str,3,pos('@',str)-3);
nei_rong:=copy(str,pos('@',str)+1,length(str)-pos('@',str));
for i:=0 to listbox1.items.count-1 do //实现私聊
begin
if ServerSocket1.Socket.Connections.RemoteHost=name then
ServerSocket1.Socket.Connections.SendText('交谈@'+socket.RemoteHost+'@'+nei_rong);
end;
end;
{
if str='离开' then //离开@
begin
RichEdit1.Lines.add(socket.RemoteHost+str);
for i:=0 to listbox1.items.count-1 do //listbox1中查找离开用户
begin
if Socket.RemoteHost= listbox1.items.strings then
//error???? ServerSocket1.Socket.RemoteHost
listbox1.Items.delete(i);
end;
for i:=0 to listbox1.items.count-1 do //广播离开用户
begin
ServerSocket1.Socket.Connections.SendText('离开@'+socket.RemoteHost);
end;
end;
}
end;
end;

procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
var i:integer;
begin
listbox1.Items.Add(socket.RemoteHost);
for i:=0 to listbox1.items.count-1 do //广播新进入用户
begin
ServerSocket1.Socket.Connections.SendText('添加用户@'+socket.RemoteHost);
end;
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket); //用户离开触发
var i:integer;
begin
RichEdit1.Lines.add(socket.RemoteHost+'离开了');
for i:=0 to listbox1.items.count-1 do //listbox1中查找离开用户
begin
if Socket.RemoteHost= listbox1.items.strings then
//error???? ServerSocket1.Socket.RemoteHost
listbox1.Items.delete(i);
end;
for i:=0 to listbox1.items.count-1 do //广播离开用户
begin
ServerSocket1.Socket.Connections.SendText('离开@'+socket.RemoteHost);
end;
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var i:integer;
begin
if key=13 then
begin
for i:=0 to listbox1.items.count-1 do //广播离开用户
begin
ServerSocket1.Socket.Connections.SendText('交谈@'+'all'+'@'+edit1.Text );
edit1.text:='';
end;
end;
end;
end.
 
看看我的:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2383933
 
我的程序是正確的,只不過在调试时,机器是一方可以ping另一方,反过来不行导致的,如果,两个机器均通的话,没问题,谢谢!
 
多人接受答案了。
 
后退
顶部