Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Windows sockets...

Dear Experts,

I am planning a network application in VB. My server application will contain a winsock control which will eat & spit serial port data on the network. My client application is supposed to collect this data & plot it in real time. In short this is a tele viewing application where i will decide what to select (parse) from the serial port data (server side) & what to show (client side) from the LAN data being received from the server application.

I hope this is not so complex & someone of you Experts will help me out with some code snippets.

cOdEdRiLLeR
     [*_*]
0
codedriller
Asked:
codedriller
  • 2
1 Solution
 
vippxCommented:
Hi,

 use commcontrol for interfacing to ur communication ports and winsock control for network connectivity. details can be found in MSDN

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What side do you need help with...Client or Server?  Which part of the client/server do you need snippets on?
0
 
codedrillerAuthor Commented:
Its selfish... but I need both of them 'Idle Mind'. Of you do not have much time, Clientside will do.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...I just didn't want to post a ton of code if you didn't need it.  Below are a basic server and a basic client.  The winsock control in the server has its Index property set to 0 (zero) at design time. The client does not.

' --------------------------------------------------
' Server
' --------------------------------------------------
Option Explicit

Private inputBuffer As String
Private connections As Integer

Private Sub Form_Load()
    Me.Caption = "Server - Listening..."
    connections = 0
   
    ' channel 0 is the only listening port
    Winsock1(0).LocalPort = 10001
    Winsock1(0).Listen
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim a As Integer
   
    ' shut everything down
    For a = Winsock1.UBound To 0 Step -1
        If Winsock1(a).State <> sckClosed Then
            Winsock1(a).Close
        End If
    Next a
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim acceptIndex As Integer
    Dim curindex As Integer
   
    ' See if there is an available Channel already loaded
    acceptIndex = -1
    If Index = 0 Then
        For curindex = 1 To Winsock1.UBound
            If Winsock1(curindex).State = sckClosed Then
                acceptIndex = curindex
                Exit For
            End If
        Next curindex
    End If
   
    ' Load a channel if none available
    If acceptIndex = -1 Then
        acceptIndex = Winsock1.Count
        Load Winsock1(acceptIndex)
    End If
   
    ' Accept connection on Channel decided upon
    Winsock1(acceptIndex).Accept requestID
   
    ' Increase number of connections
    connections = connections + 1
    Me.Caption = "Server : " & connections & " Connection(s)"
End Sub

Private Sub Winsock1_Close(Index As Integer)
    Winsock1(Index).Close
   
    ' Decrease Total number of connections
    connections = connections - 1
    If connections > 0 Then
        Me.Caption = "Server : " & connections & " Connection(s)"
    Else
        Me.Caption = "Server - Listening..."
    End If
   
    ' attempt to release unneeded Winsock Controls from end of array
    Do While Winsock1(Winsock1.UBound).State = sckClosed
        Unload Winsock1(Winsock1.UBound)
    Loop
End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim inputString As String
   
    ' add current command to buffer and process the buffer
    Winsock1(Index).GetData inputString, vbString
    inputBuffer = inputBuffer & inputString
    processInputs
End Sub

Private Sub processInputs()
    Dim markerPos As Integer
    Dim curInput As String
    Dim values As Variant
   
    ' see if we have a msg in our buffer
    ' chr(1) denotes the end of a message
    ' chr(0) is used to delimit different parts of each message
    markerPos = InStr(inputBuffer, Chr(1))
    Do While markerPos > 0 ' if we have a msg then process it
        curInput = Left$(inputBuffer, markerPos - 1) ' get msg from buffer
        inputBuffer = Mid(inputBuffer, markerPos + 1) ' remove msg from buffer
       
        values = Split(curInput, Chr(0)) ' split msg into command and its values
       
        Select Case values(0) ' what command was it?
            Case "a"
           
            Case "b"
           
            Case "c"
           
            Case Else
               
        End Select
       
        markerPos = InStr(inputBuffer, Chr(1)) ' any more msgs in buffer?
    Loop
End Sub

' --------------------------------------------------
' Client
' --------------------------------------------------
Option Explicit

Private IP As String
Private port As Long
Private inputBuffer As String

Private Sub Form_Load()
    Me.Caption = "Client - Connecting..."
    IP = "127.0.0.1"
    port = 10001
   
    ' attemp to connect to the server
    Winsock1.Connect IP, port
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim f As Form
   
    Winsock1.Close
    For Each f In Forms
        Unload f
    Next
End Sub

Private Sub Winsock1_Close()
    Me.Caption = "Client Disconnected"
End Sub

Private Sub Winsock1_Connect()
    Me.Caption = "Client Connected"
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim inputString As String
           
    ' add msg to buffer and process it
    Winsock1.GetData inputString, vbString
    inputBuffer = inputBuffer & inputString
    processInputs
End Sub

Private Sub processInputs()
    Dim markerPos As Integer
    Dim curInput As String
    Dim values As Variant
   
    ' see if we have a msg in our buffer
    ' chr(1) denotes the end of a message
    ' chr(0) is used to delimit different parts of each message
    markerPos = InStr(inputBuffer, Chr(1))
    Do While markerPos > 0 ' if we have a msg then process it
        curInput = Left(inputBuffer, markerPos - 1) ' get msg from buffer
        inputBuffer = Mid(inputBuffer, markerPos + 1) ' remove msg from buffer
       
        values = Split(curInput, Chr(0)) ' split msg into command and its values
       
        Select Case values(0) ' what command was it?
            Case "a"
                     
            Case "b"
               
            Case "c"
               
            Case Else
               
        End Select
       
        markerPos = InStr(inputBuffer, Chr(1)) ' any more msgs in buffer?
    Loop
End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now