The TCPChat Application

Team Fly 

Page 300

            TCPSocket.Close()
        End If
    End If
End Sub

The Socket object exposes asynchronous versions of its methods. They're the BeginListen, Begin-Accept, and BeginReceive methods, which initiate the appropriate action in asynchronous mode. You need to implement AsyncCallbacks to intercept and process the event of the completion of the operation. However, most typical applications use the TcpListener, TcpClient, and UdpClient classes. These classes abstract the operations of the two types of connections and simplify coding. However, a basic understanding of sockets and the basic principles demonstrated in the previous sections are necessary to use the classes that are specific to a protocol.

Because the TCP protocol is inherently more reliable than the UDP protocol and is also used more often, we're going to demonstrate how to use the TcpListener and TcpClient classes to build a chat application.

The TCPChat Application

In this section you'll build a fairly advanced application that allows multiple remote clients to engage in a chat. Figure 12.3 shows the server and a few clients of the application. Each client joins the conversation by establishing a connection to the server. Once the client is connected to the server, it sends messages to the server. The server displays the incoming messages on a TextBox control on its own interface and then broadcasts them to all clients. Once a message arrives at a client, it's displayed on a TextBox control, along with the name of the person who sent the message. As each client establishes a connection to the chat server, it also establishes a username for the session.

image

FIGURE 12.3 The TCPChat server and clients

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net