How Do I...Make a chat client with UDPClient?
This sample illustrates using the UDPClient class to create a chat client.
This sample is a complete and fully functional chat client. It uses UDP to
send and receive chat messages with other users that are also using the client.
The program illustrates many important Microsoft .NET Framework concepts,
but the most important
(for this example) is the use of the UDPClient class.
THe UDPCLient object is initialized with a constructor call that sets the
port that the object will listen on. The object then joins a Multicast group.
This code can be found in the Initialize function of the sample code.
Once initialized, the UDPClient listens for incoming messages with the
Receive function, which can be found in the Listener function of
the sample code. The Receive function returns an array of bytes,
containing the data sent. This data can be converted into a string with
the ASCII object GetString function.
Sending data is just as easy. The UDPClient includes a function called
Send which sends out a byte array to every class that is listening
on the Multicast group.
To run this sample, you will need two machines:
- Machine A: Compile and run chat.exe
- Machine B: Compile and run chat.exe
- Machine A: Type text and it will appear on Machine B
- Machine B: Type text and it will appear on Machine A
Example
C# Chat.exe
[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\net\TCPUDP\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|