borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Username and Password with winsock)Next Topic (ASCII string to Excel via winsock) New Topic New Poll Post Reply
AndreaVB Forum : Network : Winsock
Poster Message
millerdraft
Level: Scholar

Registered: 01-02-2003
Posts: 41

icon Winsock

I have a client program that tries to connect to my server.  On all the other workstations it works fine.  On this particular computer it gives Run-time error: 40006 .  I have the server set up so that if a client tries to connect it shows the that the winsock connection was made.  The only problem is that the client doesn't connect all the way.  If someone could point me in the right direction on that it would be greatly appreciated.

Ted

____________________________
Ted Moe
I wish I was real good at this stuff!

21-05-2003 at 04:09 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: Winsock

Few things:
1) code may be needed for others to help
2) since it works on other workstations, I'd have to assume that something's different with this one

Some things that might be of help:

Network timing issues (slower connection/pc)->may be getting/sending data before the connection's ready

Make sure error handling exists in the routine to send/receive info, put logging into the program to log which routine's causing the error (if you don't already know).


____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

21-05-2003 at 06:20 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
millerdraft
Level: Scholar

Registered: 01-02-2003
Posts: 41
icon Re: Winsock

Okay here is the code that I am using.



'***********************************
'Client
'***********************************
Dim strStatus As String

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
wskServer.Close
End Sub

Private Sub txtPassword_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then cmdLogIn_Click
End Sub

Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)
    'Must do this to see if the Winsock control isn't already being used.
    If wskServer.State <> sckClosed Then wskServer.Close
    wskServer.Accept requestID
End Sub

Private Sub cmdLogIn_Click()
    Dim strData As String
    strstat = "Logging into Server"
    wskServer.Close
    wskServer.Connect "207.108.89.20", "6508"
    DoEvents
    DoEvents
        
    lblStatus.Caption = "Connected"
    strData = txtIDNumber.Text & ":" & txtPassword.Text
    wskServer.SendData strData
End Sub

Private Sub wskServer_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    lblStatus.Caption = strstat
    wskServer.GetData strData
    lblStatus.Caption = strData
    wskServer.Close
End Sub

'***********************************
'Server below
'***********************************
Option Explicit
Dim intConnection As Integer
Dim IDNumber As String
Dim StrData As String

Private Sub cmdLogin1()
    IDNumber = txtIDNumber.Text
    If IDNumber = "" Then wskServer(intConnection).SendData "You must enter a username!": Exit Sub
    Pass = ReadINI(IDNumber, "Password", App.Path & "settings.ini")
    If Pass = "" Then wskServer(intConnection).SendData "Invalid Username": Exit Sub
    If txtPassword.Text <> Pass Then wskServer(intConnection).SendData "Incorrect Password": Exit Sub
    If cmdLogin.Enabled = False Then cmdLogin.Enabled = True: GoTo Skip
    txtIDNumber.Text = "": txtPassword.Text = "": Exit Sub
    End If
        txtIDNumber.Text = ""
        txtPassword.Text = ""
End Sub

Private Sub cmdLogin_Click()
    cmdLogin1
End Sub

Private Sub wskserver_close(Index As Integer)
    Unload wskServer(intConnection)
    intConnection = intConnection - 1
    lblConnected.Caption = intConnection 'takes one off the connection status
End Sub

Private Sub wskServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    intConnection = intConnection + 1  'add another connection
    Load wskServer(intConnection)  'Loads a new winsock
    wskServer(intConnection).Accept requestID  ' Allows sever to connect
    lblConnected.Caption = lblConnected.Caption + 1  ' Shows total connected computers
End Sub

Private Sub wskServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    
    txtIPAddress.Visible = True
    Dim RemoteIP As String
    RemoteIP = wskServer(intConnection).RemoteHostIP
    txtIPAddress.Text = RemoteIP
    intData = intConnection
    wskServer(intConnection).GetData StrData
    txtIDNumber.Text = Split(StrData, ":")(0)
    txtPassword.Text = Split(StrData, ":")(1)
    DoEvents
    cmdLogin1
    
End Sub

Private Sub Form_Load()
    wskServer(intConnection).Close    'Closes any open connections
    wskServer(intConnection).Listen    ' Make winsock start listening
    lblConnected.Caption = "0"  'Displays zero connections

End Sub


____________________________
Ted Moe
I wish I was real good at this stuff!

21-05-2003 at 01:37 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
jrb
Level: Guest

icon Re: Winsock

Hi.

Hopefully the following VB code will bring you one step further!



***************************************************
Startup module
***************************************************
Option Explicit
Sub Main()
  frmServer.Show
  frmClient.Show
End Sub

***************************************************
frmServer
***************************************************
Option Explicit

' Decide how many clients to support
Private Const MAX_NO_OF_CLIENTS = 1000  ' No more than 32535!

' Characters used for transmitting data
Private Const EOF_Char = "¤"
Private Const SEP_Char = "|"

' Decide which port to listen
Private Const LISTEN_PORT = 4711

' Define client states
Private Enum CLIENT_STATE
  Client_Closed = 0
  Client_Open = 1
End Enum

' Define client type
Private Type CLIENT_TYPE
  Index As Integer
  State As CLIENT_STATE
End Type

' Dim Clients as array
Dim Clients() As CLIENT_TYPE
Dim IDNumber As String
Dim Pass As String
Dim Password As String
Dim StrData As String
Dim AppPath As String

Private Sub Form_Load()
  ReDim Clients(MAX_NO_OF_CLIENTS)
  AppPath = App.Path
  If Mid(AppPath, Len(AppPath)) <> "" Then ' Add separator
    AppPath = AppPath & ""
  End If
  With wskClient(0) ' Element 0 (zero) is used to listen for
                    ' connection requests
    .LocalPort = LISTEN_PORT
    .Listen
  End With
End Sub

Private Sub cmdLogin_Click()
  CheckForInput
End Sub

Private Function CheckForInput(Optional Index As Integer) As String
  If IDNumber = "" Then
    CheckForInput = "You must enter a username!"
    GoTo ExitNow
  End If
  Pass = ReadINI(IDNumber, "Password", AppPath & "settings.ini")
  If Pass = "" Then
    CheckForInput = "Invalid Username"
  End If
  If Password <> Pass Then
    CheckForInput = "Incorrect Password"
  End If
ExitNow:
  If Index = 0 Then
    ' cmdLogin is pressed
    If CheckForInput <> "" Then
      Beep
      MsgBox CheckForInput, vbCritical
    End If
  Else
    ' Input from client
    If wskClient(Index).State = sckConnected Then
      If CheckForInput <> "" Then
        wskClient(Index).SendData CheckForInput & vbCrLf
      Else
        wskClient(Index).SendData "Login OK" & vbCrLf
      End If
    End If
  End If
End Function

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  Dim counter As Integer
  For counter = 1 To MAX_NO_OF_CLIENTS
    With Clients(counter)
      If .State = Client_Open Then
        wskClient(.Index).Close
        Unload wskClient(.Index)
        .Index = 0
        .State = Client_Closed
      End If
    End With
  Next counter
End Sub

Private Sub wskClient_ConnectionRequest(Index As Integer, ByVal requestID As Long)
  Dim counter As Integer
  '
  ' Index is 0. This is the only winsock object in listen state
  '
  ' Find free entry in client table
  '
  For counter = 1 To MAX_NO_OF_CLIENTS
    With Clients(counter)
      If .Index = Client_Closed Then        ' Available entry
        .Index = counter
        .State = Client_Open
        txtClientsConnected = txtClientsConnected + 1
        Load wskClient(.Index)              ' Load new winsocket
        wskClient(.Index).Accept requestID  ' Accept request
        Exit Sub                            ' and exit sub
      End If
    End With
  Next counter
  ' Limit has been reached. No more entries. Do not accept request
End Sub

Private Sub wskClient_Close(Index As Integer)
  CloseClient Index
End Sub

Private Sub wskClient_DataArrival(Index As Integer, ByVal bytesTotal As Long)
  Dim s As String
  Do
    wskClient(Index).GetData s, , bytesTotal
    StrData = StrData & s
  Loop Until s = ""
  If InStr(StrData, EOF_Char) Then
    '
    Do
      IDNumber = Mid(StrData, 1, InStr(StrData, SEP_Char) - 1)
      Password = Mid(StrData, InStr(StrData, SEP_Char) + 1)
      Password = Mid(Password, 1, InStr(Password, EOF_Char) - 1)
      CheckForInput Index
      StrData = Mid(StrData, InStr(StrData, EOF_Char) + 1)
    Loop Until InStr(StrData, EOF_Char) = 0
  Else
    ' Data stream not complete. Exit sub and wait for more
  End If
End Sub

Private Sub wskClient_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  CloseClient Index
End Sub

Private Sub CloseClient(ByVal Index As Integer)
  Dim counter As Integer
  wskClient(Index).Close
  Unload wskClient(Index)
  txtClientsConnected = txtClientsConnected - 1
  For counter = 1 To MAX_NO_OF_CLIENTS
    With Clients(counter)
      If Index = .Index Then
        .Index = 0
        .State = Client_Closed
        Exit Sub
      End If
    End With
  Next counter
End Sub

Private Function ReadINI(ByVal UserID As Integer, _
                         ByVal Section As String, _
                         ByVal IniPath As String) As String
  ' Read fromn ini file
End Function

***************************************************
frmClient
***************************************************
Option Explicit

' Characters used for transmitting data
Private Const EOF_Char = "¤"
Private Const SEP_Char = "|"

' Decide which IP and port to connect to
Private Const SERVER_IP = "127.0.0.1"
Private Const SERVER_PORT = 4711

Private Sub cmdLogin_Click()
  wskServer.Connect SERVER_IP, SERVER_PORT
End Sub

Private Sub Form_Load()

End Sub

Private Sub wskServer_Connect()
  wskServer.SendData txtIDNumber & SEP_Char & txtPassword & EOF_Char
End Sub

Private Sub wskServer_DataArrival(ByVal bytesTotal As Long)
  Dim s As String
  
  wskServer.GetData s, , bytesTotal
  MsgBox s, vbInformation
  wskServer.Close
End Sub

Private Sub wskServer_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  wskServer.Close
  Beep
  MsgBox Description, vbCritical
End Sub

13-06-2003 at 10:34 PM
| Quote Reply
millerdraft
Level: Scholar

Registered: 01-02-2003
Posts: 41
icon Re: Winsock

That seems like it might work but I keep running into the same problem.  Where   Pass = ReadINI(IDNumber, "Password", AppPath & "settings.ini")

pass = "" but then the password ="test"

Any help on this would be greatly appreciated.

Thanks

Ted

[Edited by millerdraft on 18-06-2003 at 09:36 PM GMT]

____________________________
Ted Moe
I wish I was real good at this stuff!

19-06-2003 at 03:35 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : Network : Winsock
Previous Topic (Username and Password with winsock)Next Topic (ASCII string to Excel via winsock) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder