freezing issue with simple client/server
-
Hello, I am just learning the tcp/ip functions etc under vb.net so please look over me if this is obviouse. I have been all over looking into any functions that I didn't totaly understand and can't see that I am doing anything wrong (i obviousely am though) everything "works", client connects to server and when anything is sent to the server it starts the mp3 and passes all messages the way i ment for it to except that my server is supposed to display the data it recieves in a window (that worked untill i changed some things) and my server will hang after I pass a few strings to it from the client. The hanging is what I can't figure out. I can debug the other no problem. Anyway here is the code, please look over how sloppy it has gotten after hours of changeing things arround. Server : Imports System.Net Imports System.Net.Sockets Imports System.Text Public Class Form1 Inherits System.Windows.Forms.Form 'Windows Form Designer Generated Code was here "removed for size of post" Const port As Int32 = 8353 Dim localIp As IPAddress = IPAddress.Parse("127.0.0.1") Dim server As New TcpListener(localIp, port) Dim netStream As NetworkStream Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'start tcplistener on localhost: 8353 server.Start() Try 'accept connections Dim client As TcpClient = server.AcceptTcpClient() 'get stream netStream = client.GetStream While netStream.CanRead And netStream.CanWrite 'read stream into a byte array Dim reciveBufferSize(client.ReceiveBufferSize) As Byte netStream.Read(reciveBufferSize, 0, CInt(client.ReceiveBufferSize)) 'convert data from the client into a usable string variable Dim dataFromClient As String = Encoding.ASCII.GetString(reciveBufferSize) 'do something with the data TextBox2.Text = dataFromClient 'send a response to the client Dim serverResponse As String = "Transmision completed" Dim sendBufferSize As [Byte]() = Encoding.ASCII.GetBytes(serverResponse) netStream.Write(sendBufferSize, 0, sendBufferSize.Length) 'show what was sent TextBox1.Text = serverResponse If dataFromClient.ToString > "" Then Syst
-
Hello, I am just learning the tcp/ip functions etc under vb.net so please look over me if this is obviouse. I have been all over looking into any functions that I didn't totaly understand and can't see that I am doing anything wrong (i obviousely am though) everything "works", client connects to server and when anything is sent to the server it starts the mp3 and passes all messages the way i ment for it to except that my server is supposed to display the data it recieves in a window (that worked untill i changed some things) and my server will hang after I pass a few strings to it from the client. The hanging is what I can't figure out. I can debug the other no problem. Anyway here is the code, please look over how sloppy it has gotten after hours of changeing things arround. Server : Imports System.Net Imports System.Net.Sockets Imports System.Text Public Class Form1 Inherits System.Windows.Forms.Form 'Windows Form Designer Generated Code was here "removed for size of post" Const port As Int32 = 8353 Dim localIp As IPAddress = IPAddress.Parse("127.0.0.1") Dim server As New TcpListener(localIp, port) Dim netStream As NetworkStream Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'start tcplistener on localhost: 8353 server.Start() Try 'accept connections Dim client As TcpClient = server.AcceptTcpClient() 'get stream netStream = client.GetStream While netStream.CanRead And netStream.CanWrite 'read stream into a byte array Dim reciveBufferSize(client.ReceiveBufferSize) As Byte netStream.Read(reciveBufferSize, 0, CInt(client.ReceiveBufferSize)) 'convert data from the client into a usable string variable Dim dataFromClient As String = Encoding.ASCII.GetString(reciveBufferSize) 'do something with the data TextBox2.Text = dataFromClient 'send a response to the client Dim serverResponse As String = "Transmision completed" Dim sendBufferSize As [Byte]() = Encoding.ASCII.GetBytes(serverResponse) netStream.Write(sendBufferSize, 0, sendBufferSize.Length) 'show what was sent TextBox1.Text = serverResponse If dataFromClient.ToString > "" Then Syst
I'm not an expert on the sockets stuff, but my gut feeling makes me wonder if the loops you are in need a DoEvents to keep the events running through the rest of your application. You could also use the system.diagnostics namespace to get access to the Debug object and start writing some assert and trace statements, that will show where your code is stopping. It really might just be that it looks like its hung when in fact its in such a tight loop it can't do any of the event stuff it needs to do. So the doevents will yield for a time and let the system do its thing. Nursey
-
I'm not an expert on the sockets stuff, but my gut feeling makes me wonder if the loops you are in need a DoEvents to keep the events running through the rest of your application. You could also use the system.diagnostics namespace to get access to the Debug object and start writing some assert and trace statements, that will show where your code is stopping. It really might just be that it looks like its hung when in fact its in such a tight loop it can't do any of the event stuff it needs to do. So the doevents will yield for a time and let the system do its thing. Nursey
Thanks, I haven't gottin real deap into the diagnostics namespaces but the whole purpose is to learn so I'll look into it. Defenetly sounds like a good thing to learn to use for debugging. Thanks again. 2 U.S. coins equal 30 cents and one is NOT a nickle. Hmm..