Are you referring to UDP? Uh, please ignore previous post!! I was thinking
of something else (i.e. doing the nasty with someone else's TCP socket).
Take a look at the UdpClient Class.
Jon
..NET Framework Class Library
UdpClient Class
See Also
UdpClient Members | System.Net.Sock ets Namespace | TcpClient | TCP/UDP |
UdpClient Members (Visual J# Syntax) | Managed Extensions for C++
Programming
Requirements
Namespace: System.Net.Sock ets
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003
family, .NET Compact Framework
Assembly: System (in System.dll)
..NET Framework Security:
SocketPermissio n To establish an outgoing connection or accept an incoming
request.
Language
C#
C++
JScript
Visual Basic
Show All
Provides User Datagram Protocol (UDP) network services.
For a list of all members of this type, see UdpClient Members.
System.Object
System.Net.Sock ets.UdpClient
[Visual Basic]
Public Class UdpClient
Implements IDisposable
[C#]
public class UdpClient : IDisposable
[C++]
public __gc class UdpClient : public IDisposable
[JScript]
public class UdpClient implements IDisposable
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread
safe. Any instance members are not guaranteed to be thread safe.
Remarks
The UdpClient class provides simple methods for sending and receiving
connectionless UDP datagrams in blocking synchronous mode. Because UDP is a
connectionless transport protocol, you do not need to establish a remote
host connection prior to sending and receiving data. You do, however, have
the option of establishing a default remote host in one of the following two
ways:
Create an instance of the UdpClient class using the remote host name and
port number as parameters.
Create an instance of the UdpClient class and then call the Connect method.
You can use any of the send methods provided in the UdpClient to send data
to a remote device. Use the Receive method to receive data from remote
hosts.
Note Do not call Send using a host name or IPEndPoint if you have already
specified a default remote host. If you do, UdpClient will throw an
exception.
UdpClient methods also allow you to send and receive multicasted datagrams.
Use The JoinMulticastGr oup method to subscribe a UdpClient to a multicast
group. Use the DropMulticastGr oup method to unsubscribe a UdpClient from a
multicast group.
Example
[Visual Basic, C#, C++] The following example establishes a UdpClient
connection using the host name
www.contoso.com on port 11000. A small string
message is sent to two separate remote host machines. The Receive method
blocks execution until a message is received. Using the IPEndPoint passed to
Receive, the identity of the responding host is revealed.
[Visual Basic]
' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient()
Try
udpClient.Conne ct("www.contoso .com", 11000)
' Sends a message to the host to which you have connected.
Dim sendBytes As [Byte]() = Encoding.ASCII. GetBytes("Is anybody there?")
udpClient.Send( sendBytes, sendBytes.Lengt h)
' Sends message to a different host using optional hostname and port
parameters.
Dim udpClientB As New UdpClient()
udpClientB.Send (sendBytes, sendBytes.Lengt h, "AlternateHostM achineName",
11000)
'Blocks until a message returns on this socket from a remote host.
Dim RemoteIpEndPoin t As New IPEndPoint(IPAd dress.Any, 0)
' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Recei ve(RemoteIpEndP oint)
Dim returnData As String = Encoding.ASCII. GetString(recei veBytes)
' Which one of these two hosts responded?
Console.WriteLi ne(("This is the message you received " + _
returnData.ToSt ring()))
Console.WriteLi ne(("This message was sent from " + _
RemoteIpEndPoin t.Address.ToStr ing() + _
" on their port number " + _
RemoteIpEndPoin t.Port.ToString ()))
udpClient.Close ()
udpClientB.Clos e()
Catch e As Exception
Console.WriteLi ne(e.ToString() )
End Try
End Sub
[C#]
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient();
try{
udpClient.Conne ct("www.contoso .com", 11000);
// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII. GetBytes("Is anybody there?");
udpClient.Send( sendBytes, sendBytes.Lengt h);
// Sends a message to a different host using optional hostname and
port parameters.
UdpClient udpClientB = new UdpClient();
udpClientB.Send (sendBytes, sendBytes.Lengt h,
"AlternateHostM achineName", 11000);
//IPEndPoint object will allow us to read datagrams sent from any
source.
IPEndPoint RemoteIpEndPoin t = new IPEndPoint(IPAd dress.Any, 0);
// Blocks until a message returns on this socket from a remote
host.
Byte[] receiveBytes = udpClient.Recei ve(ref RemoteIpEndPoin t);
string returnData = Encoding.ASCII. GetString(recei veBytes);
// Uses the IPEndPoint object to determine which of these two hosts
responded.
Console.WriteLi ne("This is the message you received " +
returnData.ToSt ring());
Console.WriteLi ne("This message was sent from " +
RemoteIpEndPoin t.Address.ToStr ing() +
" on their port number " +
RemoteIpEndPoin t.Port.ToString ());
udpClient.Close ();
udpClientB.Clos e();
}
catch (Exception e ) {
Console.WriteLi ne(e.ToString() );
}
[C++]
// With this constructor the local port number is arbitrarily assigned.
UdpClient __gc *udpClient = new UdpClient();
try {
udpClient->Connect(S"host .contoso.com", 11000);
// Send message to the host to which you have connected.
Byte sendBytes __gc[] = Encoding::ASCII->GetBytes(S"I s anybody there?");
udpClient->Send(sendBytes , sendBytes->Length);
// Send message to a different host using optional hostname and port
parameters.
UdpClient __gc *udpClientB = new UdpClient();
udpClientB->Send(sendBytes , sendBytes->Length,
S"AlternateHost MachineName", 11000);
//IPEndPoint object will allow us to read datagrams sent from any
source.
IPEndPoint __gc *RemoteIpEndPoi nt = new IPEndPoint(IPAd dress::Any, 0);
// Block until a message returns on this socket from a remote host.
Byte receiveBytes __gc[] = udpClient->Receive(&Remot eIpEndPoint);
String __gc *returnData = Encoding::ASCII->GetString(rece iveBytes);
// Use the IPEndPoint object to determine which of these two hosts
responded.
Console::WriteL ine(String::Con cat(S"This is the message you received ",
returnData->ToString())) ;
Console::WriteL ine(String::Con cat(S"This message was sent from ",
RemoteIpEndPoin t->Address->ToString(),
S" on their port number ",
__box(RemoteIpE ndPoint->Port)->ToString())) ;
udpClient->Close();
udpClientB->Close();
}
catch (Exception __gc *e ) {
Console::WriteL ine(e->ToString());
}
[JScript] No example is available for JScript. To view a Visual Basic, C#,
or C++ example, click the Language Filter button in the upper-left corner
of the page.
Requirements
Namespace: System.Net.Sock ets
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003
family, .NET Compact Framework
Assembly: System (in System.dll)
..NET Framework Security:
SocketPermissio n To establish an outgoing connection or accept an incoming
request.
See Also
UdpClient Members | System.Net.Sock ets Namespace | TcpClient | TCP/UDP |
UdpClient Members (Visual J# Syntax) | Managed Extensions for C++
Programming
Syntax based on .NET Framework version 1.1.
Documentation version 1.1.1.