如何实现,通过socket向指定服务器发送一个字符串?(100分)

P

popboy

Unregistered / Unconfirmed
GUEST, unregistred user!
比如向服务器 192.168.1.3:14001
发送 "[begin] ID:432;Passwd:xxxx[end]"

用delphi如何实现??
谢谢
 
TCP or UDP?
 
procedure Tfrm_p2p.mnuitm_infomationClick(Sender: TObject);
var
i: integer;
sendstr: string;
begin
sendstr := InputBox('P2P即时消息', '请输入要发送的信息', 'I from ' +
clntSckt.Address);
sendstr := 'i' + sendstr;
for i := 0 to svrsckt.Socket.ActiveConnections - 1 do
begin
if svrsckt.Socket.Connections.LocalAddress =
lv_watch.ItemFocused.subitems[0] then
break;
end;
svrsckt.Socket.Connections.SendText(sendstr);
svr_state := stWait;
end;
 
帮你UP!
 
udp很方便的阿,添加tnump控件。。。。
 
不过用UDP好像不建立链接,不安全,如果要安全一点的话,用TCP/IP协议,建立链接
后才开始发送数据。
 
唉!闲着也是闲着!

在Form上加一个 tnmudp1,俩个port都为5558,Edit1,Edit2为udp,服务端ip,发送得信息,
加button1为发送!

procedure tform1.button1click();
var
tms:string;
tmarr:packed array [1..255] of char;
tml:word;
begin
setlength(tms,119);
tms:=trim(edit2.text);
if (tms='') then
begin
showmessage('No information!');
edit2.setfocus;
exit;
end;
if (trim(edit1.text)='') then
begin
showmessage('input server ip!');
edit2.setfocus;
exit;
end;
tml:=length(tms);
move(tms[1],tmarr[3],tml);//发送得字符
move(tml,tmarr[1],2);//字符串长度
nmudp1.remotehost:=trim(edit1.text);
nmudp1.sendbuffer(tmarr,sizeof(tmarr));
end;
procedure tform1.nmudp1datarecive();//就是接受事件,单词忘了
var
tmarr:packed array [1..255] of char;
tmp:pchar;
begin
fillchar(tmarr,length(tmarr),0);
nmudp1.readbuffer(TmArr[1],Recivedlength);//就是接受长度
tmp:=@tmarr[3]
showmessage('收到字符串:'+trim(string(tmp)));
end;

编译运行,在Edit1处输入服务端的IP地址,也可以是本机的IP,在Edit2处输入
要发送的信息,点Button1按钮即可。

若为两台电脑,可以互相输入对方的ip地址而改为聊天程序!
 
顶部