Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB client and server...

Posted on 2004-10-08
5
200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 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