473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Server/Client Not all connecting after server diconnect

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
0 1500

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

Similar topics

1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
3
by: Jo Davis | last post by:
www.shanje.com does sql server hosting, on shared servers, at a reasonable price. It seems. They also allow client connections. Just playing around I've managed to connect an Access Data Project...
7
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly...
29
by: Arno R | last post by:
Hi all, I am involved in a project where a client needs a new database over a wan (30 or more locations). The client is a health-care organisation that 'services' mentally disordered people. ...
2
by: Kevin R | last post by:
I'm trying to get asp.net 1.1 running on my home PC. When I try creating a new ASP.NET Web Application in 'Visual Studio .NET 2003' I get the following error: "Visual Studio .NET has detected...
0
by: Suresh | last post by:
Hi Guys I have Db2 server installed on remote server. i am connecting to that remote server by using VPN. I want to connect that remote DB2 server instance using my local machine DB2...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
5
by: Cichy | last post by:
Hello, I'm writing a Client-Server application using sockets (asynchronous). There is a Server (Master) which accepts incoming connections, and Client (Slave). Afetr establishing connections...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.