The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

WinTalk Sample (Introduces Windows Forms and Sockets) 

Download sample

This sample offers a brief introduction to Windows Forms and Sockets including other Framework Class Library (FCL) topics. The sample is a very simple Sockets Chat application.

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

Building and Running the Sample

To open the sample file in Solution Explorer

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

    Make sure that the Show extracted files check box is selected, and click Finish.

  4. Double-click the sample's .sln file.

    The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.

To build this sample

To run this sample

  1. Type WinTalk.exe /? in the command line for a complete list of command line options for the utility.

  2. To try the sample where the client and the server are on the same machine, simply run the application twice with no parameters.

  3. When specifying a port number, verify that port is not already in use by another program; otherwise, the sample will not run correctly. Try using a high port number between 5001 and 5150.

  4. For the sample to function properly, your Path, Include, and Lib environment variables must be set correctly. You can set these variables by running vsVars.bat, located in the <SDKRoot>\Bin directory. vsVars.bat must be executed in every command shell.

Classes and Technologies in the Sample

The following classes and technologies are used in this sample.

Windows Forms
Class/Technology Description


Used by the sample application to implement a message pump.


Control type that handles splitter semantics. Used in the application to separate the send and receive text frames.


Allows for flexible organizing, docking, and anchoring of controls on a form. In this case, the panel contains the two text boxes and the splitter. The status control, on the other hand, is a peer to the panel.


Implements two edit controls in the sample. One is read-only for receiving text.


Used to display static status information in the sample.


Used by the sample to relay information about errors and shutdown situations.

Class/Technology Description


Used by the sample to encapsulate an IP address and port number.


Provides useful IPAddress-related logic.


Used to get an IP address from a DNS name.


Most networking implemented in managed code is done using an abstraction such as XML Web services. However, it is possible to implement socket code directly, and this sample uses the Socket class to do so.


Derived from stream, and uses a socket as its underlying device. This makes it very useful for streaming data over a socket. You can use the NetworkStream type just like you would use any other stream type, such as FileStream.

Class/Technology Description


Used to write to the network stream that represents a socket.


Used to read from the network stream that represents a socket.

Garbage Collection
Class/Technology Description


Used to suppress finalization of a custom object that has already been disposed.

Class/Technology Description


This sample implements a delegate type derived from MulticastDelegate. This type is used for notifications related to network events. The sample uses this delegate type to bind the networking type to the form type in the sample

Class/Technology Description


The custom Talker class in this sample handles networking logic and uses a second thread for reading from the network. However, rather than creating a thread, it simply uses a thread from the common language runtime thread pool. This is the suggested method for multithreading in managed code.

Class/Technology Description


The StreamReader and StreamWriter types commonly throw the IOException type. However, to catch and handle these exceptions properly, it is commonly necessary to check the attached exception that reflects the more base error. In this sample's case this will be the SocketException type.


This sample catches the SocketException type and checks the ErrorCode property of the socket for certain expected error types.

Command-Line Requirements

For the Visual J# samples to function properly when building and running from the command line, your Path environment variable must be set correctly. You must set the Path variable to include the <%windir%>\Microsoft .NET\Framework\v<%version%> directory. Additionally, you may also need to run CorVars.bat (located in the <.NETFrameworkSDKRoot>\Bin directory).

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft