|
-
Jan 30th, 2009, 11:03 PM
#1
Thread Starter
Member
TcpListener Freeze Problem
I am creating a TCP Listener and listener and I am using the code from the MSDN pages located here:
For some reason I am having a problem with the listener code freezing up on me. Does anyone know what might be causing this?
Here is the Listener:
Code:
Class MyTcpListener
Public Shared Sub Main()
Dim server As TcpListener
server = Nothing
Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing
' Enter the listening loop.
While True
Form2.TextBox2.Text = "Waiting for a connection... "
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Form2.TextBox2.Text = "Connected!"
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Form2.TextBox2.Text = "Received: {0}" & data
' Process the data sent by the client.
data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
Form2.TextBox2.Text = "Sent: {0}" & data
i = stream.Read(bytes, 0, bytes.Length)
End While
' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try
Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
Console.Read()
End Sub 'Main
End Class 'MyTcpListener
Here is the Client:
Code:
Shared Sub Connect(ByVal server As [String], ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 13000
Dim client As New TcpClient(server, port)
' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)
' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty
' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
Console.WriteLine("Received: {0}", responseData)
' Close everything.
stream.Close()
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
Console.Read()
End Sub 'Connect
The Exception I am getting is
Code:
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
Any other code you need just let me know. Thanks in advance for the replies.
Last edited by g19max1; Jan 30th, 2009 at 11:26 PM.
-
Jan 30th, 2009, 11:51 PM
#2
Re: TcpListener Freeze Problem
What line does the debugger highlight when it throws the error?
-
Jan 31st, 2009, 09:00 AM
#3
Re: TcpListener Freeze Problem
I am not entirely sure but if you are executing on the same computer then the exception maybe because you are using the same port for those two applications?
-
Jan 31st, 2009, 11:56 AM
#4
Thread Starter
Member
Re: TcpListener Freeze Problem
MaximilianMayrhofer:
Strangely It doesn't highlight the line as it would for a normal exception, it just prints the error in the Immediate Window while debugging.
Here is the block of code in the tcplistener section that it shows the message on:
Code:
Try
Catch
Dim client As TcpClient = server.AcceptTcpClient()
Form2.TextBox2.Text = "Connected!"
dee-u
This could also be a possibility because I am testing it on the same computer. Unfortunately I do not have a second machine to test it on right now so any other suggestions or if you are sure this might be the problem, I really appreciate it. Thanks.
-
Jan 31st, 2009, 12:00 PM
#5
Re: TcpListener Freeze Problem
Could it be that the exception is written there since you are using this?
Code:
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
-
Jan 31st, 2009, 12:22 PM
#6
Thread Starter
Member
Re: TcpListener Freeze Problem
Hmm, Well that would seem logical. However, I am very new to network programming and only been in VB coding for around 1 year.
So, what would be the best method to fix this issue?
Oh I would also like to add that I am doing this all within one application. The listener and the client are just two different forms. Could this be why I am receiving an error?
Last edited by g19max1; Jan 31st, 2009 at 12:29 PM.
-
Jan 31st, 2009, 04:26 PM
#7
Thread Starter
Member
Re: TcpListener Freeze Problem
Ideas anyone?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|