Building Basic Client/Server Applications
with VS.Net
Lance Robinson
lancer@nsoftware.com
This users guide will go through some of the basic steps that one might take in order to create a simple client/server application in VS.Net, including fully implemented source code.
In this guide, we will make use of the IPPort and IPDaemon components included in the IP*Works! .Net Edition. Click here to download a fully functional free trial of this very useful toolkit. For the purposes of this document, we will assume that IP*Works! .Net Edition has already been installed on the readers computer. This toolkit includes sample code for both VB.Net and C#, but here we will be coding in VB.Net.
Basic Server
The basic building block for servers in the IP*Works! toolkit is the IPDaemon component. This is a basic TCP/IP listener. Tell the IPDaemon component what port to listen to with the LocalPort property, and set Listening to true. You now have your own server application. What it does is up to you. The IPDaemon component can be used to create an SMTP server, HTTP server, custom server, any server. In order to implement something like an SMTP server you would need to implement the SMTP protocol. In order to keep things straight-forward - we'll implement a very simple echo server. Clients can connect to the server and any data that the client sends to the server will simply be echoed back to them.
Echo Server
On our server form we'll take input for the port number that we want IPDaemon to listen on. We'll have an output textbox to output what the server receives from the client. And lastly, a "Start" button to start IPDaemon listening.
Figure 1.
The "Start" button (bRun) will simply set the Localport property to the value in the input box (tbPort.Text), set the Listening property to True, and make a note in the output textbox (tbLog) that the IPDaemon is now listening.
Private Sub bRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bRun.Click
Ipdaemon1.LocalPort = tbPort.Text
Ipdaemon1.Listening = True
bRun.Text = "Stop"
tbLog.AppendText("Starting server." & vbCrLf)
End Sub
Once the IPDaemon component is listening, it can accept and handle up to 1000 simultaneous connections (clients) at one time. If you have need for the ability to handle more than 1000 simultaneous connections, this can be arranged. Each incoming client is uniquely identified by the IPDaemon by a ConnectionID. This ConnectionID can be used to send information to a specific client. All of the IPDaemon events will report this ConnectionID in its parameter list for ease of use.
Upon the initial connection request from a client, the IPDaemon will fire a ConnectionRequest event. All connections are accepted by default, but if you desire to deny incoming connections for any reason, this is the place to do it. To deny a connection, simply set the Accept parameter to False.
After the connection is accepted (again, this is done by default, so most of the time using the ConnectionRequest event is unnecessary) a Connected event will fire. A connection id is assigned to the client, the ConnectionID parameter of the Connected event provides this information to you. When a connection is successful, the StatusCode and Description parameters will be zero and "OK", respectively. In the event of an error while connecting, these parameters will reflect that status as well.
In our echo server, we will use the Connected event only to add log entries into tbLog:
Private Sub Ipdaemon1_OnConnected(ByVal sender As Object, ByVal e As nsoftware.IPWorks.IpdaemonConnectedEventArgs) Handles Ipdaemon1.OnConnected
tbLog.AppendText(Ipdaemon1.RemoteHost(e.ConnectionId) & " has connected." & vbCrLf)
End Sub
Once we are connected to a client, we will be able to accept data from that client. The IPDaemon component also has the ability to control when a client can and cannot send it data. In order to turn off the ability to accept data from a particular client, set the AcceptData property to False for that connectionID, ie:
Ipdaemon1.AcceptData(e.ConnectionId) = True
For this example, we will always accept data from any client. Any data that the client sends to us will arrive through the DataIn event. Since we are developing an echo server, this application will immediately send any data received in the DataIn event right back to the client that sent it. The DataIn event will provide us with the Text that the client sent as well as the connection id of the client that sent it. This way we can immediately send the data straight to that connectionID.
Private Sub Ipdaemon1_OnDataIn(ByVal sender As Object, ByVal e As nsoftware.IPWorks.IpdaemonDataInEventArgs) Handles Ipdaemon1.OnDataIn
tbLog.AppendText(Ipdaemon1.RemoteHost(e.ConnectionId) & ": " & e.Text & VbCrLf)
Ipdaemon1.DataToSend(e.ConnectionId) = e.Text
End Sub
The above DataIn event subroutine outputs the IP of the client that is sending us data by using the RemoteHost property of IPDaemon, as well as the text that client sent. Then, the DataToSend property automatically sends data to the client.
The Disconnected event will fire when a client is disconnected.
Basic Client
The IPPort component of the IP*Works! toolkit is a basic TCP/IP client component. It can be used to create all kinds of client applications - FTP clients, HTTP clients (web browsers), Finger clients, custom clients, and more. Of course, the IP*Works! toolkit includes its own FTP client component, HTTP component, and many more. Simply provide the IPPort control with a remote host and remote port and use the Connect method to connect to your server. Of course, once connected, you'll have to speak the same language (protocol) that the server is.
Echo Client
We have a very simple echo server application ready to go now. So lets begin creating a simple Echo Client application. To do this, we'll use the IPPort component.
For the client form, we'll provide input boxes for a remote host and port where a server is located, as well as an input box for text to send to this server. We will provide an output textbox to display the information received from the server, as well as a "Connect" button to get connected.
The "Connect" button (bConnect), will simply attempt to connect to the specified server (tbServer) and port (tbPort) using the IPPort component. To do this, we'll call the Connect method of IPPort with parameters tbServer and tbPort.
Private Sub bConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bConnect.Click
Ipport1.Connect(tbServer.Text, tbPort.Text)
End Sub
After the connection attempt is completed, a Connected event will fire. The IPPort Connected event contains StatusCode and Description parameters which reflect the success or failure of the attempted connection. If the connection is rejected, most likely the server is either not listening, is not reachable on the network, or the client is not attempting the connection on the same port that the server is listening on.
In our echo client Connected event, we'll simply log the fact that we have connected to a server:
Private Sub Ipport1_OnConnected(ByVal sender As Object, ByVal e As nsoftware.IPWorks.IpportConnectedEventArgs) Handles Ipport1.OnConnected
tbEcho.AppendText("Connected." & vbCrLf)
End Sub
Now that we are connected to the echo server, we'll use the Send button (bSend) to send data to the server to be echoed back to us.
Private Sub bSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSend.Click
If Ipport1.Connected = True Then
Ipport1.DataToSend = tbText.Text & vbCrLf
tbEcho.AppendText("Sending '" & tbText.Text & "'." & vbCrLf)
Else
tbEcho.AppendText("You're not connected." & vbCrLf)
End If
End Sub
First we'll make sure that we are in fact connected by checking the Connected property. This property will be True if there is a connection and False otherwise. If we are connected, set the IPPort DataToSend property equal to the text we want to send and it will automatically be sent to the server. We'll also add some logging code to make a note of exactly what we are sending to the server.
Now, just like in the server IPDaemon component, the IPPort component will fire a DataIn event when data is received from the server. In our echo client we will only log this information to the textbox on the form:
Private Sub Ipport1_OnDataIn(ByVal sender As Object, ByVal e As nsoftware.IPWorks.IpportDataInEventArgs) Handles Ipport1.OnDataIn
tbEcho.AppendText("Received '" & e.Text & "' from " & Ipport1.RemoteHost & vbCrLf)
End Sub
Run the Echo Server and Client at the same time. Start the server listening. Connect to the server from the client by clicking on the "Connect" button. Send data to the server by filling text in the specified input box and click "Send". The Server will receive the information and send it right back to you.
What Good is This?
With little more than the code you see in this short tutorial you can create a fully fledged client/server application for all kinds of purposes. For example, instant messenging, p2p and file sharing, and streaming applications all become extremely easy with the IPDaemon and IPPort components at your fingertips.
More Information
For information about the author, please contact lancer@nsoftware.com. For more information about the FTP component or IP*Works!, contact /n software.
Copyright © 2002, Lance Robinson - All Rights Reserved. For publishing permissions, contact lancer@nsoftware.com.
|