By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Server/Client Not all connecting after server diconnect

P: 6
I am creating a small server client program that is meant for up to 70 connections from 70 different computers on a network.

Everything in the program functions correctly except when testing operations with a server crash, the clients go into a disconnected state and wait for the server to come back up.

Once the server is up the clients attept to connect. 75% of the time all clients connect and there connect message is accepted and parsed without a problem then they are added to a clients hash table, the other 25% the server still connects all clients but thier connect message is not accepted or parsed so they are not added to the client hash table, maybe missine one or two, the clients sent the connect string but its as if the server never got the message. I have no clue how to remedy this. Here is some sample code hopefully enough to explain the process. Thanks to all I'm losing my mind on this.

Server:

Expand|Select|Wrap|Line Numbers
  1. Private listener As TcpListener
  2. Private listenerthread As Threading.Thread
  3. Public Shared statusdict As New Hashtable
  4. Public Shared clients As New Hashtable
  5.  
  6.  Private Sub AdminForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7.  
  8.         '**********************************************************************
  9.         'SOCKET CONNECTION SECTION 
  10.         listenerthread = New Threading.Thread(AddressOf DoListen)
  11.         listenerthread.Start()
  12.  
  13.         Updatestatus("Listener Started")
  14.         '**********************************************************************
  15.  
  16.   End Sub
  17.  
  18.  Private Sub DoListen()
  19.         Try
  20.             ' Listen for new connections.
  21.           listener = New TcpListener(System.Net.IPAddress.Any, config.PortNumber)
  22.             listener.Start()
  23.  
  24.             Do
  25.                 ' Create a new user connection using TcpClient returned by
  26.  
  27.                 Dim client As New UserConnection(listener.AcceptTcpClient)
  28.  
  29.                 ' Create an event handler to allow the UserConnection to communicate with the window.
  30.  
  31.                 AddHandler client.LineReceived, AddressOf OnLineReceived
  32.                 Updatestatus("New connection found: waiting for log-in")
  33.             Loop Until False
  34.         Catch ex As Exception
  35. 'error handling
  36.         End Try
  37.     End Sub
  38.  
  39.  
  40.  Private Sub ConnectUser(ByVal workstation As String, ByVal sender As UserConnection)
  41.         Try
  42.             If Not clients.Contains(workstation) Then
  43.  
  44.                 sender.WS = workstation
  45.                 '
  46.                 'adds connected ws to hashtable
  47.                 clients.Add(workstation, sender)
  48.                 'sends connected to client
  49.                 ReplyToSender("CONNECTED|" sender)
  50.             End If
  51.         Catch ex As Exception
  52.             'MsgBox(Environment.StackTrace.ToString)
  53.          End Try
  54.     End Sub
  55.  
  56. Private Sub OnLineReceived(ByVal sender As UserConnection, ByVal data As String)
  57.         Dim dataArray() As String
  58.         Dim clientcount As Integer = clients.Count
  59.         ' Message parts are divided by "|"  Break the string into an array accordingly.
  60.         dataArray = data.Split(Chr(124))
  61.         ' dataArray(0) is the command. 
  62.         Try
  63.  
  64.             Select Case dataArray(0)
  65.                 Case "CONNECT"
  66.                     'when connect is sent from client, the ws is the dataarray(1)
  67.                     Try
  68.                         Updatestatus(dataArray(1) & " has connected.")
  69.                         ConnectUser(dataArray(1), sender)
  70.                       Catch ex As Exception
  71.                               'some error processing
  72.                       End Try
  73.  
  74.  Case "RECSTATUS"
  75.                     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  76. 'creates and updates a separete hashtable for display of ws status
  77.  
  78.                     Try
  79.                         If Not statusdict.ContainsKey(sender.WS) Then
  80.                             statusdict.Add(sender.WS, dataArray(3))
  81.                         Else
  82.                             statusdict.Item(sender.WS) = dataArray(3)
  83.                         End If
  84.                       Catch ex As Exception
  85.                         MsgBox(ex)
  86.                         WriteError.WriteErrorFile(Me.Name.ToString, ex.ToString(), True, True, True, True)
  87.                     End Try
  88.             End Select
  89.         Catch ex As Exception
  90.            ' WriteError.WriteErrorFile(Me.Name.ToString, ex.ToString(), True, True, True, True)
  91.         End Try
  92. End select 
  93. End sub
  94.  
  95.     Private Sub Updatestatus(ByVal statusmessage As String)
  96.         lstStatus.Items.Add(statusmessage)
  97.         'lstStatus.SelectedIndex = lstStatus.Items.Count - 1
  98.     End Sub
  99.  
  100. Public Shared Sub ReplyToSender(ByVal strmessage As String, ByVal sender As UserConnection)
  101.         Try
  102.             sender.SendData(strmessage)
  103.         Catch ex As Exception
  104.             'MsgBox("1 " & Environment.StackTrace.ToString)
  105.         End Try
  106.  
  107. End Sub
Client:

Expand|Select|Wrap|Line Numbers
  1.    Dim currentstatus As String = ""
  2.     Dim oldstatus As String = ""
  3.     Dim conncount As Integer = 0
  4.  
  5.     Const READ_BUFFER_SIZE As Integer = 255
  6.     Private client As TcpClient
  7.     Private readbuffer(READ_BUFFER_SIZE) As Byte
  8.  
  9. Private Sub clientform_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  10.         MakeConnection(Nothing)
  11. End Sub
  12.  
  13. Public Sub MakeConnection(ByVal callingfunc As String)
  14.  
  15.         Try
  16.             If callingfunc <> "MarkAsDisconnected" Then
  17.                 Me.Show()
  18.             End If
  19.  
  20.             Do
  21.                 Try
  22.                     client = New TcpClient()
  23.                     client.Connect("localhost", 6000)
  24.                     Me.Enabled = True
  25.                     Label1.BackColor = Color.Green
  26.                 Catch e As Exception
  27.                     Label1.BackColor = Color.Red
  28.                 End Try
  29.             Loop Until client.Connected = True
  30.  
  31.             ' Start an asynchronous read invoking DoRead to avoid lagging the user
  32.             ' interface. 
  33.  
  34.             client.GetStream.BeginRead(readbuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
  35.  
  36.             ' If callingfunc <> "MarkAsDisconnected" Then 'attempt to make it recheck for connection and reconnect
  37.             SendCMD("CONNECT|" & System.Environment.MachineName)
  38.  
  39.  
  40.  
  41.         Catch e As Exception
  42.              Me.Dispose()
  43.         End Try
  44.     End Sub
  45.  
  46.     Private Sub DoRead(ByVal ar As IAsyncResult)
  47.         Dim BytesRead As Integer
  48.         Dim strMessage As String
  49.         Try
  50.             ' Finish asynchronous read into readBuffer and return number of bytes read.
  51.             BytesRead = client.GetStream.EndRead(ar)
  52.             If BytesRead < 1 Then
  53.                 ' If no bytes were read server has close.  Disable input window.
  54.                MarkAsDisconnected()
  55.                Exit Sub
  56.             End If
  57.             ' Convert the byte array the message was saved into, minus two for the
  58.             ' Chr(13) and Chr(10)
  59.  
  60.             strMessage = Encoding.ASCII.GetString(readbuffer, 0, BytesRead - 2)
  61.  
  62.             ProcessCommands(strMessage)
  63.  
  64.             ' Start a new asynchronous read into readBuffer.
  65.             client.GetStream.BeginRead(readbuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
  66.  
  67.         Catch e As Exception
  68.            MarkAsDisconnected()
  69.         End Try
  70.     End Sub
  71.  
  72.  Private Sub MarkAsDisconnected()
  73.                conncount = 1
  74.         oldstatus = currentstatus
  75.                MakeConnection("MarkAsDisconnected")       
  76. End Sub
  77.  
  78.     Private Sub SendCMD(ByVal data As String)
  79.         Dim writer As New IO.StreamWriter(client.GetStream)
  80.         writer.Write(data & vbCr)
  81.         writer.Flush()
  82.     End Sub
  83.  
  84. Private Sub ProcessCommands(ByVal strMessage As String)
  85.         Dim dataArray() As String
  86.         ' Message parts are divided by "|"  Break the string into an array accordingly.
  87.         dataArray = strMessage.Split(Chr(124))
  88.  
  89.         If dataArray(0) = "CONNECTED" Or dataArray(0) = "LOCKED" Or _
  90.         dataArray(0) = "LOGGEDIN" Or dataArray(0) = "LOGGEDOUT" Then
  91.             currentstatus = dataArray(0)
  92.         End If
  93.  
  94.  
  95.         Select Case dataArray(0)
  96.             Case "CONNECTED"
  97.                 Label1.BackColor = Color.Green
  98.                 If conncount = 0 Then
  99.                     'sends status connected, but dont want to log on recon
  100.                     SendCMD("RECSTATUS|CONNECTED|" & System.Environment.MachineName.ToString)
  101.  
  102.                 ElseIf conncount = 1 Then
  103.                     currentstatus = oldstatus
  104.                     SendCMD("RECSTATUS|" & currentstatus & "|" & System.Environment.MachineName.ToString)
  105.                     conncount = 0
  106.                 End If
  107.  
  108. Case Else
  109.                 MsgBox("no message")
  110.  
  111.         End Select
  112.     End Sub
Hopefully this is enough to help understand. I appreciate everything anyone can help me with.
Mar 25 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.