Solved

VB client and server...

Posted on 2004-10-08
5
196 Views
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 ..
prashanth...
0
Comment
Question by:prashanth_gurijala
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:eozz_2000
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.
0
 

Author Comment

by:prashanth_gurijala
ID: 12266945
Hi,
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
0
 
LVL 10

Accepted Solution

by:
Hans Langer earned 500 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)
      objThread.Start()
      'SendData("GERENTE")
   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)
         'MsgBox(strData)

      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

      Try
         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


   '----------------------
   'Server

   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)
      objListener.Start()
      objThread = New System.Threading.Thread(AddressOf WaitClient)
      objThread.Start()
   End Sub

   Private Sub WaitClient()
      Dim sctClient As Client

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

         System.Threading.Interlocked.Increment(lgnIdClient)

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

         sctClient.Thread.Start()

      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
         Try

            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)
            sctData.Thread.Abort()
         Catch e As Exception
            SyncLock Me
               'Delete Client
               ClientList.Remove(lngIdReal)
            End SyncLock

            Exit While
         End Try
      End While

   End Sub
0
 

Author Comment

by:prashanth_gurijala
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 127.0.0.1 as IpAdress to openconnection function?

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


and again thanx a lot
-prashanth
0
 
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,

GL


0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now