Friday, April 5, 2013

Example on AcceptTcpClient method in Visual Basic

Example on AcceptTcpClient method in Visual Basic


AcceptTcpClient VB.NET


When you create a TCPClient, is not enough to start your server, you also need to accept incoming connection. In a previous post, I explained in 3 steps how to make a client and a server but nothing more: Tutorial client server using Visual Basic.
You will have to use the AcceptTcpClient method to safely pick up any inbound connection (server side).
If you don’t use AcceptTCPClient or any equivalent, no communication will start. Is like trying to call for someone and never be able to speak with the person you wish to speak. The phone is ringing and no one is there.



Lesson FOUR : Accept the client

The connection is pending after calling the Start method from a TCPListener. So is important to create a connection by using AcceptTCPClient. If a client tries to connect to the server, the server side will create a client object. That object will normally obtain the address IP of the client.
AcceptTCPClient return a TCPClient object and you can use it to send and receive data using NetStream.
In the next example, we use the local host. It will work the same way if we replace the localhost or 127.0.0.1 with a “real” client-server.
Server side:
Imports System.Net ' for IPAddress
Imports System.Net.Sockets 'for TcpListener
Imports System.Text

Module Module1

    Sub Main()
        Dim port As Integer 'this is the port number
        Dim localAddr As IPAddress ' this is the IP address
        Dim server As TcpListener ' This is for the TCP/IP Protocol
        Dim client As TcpClient
        Dim aString As String
        Dim negotiateStream As Security.NegotiateStream = Nothing
        Try
            'define the two values for your Server
            port = 1234
            localAddr = IPAddress.Loopback 'loopbak = 127.0.0.1 = myself

            'Create your server
            server = New TcpListener(localAddr, port)

            'make your server available
            server.Start()
            Console.WriteLine("Server ready")

            'this is a block method, it will wait for new connection
            'before continuing
            client = server.AcceptTcpClient
            Console.WriteLine("Connected to " & IPAddress.Parse(CType(client.Client.LocalEndPoint, IPEndPoint).Address.ToString).ToString)
          

            Do
                Console.Write(" Ready to quit? (y/n)")
                aString = Console.ReadLine()
                If aString <> "y" Then
                    'do something
                End If
            Loop Until aString = "y" ' Or aString = "n"

        Catch ex As Exception

        End Try
    End Sub

End Module




Client side :
Imports System.Net ' for IPAddress
Imports System.Net.Sockets 'for TcpListener
Imports System.Text

Module Module1
    ''' <summary>
    ''' CLIENT
    ''' </summary>
    ''' <remarks></remarks>
    Sub Main()
        Dim aString As String
        Dim port As Integer 'this is the port number
        Dim localAddr As IPAddress ' this is the IP address
        Dim client As TcpClient ' This is for the TCP/IP Protocol
        Try
            'define the two values for your Server
            port = 1234
            localAddr = IPAddress.Loopback 'loopbak = 127.0.0.1 = myself


            Do
                Console.Write("are you ready to connect to your server? (y/n) ")
                aString = Console.ReadLine()

            Loop Until aString = "y" Or aString = "n"
          
            Do
                If aString = "y" Then
                    'Create a TcpClient.
                    client = New TcpClient(localAddr.ToString, port)
                    Console.Write(" Connencted to : " & IPAddress.Parse(CType(client.Client.LocalEndPoint, IPEndPoint).Address.ToString).ToString & " ")

                End If
                Console.Write("Ready to quit? (y/n)")
                aString = Console.ReadLine()

            Loop Until aString = "y" Or aString = "n"
        Catch ex As Exception

        End Try

    End Sub

End Module




Now, you are able to establish a connection between a client application and a server application. If you want to put the client and the server on 2 different computers, you just need to change the IPAddress to a valid IP address.

Project to download: SampleCLientServer.zip

Reference:


References:



The program I love to use, buy it: Visual Studio 2010 Professional (Old Version)

2 comments:

  1. sir, may be you can teach me how TCP server can get message from TCP client?
    what's the syntax for tcp server can accept a message from tcp client?
    thanks

    ReplyDelete