A Simple Command Line Client and Server Program

This simple example shows the basic steps that are needed in order to write a simple client server program in VB.net, this example is command line based to keep the lines of code clear, (remember just

ITPro Today

October 19, 2003

2 Min Read
ITPro Today logo

This simple example shows the basic steps that are needed in order to write a simple client server program in VB.net, this example is command line based to keep the lines of code clear, (remember just because its VB doesnt mean it has to be graphical.The client Sub Main() Dim tcpc As TcpClient = New TcpClient() Dim read(32) As Byte 'Check there is one command line argument present Dim args() As String = Split(Command()) If Command().Length = 0 Or args.Length 1 ThenConsole.WriteLine("Please specify a server name in the _ command line") Exit Sub End If 'Verify the server exists Dim server As String = args(0) Try Dim ipInfo As IPHostEntry = Dns.GetHostByName(server) Catch socketExcep As SocketExceptionConsole.WriteLine("Cannot find server " & server & vbCrLf & socketExcep.ToString) Exit Sub End Try 'Try to connect to the server Try tcpc.Connect(server, 13) 'Get the stream Dim s As Stream = tcpc.GetStream 'Read the stream and convert it to ASCII Dim bytes As Integer = s.Read(read, 0, read.Length) Dim time As String = Encoding.ASCII.GetString(read) 'Display the data Console.WriteLine("Received " & bytes & " bytes") Console.WriteLine("Current date and time is: " & time) tcpc.Close() catch any exceptions Catch ex As ExceptionConsole.WriteLine("Exception connecting to server", ex.ToString()) End Try Console.WriteLine("Press ENTER to exit") Console.Read() End SubThe serverSub Main() Dim a As Boolean = True set the tcp Listeners Dim tcpL As TcpListener tcpL = New TcpListener(13) tcpL.Start()Console.WriteLine("Waiting for clients to connect press Ctrl+c to quit") Do While a = True Dim s As Socket = tcpL.AcceptSocket Dim myDate As String Dim myTime As String Encode the data to be transfered Dim ASCII As Encoding = Encoding.ASCII Dim anASCStream As NetworkStream myDate = Date.Now myTime = Date.Now.TimeOfDay.ToString Dim DateTime() As Byte = ASCII.GetBytes(myDate) s.Send(DateTime, DateTime.Length, 0) Console.WriteLine(DateTime) Loop End SubAs you can see from the code it is a simple process to encode the data you wish to transfer, and then send the data. Encode the data to be transferred Dim ASCII As Encoding = Encoding.ASCII set the network stream Dim anASCStream As NetworkStream put together the data to be sent, in this case the date and time myDate = Date.Now myTime = Date.Now.TimeOfDay.ToString encode the data ready to be sent Dim DateTime() As Byte = ASCII.GetBytes(myDate) send the data s.Send(DateTime, DateTime.Length, 0)This is all you need.You then only need these few lines of code to receive and display the data sentConnect to the server tcpc.Connect(server, 13) 'Get the data stream Dim s As Stream = tcpc.GetStream 'Read the stream and convert it to ASCII Dim bytes As Integer = s.Read(read, 0, read.Length) Dim time As String = Encoding.ASCII.GetString(read) 'Display the data Console.WriteLine("Received " & bytes & " bytes") Console.WriteLine("Current date and time is: " & time) Remember keep it tidy close youre connection tcpc.Close()This code can be easily edited to use a form and text boxes rather than the command line.So what are you waiting for, its time to write youre own messaging client.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like