Wednesday, April 3, 2013

Tutorial client server using Visual Basic

Tutorial client server using Visual Basic


Client/Server in VB.NET


are you ready to connect to your server? (y/n)
This is a tutorial for beginner or intermediate programmers. This will show you how communication between two computers works using a client server scheme. With Visual Basic as a programming language, you will be able to get the few concept of a classic TCP/IP data communication.

There are many samples on the internet. Some are written in Java or C++.  Some applies for mobiles phones and other uses older technologies such as Winsock. In all case, I wasn’t satisfied because they are either to complex or incomplete.

This tutorial will be only a beginning for those who want to start to do a client server application using Visual Studio: Visual Basic .NET (VB.NET), C# .NET . This tutorial will contain many samples and many references to help learn the main concept of a client server application for Windows. I will slowly illustrate very small code and tiny code sample in the first pages of this article. I will eventually end up with a more complete program if I have the time.



Also, keep in mind that is impossible to know everything on any client server application, TCP/IP protocol and all the other stuff related to communications between 2 electronic devices. In April 3rd 1973, 40 years ago, Motorola created their first cell phone.

Reference :

Lesson ONE : The data exchange.


Ok! This part is boring but I won’t stress it too much. Keep in mind that computer send and received bytes.  A byte is equal to 8 bits. Each byte represents a letter or a number.

bytes

Example : the letter “A” (upper case) in binary  8 bits is = 01000001

So if you need to send a word such as “APPLE”, you will need to send 5 bytes because your word as 5 letters : A, P, P, L and E.
That is all you need to know for now.

Reference:

Lesson TWO :  Send large amount of Data.


Any digital device can’t send data like water in a pipe.  Sending data is more like putting people in a train. You fill the train and when the train is full or ready to go, the train leaves and you have to wait for the next one.
Sending data over the internet or over any communication line work the same way than sending people from one place to another. It is a kind of stop and go process.
Your program written in Visual Basic or any other language will generate data or process information. When your program will be ready to send the data over the internet trough your modem, you ether card or your Wi-Fi antenna, your program will have to slice the data and put then into something call a SOCKET.
A socket is a vehicle with a limited amount of space to put your data.


socket

The only thing you need to remember is the data is cut into smaller group of data. Any computer will have to chew the data before processing new data.  I used the term SOCKET but other terms may exist.

You could type in Google the word Socket. Here are ideas or keywords:
+Socket microprocessor
+Trunk communications
+Communications buffer

That is all you need to know.

Lesson THREE : Establishing a connection


When you wish to reach someone by phone, you need to dial a phone number. Something like 1-234-5678 to reach someone in the United States or in Canada. After you dial the number, the phone will ring and the person you wish to speak with will pick up the phone.
It work the same way for 2 computers (Client and Server). When you computer need to connect to a server, it will use a protocol named TCP/IP. To establish a connection, the TCP/IP will need these two information: an IP address and a port number
To make this very simple: the IP address will guide your transmission to the right server and the port number will designate the right program running on your server.
Port numbers are normalized to make thing easier.
For example, your computer needs to connect to a web site. Your computer will normally use the port 80.
In Microsoft Visual Basic or in Microsoft C#, you could start building your own little server with this code (server side):

Imports System.Net ' for IPAddress
Imports System.Net.Sockets 'for TcpListener

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
        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()
        Catch ex As Exception

        End Try
    End Sub

End Module



The code is extremely simple because using an IPAddress, a port; you could create your own TCP protocol. But this code does nothing. Is like simply connecting your telephone in the wall. But this is a beginning.

On the client side, I almost copy pasted the server code because is really almost the same. I simply changed a few words and added a command line to stop the client program from continuing.


Imports System.Net ' for IPAddress
Imports System.Net.Sockets 'for TcpListener

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"
            If aString = "y" Then
                'Create a TcpClient.
                client = New TcpClient(localAddr.ToString, port)
            End If
          

            Do
                Console.Write("Connected to the server. 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



That is all you need to know for now.
In the next lesson, we will continue with the Accept function. Your server needs to accept the client. In other words, if you call someone by telephone, if the person you wish to speak with you doesn’t want to speak with you, you will be doomed!

Project to download: SampleCLientServer.zip
Reference:

Example from MSDN

TCP Listener from MSDN

Sockets from MSDN

Sample from MSDN


References:



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



SQL Server Developer Edition 2012 and Microsoft Visual Studio Pro 2012 are very cheap on Amazon ! Free Delivery. Check here:
  



9 comments:

  1. Hey all, this is great time to Build your knowledge, your network, and your community. Join JavaOne India 2013 8-9 May in Hyderabad at JavaOne India. Please sign up here https://www.regpulse.com/javaone2013/register.php?pcode=737266&src=4003&Act=1 to find out more details and launch date for the 2013 JavaOne.
    As always, the two days will be packed full with more valuable content from keynotes to technical sessions to hands-on labs and demos.

    ReplyDelete
  2. Client-server architectures are sometimes known as two-tier architectures.it's useful for send data from client to server.

    ReplyDelete
  3. A straight line is the shortest distance between two points.
    Here I present to you:
    The shortest and easiest way to program a simple calculator in less time and at the lowest possible number of code
    Waiting for your comments and opinions

    https://www.youtube.com/watch?v=ZHilOSSSRuY

    ReplyDelete
    Replies
    1. maybe in creating a simple system.. can be derived

      Delete
    2. maybe in creating a simple system.. can be derived

      Delete
  4. Really Nice Information,Thank You Very Much For Sharing.Web Designing Company

    ReplyDelete