PDA

View Full Version : vb.net tcplistener ,non va con il ciclo infinito


AnD01
12-02-2006, 09:17
Salve ragazzi ancora una volta ho bisogno di voi... ho fatto molte ricerche , ma siccome non sono riuscito a risolvere il mio problema allora ho deciso di postare qui.
Vi spiego subito il mio problema: Sto creando un applicazione vb.net che lavora con la classe tcplistener e come ho trovato uin tutte le guide e how to viene utilizzata la seguente struttura: (presa da msdn)

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic


Class MyTcpListener

Public Shared Sub Main()

Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

Dim server As 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
Console.Write("Waiting for a connection... ")

' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("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)
Console.WriteLine([String].Format("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)
Console.WriteLine([String].Format("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)
End Try

Console.WriteLine(ControlChars.Cr + "Hit enter to continue...")
Console.Read()
End Sub 'Main

End Class 'MyTcpListener

il mio problema è il ciclo while true...end while, infatti mettendolo e mettendo la mia applicazione in ascolto il programma si blocca e sono costretto a chiuderlo. Togliendo il ciclo ho verificato che al contrario il mio server funziona e accetta connessioni...solo una naturalmente non essendoci il ciclo...perché? dove sbaglio???
grazie mille a tutti.

ah vi riporto anche il mio codice... nel caso ci fossero degli errori che potreste trovare ad occhio:


Private Sub ascolta(ByVal sender As System.Object, ByVal e As System.EventArgs)


Dim data As [String] = Nothing
Dim bytes(1024) As [Byte]
Try
Dim sHostName As String
sHostName = Dns.GetHostName()
Dim ipE As IPHostEntry = Dns.GetHostEntry(sHostName)
Dim IpA() As IPAddress = ipE.AddressList
localaddress = IPAddress.Parse(IpA(0).ToString)
Dim server As New TcpListener(localaddress, port)
server.Start()
Dim i As Integer = 0
Dim pos As Integer
Weblog.Navigate(TextBox1.Text)
' quiva il ciclo



Dim client As TcpClient = server.AcceptTcpClient
If client.Connected Then
Dim ia As Int32
data = Nothing
Dim stream As NetworkStream = client.GetStream()
ia = stream.Read(bytes, 0, bytes.Length)

data = System.Text.Encoding.ASCII.GetString(bytes, 0, ia)

If data.Substring(0, 2) = "@@" Then
listmsg.Items.Add(data.Substring(2))
End If

If data.Substring(0, 2) = "$$" Then
pos = data.IndexOf("@")
listusers.Items.Add(data.Substring(pos + 1))
i = i + 1
vetall(i, 1) = data.Substring(pos + 1)
vetall(i, 2) = data.Substring(0, pos)
End If

If data.Substring(0, 2) = "%%" Then
pos = data.IndexOf("@")
listmsg.Items.Add("<" & data.Substring(0, pos) & "> " & data.Substring(pos + 1))
End If

data = data.ToUpper()


End If
client.Close()
Catch err As SocketException
listmsg.Items.Add(err)
End Try
End Sub

AnD01
13-02-2006, 18:43
help aiutatemi ragazzi perfavore!!

AnD01
15-02-2006, 16:36
ragazzi aiutatemi per grazia!! :help: