VB.Net by API/System.Net/NetworkStream — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:49, 26 мая 2010

NetworkStream.Read

  
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Imports System.Text
Public Class Tester
    
    Public Shared Sub Main
        Dim myTcpClient As New TcpClient()
        Dim myNetworkStream As NetworkStream
        Try
            myTcpClient.Connect("127.0.0.1", 12345)
            myNetworkStream = myTcpClient.GetStream()
        Catch ex As ArgumentOutOfRangeException
            Console.WriteLine(ex.Message)
        Catch ex As SocketException
            Console.WriteLine(ex.Message)
        End Try
        Dim myBytes() As Byte = Encoding.ASCII.GetBytes("asdf")
        myNetworkStream = myTcpClient.GetStream()
        myNetworkStream.Write(myBytes, 0, myBytes.Length)
        Dim bufferSize As Integer = myTcpClient.ReceiveBufferSize
        Dim myBufferBytes(bufferSize) As Byte
        myNetworkStream.Read(myBufferBytes, 0, bufferSize)
        Console.WriteLine(Encoding.ASCII.GetString(myBufferBytes, 0, bufferSize))
    End Sub
End Class


NetworkStream.Write

  
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Imports System.Text
Public Class Tester
    
    Public Shared Sub Main
        Dim myTcpClient As New TcpClient()
        Dim myNetworkStream As NetworkStream
        Try
            myTcpClient.Connect("127.0.0.1", 12345)
            myNetworkStream = myTcpClient.GetStream()
        Catch ex As ArgumentOutOfRangeException
            Console.WriteLine(ex.Message)
        Catch ex As SocketException
            Console.WriteLine(ex.Message)
        End Try
        Dim myBytes() As Byte = Encoding.ASCII.GetBytes("asdf")
        myNetworkStream = myTcpClient.GetStream()
        myNetworkStream.Write(myBytes, 0, myBytes.Length)
        Dim bufferSize As Integer = myTcpClient.ReceiveBufferSize
        Dim myBufferBytes(bufferSize) As Byte
        myNetworkStream.Read(myBufferBytes, 0, bufferSize)
        Console.WriteLine(Encoding.ASCII.GetString(myBufferBytes, 0, bufferSize))
    End Sub
End Class