Chat Server schließt beim beenden des Clients. (Trennungsfehler?)

  • VB.NET

Es gibt 14 Antworten in diesem Thema. Der letzte Beitrag () ist von XaioXiin.

    Chat Server schließt beim beenden des Clients. (Trennungsfehler?)

    Guten Tag,

    Ich habe ein Chat Porgramm erstellt und dieses funktioniert auch, jedoch hängt sich der Server beim verlassen des Clients auf!
    Dieser Fehler erscheint dann:






    Das ist der Client Code :

    VB.NET-Quellcode

    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Public Class Form1
    4. Dim clientSocket As New System.Net.Sockets.TcpClient()
    5. Dim serverStream As NetworkStream
    6. Dim readData As String
    7. Dim infiniteCounter As Integer
    8. Private Sub Button1_Click(ByVal sender As System.Object, _
    9. ByVal e As System.EventArgs) Handles Button1.Click
    10. Dim outStream As Byte() = _
    11. System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
    12. serverStream.Write(outStream, 0, outStream.Length)
    13. serverStream.Flush()
    14. TextBox2.Text = ""
    15. End Sub
    16. Private Sub msg()
    17. If Me.InvokeRequired Then
    18. Me.Invoke(New MethodInvoker(AddressOf msg))
    19. Else
    20. TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + readData
    21. End If
    22. End Sub
    23. Private Sub Button2_Click(ByVal sender As System.Object, _
    24. ByVal e As System.EventArgs)
    25. Me.Visible = False
    26. End Sub
    27. Private Sub getMessage()
    28. For infiniteCounter = 1 To 2
    29. infiniteCounter = 1
    30. serverStream = clientSocket.GetStream()
    31. Dim buffSize As Integer
    32. Dim inStream(10024) As Byte
    33. buffSize = clientSocket.ReceiveBufferSize
    34. serverStream.Read(inStream, 0, buffSize)
    35. Dim returndata As String = _
    36. System.Text.Encoding.ASCII.GetString(inStream)
    37. readData = "" + returndata
    38. msg()
    39. Next
    40. End Sub
    41. Private Sub Form1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter
    42. End Sub
    43. Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    44. End Sub
    45. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    46. ControlBox = False
    47. textbox516.Text = Form2.textbox515.Text
    48. readData = "Mit Server verbunden."
    49. msg()
    50. clientSocket.Connect("192.168.192.40", 22045)
    51. 'Label1.Text = "Client Socket Program - Server Connected ..."
    52. serverStream = clientSocket.GetStream()
    53. Dim outStream As Byte() = _
    54. System.Text.Encoding.ASCII.GetBytes(textbox516.Text + "$")
    55. serverStream.Write(outStream, 0, outStream.Length)
    56. serverStream.Flush()
    57. Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf getMessage)
    58. ctThread.Start()
    59. End Sub
    60. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    61. Me.Close()
    62. End Sub
    63. Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    64. End Sub
    65. End Class


    Und dies ist der Server Code :

    VB.NET-Quellcode

    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Module Servermodul
    4. Dim clientsList As New Hashtable
    5. Sub Main()
    6. Dim serverSocket As New TcpListener(22045)
    7. Dim clientSocket As TcpClient
    8. Dim infiniteCounter As Integer
    9. Dim counter As Integer
    10. serverSocket.Start()
    11. msg("#########################################")
    12. msg("# Server gestartet. Willkommen #")
    13. msg("# Chatroom ist jetzt geoeffnet #")
    14. msg("#########################################")
    15. counter = 0
    16. infiniteCounter = 0
    17. For infiniteCounter = 1 To 2
    18. infiniteCounter = 1
    19. counter += 1
    20. clientSocket = serverSocket.AcceptTcpClient()
    21. Dim bytesFrom(10024) As Byte
    22. Dim dataFromClient As String
    23. Dim networkStream As NetworkStream = _
    24. clientSocket.GetStream()
    25. networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    26. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
    27. dataFromClient = _
    28. dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    29. clientsList(dataFromClient) = clientSocket
    30. broadcast(dataFromClient + " hat den Chat betreten! ", dataFromClient, False)
    31. msg(dataFromClient + " hat den Chat betreten! ")
    32. Dim client As New handleClinet
    33. client.startClient(clientSocket, dataFromClient, clientsList)
    34. Next
    35. clientSocket.Close()
    36. serverSocket.Stop()
    37. msg("exit")
    38. Console.ReadLine()
    39. End Sub
    40. Sub msg(ByVal mesg As String)
    41. mesg.Trim()
    42. Console.WriteLine(" Console: " + mesg)
    43. End Sub
    44. Private Sub broadcast(ByVal msg As String, _
    45. ByVal uName As String, ByVal flag As Boolean)
    46. Dim Item As DictionaryEntry
    47. For Each Item In clientsList
    48. Dim broadcastSocket As TcpClient
    49. broadcastSocket = CType(Item.Value, TcpClient)
    50. Dim broadcastStream As NetworkStream = _
    51. broadcastSocket.GetStream()
    52. Dim broadcastBytes As [Byte]()
    53. If flag = True Then
    54. broadcastBytes = Encoding.ASCII.GetBytes(uName + " : " + msg)
    55. Else
    56. broadcastBytes = Encoding.ASCII.GetBytes(msg)
    57. End If
    58. broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
    59. broadcastStream.Flush()
    60. Next
    61. End Sub
    62. Public Class handleClinet
    63. Dim clientSocket As TcpClient
    64. Dim clNo As String
    65. Dim clientsList As Hashtable
    66. Public Sub startClient(ByVal inClientSocket As TcpClient, _
    67. ByVal clineNo As String, ByVal cList As Hashtable)
    68. Me.clientSocket = inClientSocket
    69. Me.clNo = clineNo
    70. Me.clientsList = cList
    71. Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
    72. ctThread.Start()
    73. End Sub
    74. Private Sub doChat()
    75. Dim infiniteCounter As Integer
    76. Dim requestCount As Integer
    77. Dim bytesFrom(10024) As Byte
    78. Dim dataFromClient As String
    79. Dim sendBytes As [Byte]()
    80. Dim serverResponse As String
    81. Dim rCount As String
    82. requestCount = 0
    83. For infiniteCounter = 1 To 2
    84. infiniteCounter = 1
    85. Try
    86. requestCount = requestCount + 1
    87. Dim networkStream As NetworkStream = _
    88. clientSocket.GetStream()
    89. networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    90. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
    91. dataFromClient = _
    92. dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    93. msg("From client - " + clNo + " : " + dataFromClient)
    94. rCount = Convert.ToString(requestCount)
    95. broadcast(dataFromClient, clNo, True)
    96. Catch ex As Exception
    97. MsgBox(ex.ToString)
    98. End Try
    99. Next
    100. End Sub
    101. End Class
    102. End Module




    Ich hoffe mir kann jemand helfen, denn ich weiß absolut nicht wie ich dies beheben kann. :(
    Ich habe dies nun genau so eingetragen, doch der Server stürzt wie folgt mit der selben fehlermeldung ab, ich habe beim Client dies eingetragen :

    VB.NET-Quellcode

    1. Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    2. serverStream.Close()
    3. End Sub
    Nachdem, immernoch das selbe :

    VB.NET-Quellcode

    1. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2. serverStream.Close()
    3. clientSocket.Close()
    4. End Sub
    Eigentlich müsste das ganze funktionieren. Das hat es zumindest bei mir immer.
    Versuch das mal, ansonsten weiß ich leider nicht weiter:

    VB.NET-Quellcode

    1. serverStream.Flush()
    2. serverStream.Close()
    3. clientSocket.Close()


    RushDen schrieb:

    Wenn du ein Try catch drumrum gebaut hast, dann stürzt der Server normalerweise nicht ab sondern läuft nach der Exception weiter?

    Das wäre aber auch keine Lösung.


    Achja und ich hätte da noch zwei kleine Codeverbesserungen.
    Nutze Anstatt TextBox2.Text = "" lieber TextBox2.Clear()
    Und Anstatt: TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + readData besser TextBox1.Text += Environment.NewLine + " >> " + readData

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „GermanElectronix“ ()

    Das hier ist beim Server :




    Ist das relevant?

    Betroffen ist das makierte rote:

    VB.NET-Quellcode

    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Module Servermodul
    4. Dim clientsList As New Hashtable
    5. Sub Main()
    6. Dim serverSocket As New TcpListener(22045)
    7. Dim clientSocket As TcpClient
    8. Dim infiniteCounter As Integer
    9. Dim counter As Integer
    10. serverSocket.Start()
    11. msg("#########################################")
    12. msg("# Server gestartet. Willkommen #")
    13. msg("# Chatroom ist jetzt geoeffnet #")
    14. msg("#########################################")
    15. counter = 0
    16. infiniteCounter = 0
    17. For infiniteCounter = 1 To 2
    18. infiniteCounter = 1
    19. counter += 1
    20. clientSocket = serverSocket.AcceptTcpClient()
    21. Dim bytesFrom(10024) As Byte
    22. Dim dataFromClient As String
    23. Dim networkStream As NetworkStream = _
    24. clientSocket.GetStream()
    25. networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
    26. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
    27. dataFromClient = _
    28. dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    29. clientsList(dataFromClient) = clientSocket
    30. broadcast(dataFromClient + " hat den Chat betreten! ", dataFromClient, False)
    31. msg(dataFromClient + " hat den Chat betreten! ")
    32. Dim client As New handleClinet
    33. client.startClient(clientSocket, dataFromClient, clientsList)
    34. Next
    35. [color=#FF0000]clientSocket.Close()[/color] <-------------------------------- HIER DAS
    36. serverSocket.Stop()
    37. msg("exit")
    38. Console.ReadLine()
    39. End Sub