ASP Code
  Home arrow ASP Code arrow Building Basic Client/Server Applications ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP CODE

Building Basic Client/Server Applications with VS.Net by Lance Robinson
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2002-03-17

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP Code Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    IBM DB2 Deep Compression ROI Tool

    The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times.
    FREE! Go There Now!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Hello World: WebSphere Service Registry and Repository

    Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 1: Create a continuous build and integration environment

    Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Innovate don't duplicate! Asset reuse strategies for success

    Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository.
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! The dirty dozen: preventing common application-level hack attacks

    As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications.
    FREE! Go There Now!


    NEW! Using Rational Business Developer to enhance your developer productivity

    Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP CODE ARTICLES

    - ASP Forms
    - ASP: The Beginning
    - Getting Remote Files With ASP Continued
    - Inbox and Outbox Manipulation in ASP
    - Relational DropDownList Using VB.NET
    - Ad Tracking URL Hits
    - Use ViewState to display one record per page...
    - Send Email using ASP.NET formatted in HTML
    - ASP File Explorer
    - ASP/XML Interview questions by Srivatsan Sri...
    - Various methods of setting Date values to a ...
    - Conditional DataGrid Item and using checkbox...
    - Fill .NET Listbox with SQL DataReader
    - Filling Dropdown box using Code-Behinds in C#
    - FLAMES code sample written in .NET What is F...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway