Visual Basic TCP (Client/Server) Example

' connect to TCP server and read it's greeting message

Function ConnectToServer(ByVal Server As Integer, ByVal Port As Integer, ByVal Buffer As String, ByVal BufLen As Integer) As Integer
Dim Code As Integer
Dim DataSock As Integer
' the client connects to the server
DataSock = cscClient(Server, Port)
' error? (negative return codes are errors)
If DataSock < 0 Then
  ConnectToServer = DataSock
  Exit Function
End If
' wait (3 sec) for greeting message from server
If cscAwaitData(DataSock, 3000) <> 0 Then
  ' read server's greeting message into Buffer
  Code = cscGetData(DataSock, Buffer, BufLen)
  If Code < 0 Then
    ConnectToServer = Code
    Exit Function
  End If
  ' terminate server's text message with a NULL
  Buffer = Left(Buffer,Code) + Chr(0)
  ' return (virtual) data socket
  ConnectToServer = DataSock
  Exit Function
End If
' timed out
End Function

MSC Logo

MARSHALLSOFT is a trademark of MarshallSoft Computing, Inc.