UDPChat Sample

Sample Overview

This sample illustrates using the UdpClient class to create a chat client.

This sample is a functional chat client. It uses the user datagram protocol (UDP) to send and receive chat messages with other users that are also using the client. The program illustrates use of the UdpClient class and threading.

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 also easy. The UdpClient class includes a function called Send, which sends out a byte array to every class that is listening on the Multicast group.

Sample Source and Build Output Locations

The sample source is found in sscli\samples\howto\net\udpchat. 

The source file is:

The build output location is %TARGETCOMPLUS%\samples\howto\net\udpchat.  The output file is an executable assembly named udpchat.exe.

Building the Sample

All samples are built from the buildall script. 

You can also build all the samples by switching to the root of the sample directory, sscli\samples, and typing build -c.

You can build this specific sample by switching to the sample directory and typing build -c.

Running the Sample

These steps require that the Shared Source CLI (SSCLI) be already built and functional.

  1. Run env.bat or source the env.csh or env.sh script files depending on your platform.
  2. Switch to the %TARGETCOMPLUS%\samples\howto\net\udpchat directory.
  3. Type the following command:

    clix udpchat.exe

  4. You will need to run another instance of the udpchat application on another system in order to test the functionality.

Copyright (c) 2002 Microsoft Corporation. All rights reserved.