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
     [*_*]
codedrillerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

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.