Date time client based on TcpClient : TcpClient « Socket Network « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Socket Network » TcpClient 
22.12.2.Date time client based on TcpClient
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class DateTimeClient
  Public Shared Sub Main()
    Dim args As String() = Environment.GetCommandLineArgs()

    Dim serverHost As String = "127.0.0.1"

    Try
      Dns.GetHostByName(serverHost)
    Catch ex As Exception
      Console.WriteLine("Can not find server, {0}", serverHost)
      Exit Sub
    End Try

    Dim tcpClient As New TcpClient()
    Dim networkStream As Stream

    Try
      tcpClient.Connect(serverHost, 13)

      networkStream = tcpClient.GetStream()
    Catch exc As Exception
      Console.WriteLine("Can not connect to {0}", serverHost)
      Exit Sub
    End Try

    Try
      If networkStream.CanRead Then
        Dim recvbytes(tcpClient.ReceiveBufferSizeAs Byte
        Dim As Integer = networkStream.Read(recvbytes, 0, CInt(tcpClient.ReceiveBufferSize))
        Dim datetime As String = Encoding.ASCII.GetString(recvbytes, 0, i)

        Console.WriteLine("Receive {0} bytes.", i)
        Console.WriteLine("Current server date/Time: {0}", datetime)
      Else
        Console.WriteLine("Network Input Stream is unreadable.")
        tcpClient.Close()
        Return
      End If
    Catch ec As Exception
      Console.WriteLine(ec.StackTrace.ToString())
      Exit Sub
    End Try

    tcpClient.Close()

  End Sub
End Class
Can not connect to 127.0.0.1
22.12.TcpClient
22.12.1.Create TcpClient
22.12.2.Date time client based on TcpClient
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.