Torna al Thread

Imports System Imports System.Net Imports System.Net.Sockets Public Class tcmlistenersample Shared Sub usage() Console.WriteLine(" Uso: TcpListnerSampleVB-") Console.WriteLine() Console.WriteLine(" Available options-") Console.WriteLine(" -l address Local address for TCP server to listen on-") Console.WriteLine(" -p port Local port for TCP server to listen on-") Console.WriteLine(" -x size Size of send and receive buffers-") Console.WriteLine() End Sub Shared Sub Main() Dim listenInterface As IPAddress = IPAddress.Any Dim listenPort As Short = 10005 Dim bufferSize As Integer = 4096 Dim a As Integer Dim args As String() = Environment.GetCommandLineArgs() 'usage() For a = 1 To args.GetUpperBound(0) - 1 Try Dim CurArg() As Char = args(a).ToCharArray(0, args(a).Length) If (CurArg(0) = "-") Or (CurArg(0) = "/") Then Select Case Char.ToLower(CurArg(1), System.Globalization.CultureInfo.CurrentCulture) Case "|" ' l'interfaccia locale del server e in ascolto a = a + 1 listenInterface = IPAddress.Parse(args(a)) Case "p" 'numero della porta per la destinazione a = a + 1 listenPort = System.Convert.ToInt16(args(a)) Case "x" ' grandezza dell'invio e ricezione del buffer a = a + 1 bufferSize = System.Convert.ToInt32(args(a)) Case Else usage() Exit Sub End Select End If Catch ex As Exception usage() Exit Sub End Try Next Dim tcpServer As TcpListener = Nothing Dim tcpclient As TcpClient = Nothing Dim tcpstream As NetworkStream = Nothing Dim sendbuffer(bufferSize) As Byte Dim receiveBuffer(bufferSize) As Byte Dim byteCount() As Byte Dim byteToRead As Integer = 0 Dim nextReadCount, rc As Integer 'inizializzazione dell'invio del buffer Console.WriteLine("inizializzazione del buffer ...") For a = 0 To sendbuffer.Length - 1 sendbuffer(a) = CByte(65) Next Try 'creazione del tcp server Console.WriteLine("TCP creato sull'indirizzo {0} e sulla porta numero{1}", listenInterface.ToString(), listenPort.ToString) 'Inizio ascolto della connessione Console.WriteLine("Avviato l'ascolto sulla connessione") tcpServer = New TcpListener(listenInterface, listenPort) tcpServer.Start() 'attende la connessione con il client Console.WriteLine("in attesa della connessionecon il client") tcpclient = tcpServer.AcceptTcpClient() 'Prendi il NetworkStream in modo che possiamo fare di lettura e scrittura sul client di connessione Console.WriteLine("Ottenere il NetworkStream per leggere e scrivere nel client di connessione") tcpstream = tcpclient.GetStream() byteCount = BitConverter.GetBytes(byteToRead) 'Prima di leggere il numero di byte il client invia Console.WriteLine("La lettura del numero di byte il client invia...") tcpstream.Read(byteCount, 0, byteCount.Length) byteToRead = BitConverter.ToInt32(byteCount, 0) 'riceve i dati Console.WriteLine("Receive, Read & display data...") While (byteToRead > 0) If (byteToRead < receiveBuffer.Length) Then nextReadCount = byteToRead Else nextReadCount = receiveBuffer.Length End If ' Read some data rc = tcpstream.Read(receiveBuffer, 0, nextReadCount) ' Display what we read Dim readText As String = System.Text.Encoding.ASCII.GetString(receiveBuffer, 0, rc) Console.WriteLine("Received: {0}", readText) byteToRead -= rc End While Console.WriteLine("invi i numero di byte del server e responde con .... ") byteCount = BitConverter.GetBytes(sendbuffer.Length) tcpstream.Write(byteCount, 0, sendbuffer.Length) Console.WriteLine("Chiusura sul client") tcpstream.Close() tcpstream = Nothing tcpclient.Close() tcpclient = Nothing Catch ex As Exception ' Exceptions on the TcpListener are caught here Console.WriteLine("Socket error occurred: {0}", Err.Description) '' Catch err As System.IO.IOException ' Exceptions on the NetworkStream are caught here ' Console.WriteLine("I/O error: {0}", Err.Message) Finally ' Close any remaining open resources Console.WriteLine("Closing any remaining open resources...") If (Not IsNothing(tcpServer)) Then tcpServer.Stop() End If If (Not IsNothing(tcpstream)) Then tcpstream.Close() End If If (Not IsNothing(tcpclient)) Then tcpclient.Close() End If End Try End Sub End Class
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5