当然可以。若在同一机子上,remoteport localport最好别一样,其余没有什么不行的,我今天刚做了一个。
解决方案 »
- 使用TServerSocket控件,如何取得一个已经存在的连接的连接时间。
- 请问调用SoapWebservice如何设置proxy?
- 一个三层结构问题?
- asp网上书店的设计与数据库怎么连接???
- 请帮忙看看有什么错误?
- delphi的前途在哪?
- DELPHI 编程论坛寻求合作伙伴!
- 在三层模式中,当客户端数目达到100个以上时对每个客户的更新数据,将如何同步到其他的客户上?
- 分数不多,敬请笑纳!
- 请问我用Winsock做一个通讯软件,能通过Internet连接另一个已经连接Internet的局域网吗?
- DelPhi 能不能调出VB中标识符自动识别的功能?
- TBitmap怎么设置成256灰度??
Memo2: Status window
Button1: Sends UDP Data
NMUDP1: client and server for sending and receiving dataInsert the following code into Button1's OnClick event:procedure TForm1.Button1Click(Sender: TObject);
var
C: Array [1..3] of Char;
begin
C := 'cat';
NMUDP1.RemoteHost := '127.0.0.1';
NMUDP1.ReportLevel := Status_Basic;
NMUDP1.LocalPort := 6668;
NMUDP1.RemotePort := 6668;
NMUDP1.SendBuffer(C, 3);
end;When Button1 is clicked, C (a variable of an array of characters, 3 to be exact) is filled with the value cat. The RemoteHost property is set to 127.0.0.1, which is the IP address for local host. This could just as easily be the host name or IP address of a remote computer as well. The ReportLevel property is set to Status_Basic, to provide only basic status messages in the OnStatus event. The LocalPort property is set to 6668, so that any data sent to the computer running this application on port 6668 will be received by this component. The remote port property is also set to 6668, for sending data to port 6668 of 127.0.0.1 (basically, sending data to itself). The buffer C is now sent using the SendBuffer method.Insert the followint code into NMUDP1's OnBufferInvalid event:procedure TForm1.NMUDP1BufferInvalid(var handled: Boolean; var Buff: array of Char; var length: Integer);
begin
ShowMessage('Buffer Invalid: Buffer contains no data');
end;When the OnInvalidBuffer event is called, a message is displayed to the user informing them that the buffer being sent is invalid because it contains no data. This error could be corrected by modifying the Buff parameter so it contains the data to be sent, and the length parameter to contain the length of the Buffer. The handled property would then have to be set to TRUE to allow the component to send the data again.Insert the following code into NMUDP1's OnDataReceived event:procedure TForm1.NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);
var
C: array [1..3] of Char;
I: Integer;
begin
if NumberBytes <= 3 then
begin
NMUDP1.ReadBuffer(C, I);
Memo1.Lines.Add(C+': received '+IntToStr(I)+' bytes from '+FromIP+' on port '+IntToStr(Port));
end
else
Memo1.Lines.Add(IntToStr(I)+' bytes incoming, buffer too small');
end;When data is received by NMUDP1, if the NumberBytes parameter is 3 or less (3 or les bytes), the data is read into an array of characters (C) by the ReadBuffer method and displayed in Memo1, along with how many bytes were actually read, the IP address of the computer sending the data (FromIP), and the port the data was sent from (Port parameter). If there are more than 3 bytes, Memo1 is updated to inform the user that the incoming data was too large for the supplied buffer.Insert the following code into NMUDP1's OnDataSend event:procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
Memo2.Lines.Add('Data sent');
end;When data has been successfully sent by pressing Button1, the OnDataSend event is called, and adds a status line stating the data was sent to Memo2.
Insert the following code into NMUDP1's OnStatus event:procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
Memo2.Lines.Add(status);
end;When a status message is received, the OnStatus event adds the current status string to Memo2.
Insert the following code into NMUDP1's OnInvalidHost event:procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
var
S: String;begin
S := NMUDP1.RemoteHost;
if InputQuery('Invalid host', 'Specify valid hostname: ', S) then
begin
NMUDP1.RemoteHost := S;
handled := TRUE;
end;
end;When the host name specified to send data to is an invalid host name or IP address, the OnInvalidHost event is called. In this instance, the InputQuery function gives the user the opportunity to correct the invalid name. If the user clicks the Ok button, the host name entered is set as the host to send data to, and the handled parameter is set to true, which allows the component to attempt the action again. If the user clicks the Cancel button, the host is not changed, and handled remains false, raising an exception.Example Description:
This simple example sends data to itself using a single TNMUDP component that acts as both a client and a server. When Button1 gets clicked, the data is sent to the local machine. The data is then received by the same component, and manipulated accordingly.