VB client and server...

Posted on 2004-10-08
Medium Priority
Last Modified: 2012-06-27
Hi, i know about winsock that windows has to make life easier for handling connections. I wanna write a client and server application in VB and then send a packet of size may be 32kb or 64 kb or predefined from client and then get round trip time... RTT. UDP is preferred....or else tcp is fine... The client must just send packets and server must receive. and find RTT....
Thanx ..
Question by:prashanth_gurijala
  • 2
  • 2

Expert Comment

ID: 12266317
I would use a timer control in the client that is enabled when the package is sent, when the package arrives I would disable the timer and then see the value of a public variable previously declared.

Author Comment

ID: 12266945
yeah can i have a sample code of client ,server which send packets of some predefined size and then server echoes back those packets? then i can use timer to start and stop.

thanx   prashanth
LVL 10

Accepted Solution

Hans Langer earned 2000 total points
ID: 12273176
Hi, Try this:

   ' Client

   Private Shared objTcpClient As System.Net.Sockets.TcpClient
   Private Shared bytBufferRecep() As Byte
   Private Shared bytBufferSend() As Byte
   Private Shared objStream As IO.Stream
   Private Shared objThread As System.Threading.Thread

   Public Sub Conectar(ByVal IPServer As String, ByVal Port As Int32)
      objTcpClient = New System.Net.Sockets.TcpClient
      objTcpClient.Connect(IPServer.Trim(), Port)
      objStream = objTcpClient.GetStream()
      objThread = New System.Threading.Thread(AddressOf ReadData)
   End Sub

   Public Sub ReadData()
      Dim strData As String
      Dim Start As System.DateTime
      Dim Final As System.DateTime

      Start = Now

      While True
         Dim numBytes As Int32 = objTcpClient.ReceiveBufferSize
         Dim bytBufferRecepcion(numBytes) As Byte
         objStream.Read(bytBufferRecep, 0, numBytes)

         strData = System.Text.Encoding.ASCII.GetString(bytBufferRecep)

      End While
      Final = Now

      MsgBox("It took " & ((Final.Ticks - Start.Ticks) / 10000).ToString & " msecs to answer")

   End Sub

   Public Sub SendData(ByVal Msg As String)
      Dim Encod As New System.Text.ASCIIEncoding

         bytBufferSend = Encod.GetBytes(Msg)
         If Not (objStream Is Nothing) Then
            objStream.Write(bytBufferRecep, 0, bytBufferRecep.Length)
         End If
      Catch ex As Exception

      End Try
   End Sub


   Private Shared objTcpClient2 As System.Net.Sockets.TcpClient
   Private ClientList As New Hashtable
   Private Shared lgnIdClient As Long = 0
   Private objListener As System.Net.Sockets.TcpListener

   Private Structure Client
      Public Socket As System.Net.Sockets.Socket
      Public Thread As System.Threading.Thread
      Public EndPoint As System.Net.EndPoint
   End Structure

   Public Sub OpenConnection(ByVal IpAdress As String, ByVal Port As Integer)
      objListener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse(IpAdress), Port)
      objThread = New System.Threading.Thread(AddressOf WaitClient)
   End Sub

   Private Sub WaitClient()
      Dim sctClient As Client

      While True
         'Wait for a client
         sctClient.Socket = objListener.AcceptSocket()
         sctClient.EndPoint = sctClient.Socket.RemoteEndPoint


         sctClient.Thread = New System.Threading.Thread(AddressOf ReadDataClient)
         SyncLock Me
            ClientList.Add(lgnIdClient, sctClient)
         End SyncLock


      End While
   End Sub

   Public Sub ReadDataClient()
      Dim lngIdReal As Long = lgnIdClient
      Dim sctClient As Client = CType(ClientList(lngIdReal), Client)
      Dim sctCliente2 As Client
      Dim strAnwswer As String

      While True

            Dim numBytes As Int32 = objTcpClient2.ReceiveBufferSize
            Dim bytBufferRecepcion(numBytes) As Byte
            objStream.Read(bytBufferRecepcion, 0, numBytes)
            strAnwswer = System.Text.Encoding.ASCII.GetString(bytBufferRecepcion)

            strAnwswer &= "User " & strAnwswer & " Connected"

            'Answer to all client
            For Each sctCliente2 In ClientList.Values
               If sctCliente2.Socket.Connected Then

                  sctCliente2.Socket.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(strAnwswer), System.Text.ASCIIEncoding.ASCII.GetBytes(strAnwswer).Length, System.Net.Sockets.SocketFlags.None)
               End If
            Next sctCliente2

         Catch ex As Exception
            Dim sctData As Client = CType(ClientList(lngIdReal), Client)
         Catch e As Exception
            SyncLock Me
               'Delete Client
            End SyncLock

            Exit While
         End Try
      End While

   End Sub

Author Comment

ID: 12275159
Hi thanx a lot Gerente,
it helped me a lot.. now i got an idea to approach...
if u can can please tell me in server code why we need to mention the server ip

OpenConnection(ByVal IpAdress As String, ByVal Port As Integer)

becuase any way server listens on a port of local host. Do we need to pass as IpAdress to openconnection function?

and also i suppose we will have some processing delay... from server side....

and again thanx a lot
LVL 10

Expert Comment

by:Hans Langer
ID: 12277849
If you have more than one network card you need to specify what of those you gonna use, his IP and what port to listen.
If you just use port to listen you gonna listen (For Internet or LAN) with all you network card including localhost,



Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question