i build some chat, client server, but after 1 message the client send, he get disconnected any tips?
server :
client :
server :
VB.NET:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim thread As System.Threading.Thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (Button1.Text = "Server - OFF") Then
Button1.Text = "Server - ON"
MsgBox("The server is online now")
thread = New System.Threading.Thread(AddressOf server)
thread.Start()
ElseIf (Button1.Text = "Server - ON") Then
Button1.Text = "Server - OFF"
MsgBox("The server is no offline now")
End If
End Sub
Private Sub server()
Dim portNumber As Integer = 8000
Dim tcpListener As New TcpListener(portNumber)
TextBox1.Text = TextBox1.Text + "[Server] Loading Server..."
Try
Label1.Text = "Status - Server is ON"
tcpListener.Start()
TextBox1.Text = TextBox1.Text + vbCrLf & "[Server] Server Loaded succesfully, waiting for client"
While (True)
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Dim networkStream As NetworkStream = tcpClient.GetStream()
TextBox1.Text = TextBox1.Text + vbCrLf & "Client connection request aproved"
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
TextBox1.Text = TextBox1.Text + vbCrLf & "[Client] " + clientdata
End While
Catch ex As SocketException
TextBox1.Text = TextBox1.Text + "Uknown error, server load fail!"
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
End Sub
End Class
VB.NET:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim thread As System.Threading.Thread
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Private Sub connect()
Try
clientSocket.Connect("127.0.0.1", 8000)
Label1.Text = "Status - Server Connected "
Catch ex As SocketException
MessageBox.Show("Unknown Error occurd", "Error")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
connect()
End Sub
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
thread = New System.Threading.Thread(AddressOf client)
thread.Start()
End Sub
Private Sub client()
If TextBox2.Text = "" Then
MessageBox.Show("Message cant be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Try
Dim serverStream As NetworkStream = clientSocket.GetStream()
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(TextBox2.Text)
serverStream.Write(outStream, 0, outStream.Length)
Catch ex As SocketException
MessageBox.Show("Uknown Error occurd", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
End Sub
End Class