Course Info
Policies
Schedule
Homework
Labs
Exams
Lectures

TA Office hours

Gallery

CLIENT CODE

Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Client

Public Shared Sub Main()
Dim tcpc As New TCPClient()
Dim read(35) As Byte
Dim args As String() = Environment.GetCommandLineArgs()

If (args.Length < 2) Then
Console.WriteLine("Please specify a server name in the command line")
Exit Sub
End If

Dim server As String = args(1)

' Verify that the server exists
Try
DNS.GetHostByName(server)
Catch
Console.WriteLine("Cannot find server: {0}", server)
Exit Sub
End Try


' Try to connect to the server
tcpc.Connect(server,13)

' Get the stream
Dim s As Stream
Try
s = tcpc.GetStream()
Catch exc As InvalidOperationException
Console.WriteLine("Cannot connect to server: {0}", server)
Exit Sub
End Try

' Read the stream and convert it to ASII
Dim bytes As Integer = s.Read(read, 0, read.Length)
Dim Time As String = Encoding.ASCII.GetString(read)

' Display the data
Console.WriteLine("Received {0} bytes", bytes)
Console.WriteLine("Current date and time is: {0}", Time)

tcpc.Close()

' Wait for user response to exit
Console.WriteLine("Press Return to exit")
Console.Read()
End Sub

End Class

 

SERVER CODE

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.Globalization

Public Class DateTimeServer
Public Shared Sub Main()
Dim now As Date
Dim strDateLine As String
Dim ASCII As Encoding = Encoding.ASCII

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture

Try
Dim tcpl As New TCPListener(13) 'listen on port 13
tcpl.Start()

Console.WriteLine("Waiting for clients to connect")
Console.WriteLine("Press Ctrl+c to Quit...")

While (True)
' Accept will block until someone connects
Dim s As Socket = tcpl.AcceptSocket()

' Get the current date and time then concatenate it
' into a string
now = DateTime.Now
strDateLine = now.ToShortDateString() + " " + now.ToLongTimeString()

' Convert the string to a Byte Array and send it
Dim byteDateLine() As Byte = ASCII.GetBytes(strDateLine.ToCharArray())
s.Send(byteDateLine, byteDateLine.Length, SocketFlags.None)
s.Close()
Console.WriteLine("Sent {0}", strDateLine)
End While

Catch socketError As SocketException
If (socketError.ErrorCode) = 10048 Then
Console.WriteLine("Connection to this port failed. There is another server is listening on this port.")
End If
End Try

End Sub
End Class