Chat Program crashes

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
Does anyone know why this code crashes my machine? also does it look like it should work.
VB.NET:
Imports[/color][/size][size=2] System.Net.Sockets[/size]
[size=2][color=#0000ff]Imports[/color][/size][size=2] System.Text
[/size][size=2][color=#0000ff]Public[/color][/size][size=2][color=#0000ff]Class[/color][/size][size=2] Form1
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] Client [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] TcpClient[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] port [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 8000[/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] Listener [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] TcpListener(port)
[/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ButtonSend_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ButtonSend.Click
 
Client.Connect("127.0.0.1", port)
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] networkStream [/size][size=2][color=#0000ff]As[/color][/size][size=2] NetworkStream = Client.GetStream()[/size]
[size=2][color=#0000ff]If[/color][/size][size=2] networkStream.CanWrite [/size][size=2][color=#0000ff]And[/color][/size][size=2] networkStream.CanRead [/size][size=2][color=#0000ff]Then[/color][/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] sendBytes [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Byte[/color][/size][size=2]() = Encoding.ASCII.GetBytes("Is anybody there")[/size]
[size=2]networkStream.Write(sendBytes, 0, sendBytes.Length)
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] bytes(Client.ReceiveBufferSize) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Byte
 
[/color][/size][size=2]networkStream.Read(bytes, 0, [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](Client.ReceiveBufferSize)) 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] returndata [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = Encoding.ASCII.GetString(bytes) 
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].RichTextBoxClient.Text = returndata
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]If
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] ButtonStart_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] ButtonStart.Click
 
Listener.Start()
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].RichTextBoxServer.Text = "Waiting for connection..."
[/size][size=2][color=#0000ff]Try
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] tcpClient [/size][size=2][color=#0000ff]As[/color][/size][size=2] TcpClient = Listener.AcceptTcpClient()
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].RichTextBoxServer.Text = "Connection accepted"
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] networkStream [/size][size=2][color=#0000ff]As[/color][/size][size=2] NetworkStream = tcpClient.GetStream()
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] bytes(tcpClient.ReceiveBufferSize) [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Byte
[/color][/size][size=2]networkStream.Read(bytes, 0, [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](tcpClient.ReceiveBufferSize))
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] clientData [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = Encoding.ASCII.GetString(bytes)
[/size][size=2][color=#0000ff]Me[/color][/size][size=2].RichTextBoxServer.Text = clientData
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] response [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Connected to server"
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sendbytes [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Byte[/color][/size][size=2]() = Encoding.ASCII.GetBytes(response)
networkStream.Write(sendbytes, 0, sendbytes.Length)
 
[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
MessageBox.Show(ex.ToString)
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try[/color][/size]
[size=2][color=#0000ff]
[/color][/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub[/color][/size]
 
[size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Class[/color][/size]
[color=#0000ff]
 
Back
Top