Information
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

Application

Used by the sample application to implement a message pump.

Splitter

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

Panel

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.

TextBox

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

Label

Used to display static status information in the sample.

MessageBox

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

Networking
Class/Technology Description

IPEndPoint

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

IPAddress

Provides useful IPAddress-related logic.

Dns

Used to get an IP address from a DNS name.

Socket

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.

NetworkStream

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.

I/O
Class/Technology Description

StreamWriter

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

StreamReader

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

Garbage Collection
Class/Technology Description

GC

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

Delegates
Class/Technology Description

MulticastDelegate

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

Threading
Class/Technology Description

ThreadPool

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.

Exceptions
Class/Technology Description

IOException

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.

SocketException

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

Show:
© 2015 Microsoft