Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

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:

  1. Machine A: Compile and run chat.exe
  2. Machine B: Compile and run chat.exe
  3. Machine A: Type text and it will appear on Machine B
  4. Machine B: Type text and it will appear on Machine A


VB Chat.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\net\TCPUDP\]

Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.