Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / Visual Basic
Article

A Synoptic Analysis Of Multi Socket Programming-The Client

Rate me:
Please Sign up or sign in to vote.
1.80/5 (4 votes)
1 Oct 20075 min read 25.7K   13   2
This Article Contains In Snoptical Analysis Of Multi Socket Programming (Part I)- The Client

Introduction

This article contains synoptical analysis of multi socket programming for client side. Doing coding for client side in socket programming & doing coding it in muliti socket programming; is different.


Background

Multi socket programming is few steps ahead in socket programming as one client is connected to one server who is listing at particular port in traditional socket programming whereas one client get connected to two servers at different ports on same machine or on different machines in multi socket programming.

This analysis is done on Microsoft Visual Studio 2003 and .Net Framework 1.1. This analysis is purely meant for intermediate and/or expert professionals of Microsoft Visual Studio 2003 and .Net Framework 1.1. If you have firewall on your system make sure it won't disallow connection.

In this report I have added order analysis and flow analysis of execution for these applications; this will definitely enhance your gained knowledge. It is better to understand the order and flow of execution for a sound professional in software engineering. If you think that you are expert in software development you can directly switch your study to these (order analysis and flow analysis).

Some professionals may find the scope of explanation from technical point of view but as a sound understanding of these applications (i.e. server and client) it is sufficient. If I write whole things, this will put me on long journey to complete and make puzzle and confuse you too. Do remember one thing that there should be limit of explanation depending on requirement and that limit is achieved here for our requirement and purpose. Even if you want to explore more you can proceed; I Welcome.

I have included at the end complete references (i.e. their definition, overview, public constructors, public methods, protected methods, properties etc.) of TcpListener Class, TcpClient Class. Make sure that you should read them as I have used TcpListener Class and TcpClient Class for multi socket programming.

Using the code

Multiple Sockets Programming

What is multi socket programming? Technically it is way of socket programming in which two sockets connecting to either two different servers or same server. What exactly multi socket programming is? For that understand what traditional socket programming is, simultaneously with multi socket programming.

Socket or sockets are logical connections that are created to connect the computers to each others. A port number and a host IP Address/Host name is required to create a socket. Thread or threads are that pieces of a process or program that are assigned some resources, such as file, I/O, and so forth and are able to run individually. Multithreading means many threads of a process can be run on a single processor assigning them an equal time slice and priority, each feeling that it is the only process running.

We implement socket programming in case whenever there is need to make a communication between two machines (here machine stands for computer system), in which one machine act as server and while other as client.

The role of server is to keep listening to the incoming connection's request(s) at particular port. Client sends a request to server to establish a communication through ipaddress of server (or name of server) and port no at which server is listening. Now what happen in multi socket programming is that a client sends request of connection to two or more servers those are listening at their respective port in consideration.

It is up to the server(s) to accept connection request (send by client) or to reject it. If server(s) accepts the connection a virtual pipeline (technically a socket) is created for the communication of messages between server and client to and fro. During the time when a client has active connection to server(s) it can send and receive data from server(s), and then close the connection. These connections are expensive in terms of resources utilized. Server (s) can also restricted to limited number of clients to connect. Again I am repeating that it is server/client architecture because one the computer will act as a server (which is responding to client messages) whiles all other computer behaves as clients (that only send request to server to establish a communication).

A beautiful complexity exhibits by multi socket programming is that a server(s) sends data to any connected client or to all connected clients (that is broadcasting) is to be read by client(s) but how the client(s) came to know which server has send this data. Other difficulty is when either side sends data to other side is supposed to read it but how the other side came to know that any data has arrived. I will explain these issues in detail in later part of this document so stick to it.

Coding Analysis- The Client

Here I am exploring simple client for better understanding. This client connects to two different servers and start communicating with them. The total numbers of functions which are used in this are listed as under:

i) Form2_Load()
ii) DoReadOne()
iii) DoReadSec()
iv) DisplayText()
v) btnSendFirst_Click()
vi) SendForFirst()
vii) btnSendSecond_Click()
viii) SendForSecond()
ix) btnAllServer_Click()
x) MarkAsDisconnected()

Same important thing I write in my every document is before proceeding that all these functions are listed on structural basis. If anyone is trying to find syntax errors (e.g. no of arguments in Form2_Load event) please forgive me. Thanks to God.

Now one more thing following are the imports files for this project's class
Imports System.Net
Imports System.Net.Sockets

Both these files are important as .net provide enrich set of functions through these classes.

VB.NET
'declaring form level variables 
Private marDataOne(1024) As Byte 
Private mobjClient, mobjClientSec As TcpClient 
Private Delegate Sub callDisplayText(ByVal t As String) 
<p>'marDataOne' is an array of bytes which is used to store data which is coming from server in 'DoReadOne()' function. </p>

<p>'mobjClient' & 'mobjClientsec' are objects of TcpClient class & are used to connect to particular servers at concern port see in 'Form2_Load()'.</p>

<p>Elaborating Function 'Form2_Load()' </p>

<p>The full code is as under </p>

<pre lang="vb.net">'code on form load event 
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load 
Try 
'create a new client & display message 
 mobjClient = New TcpClient("192.168.1.8", 1007) 
DisplayText("Connected To Server At Port: 1007" & "~" & "DoReadOne") 
'code to read data using stream 
mobjClient.GetStream.BeginRead(marDataOne, 0, 1024, AddressOf DoReadOne, Nothing) 
'create a new client & display message 
mobjClientSec = New TcpClient("192.168.1.8", 2007) 
DisplayText("Connected To Server At Port: 2007" & "~" & "DoReadSec") 
'code to read data using stream 
mobjClientSec.GetStream.BeginRead(marDataOne, 0, 1024, AddressOf DoReadSec, Nothing) 
Catch ex As Exception 
'declaring error string variable & assinging error message 
Dim strErrMsg As String 
strErrMsg = ex.Message.ToString 
'checking for particular error message 
If strErrMsg = "No connection could be made because the target machine actively refused it" Then 
strErrMsg = "Ask Administrator To Start The Server As " & vbCr & vbCr & "No Connection Could Be Made Because The Target Machine Actively Refused It" 
MessageBox.Show(strErrMsg, "Connection Error") 
Else 
strErrMsg = "Error: While Connecting To Desired Machine For Communication" 
MessageBox.Show(strErrMsg, "Connection Error") 
End If 
'exiting from application properly 
Application.Exit() 
Me.Close() 
End Try 
End Sub 
<p style="TEXT-ALIGN: justify">In this function objects of TcpClient 'mobjClient' & 'mobjClientSec' is sending a request to server for communication by passing Ipaddress and port of machine. DisplayText() function is used to display the data on listbox. </p>

<p>Now I am checking asynchronously for data to receive from either server. This is done with the help of BeginRead() function which is available in getstream of TcpClient object. This BeginRead function is called twice first by 'mobjClient' and second by 'mobjClientSec'. All the expected generic errors are first identify and handled carefully. The code in catch block is self explanatory.</p>

<p>Elaborating Function 'DoReadOne()' </p>

<p style="TEXT-ALIGN: justify">DoReadOne() function accepts an object of IAsyncResult interface. This object is responsible for receiving no of bytes from network stream. If number of bytes received are less than 1 it means that particular socket is now closed (obviously it means that particular server is closed) so call 'MarkAsDisconneced()' function to exit the application successfully. If number of bytes are greater than one it means some data has arrived now to get that data use = 'System.Text.ASCIIEncoding' property. Finally invoking DisplayText() function by delegate to display data on list box. The catch block is again self explanatory. Do remember that reading and displaying of data in DoReadone is done by (only by) mobjClient. </p>

<pre>'function to listen port asynchronsly to read data from server 
Private Sub DoReadOne(ByVal ar As IAsyncResult) 
Try 
'declaring some local variables & assinging values 
Dim strMsg As String 
Dim intCount As Integer 
Dim tempObj As Object 
'getting no of bytes available to read 
intCount = mobjClient.GetStream.EndRead(ar) 
'checking if connected or disconnected 
If intCount < 1 Then 
'call markAsDisconected() function 
MarkAsDisconnected() 
Exit Sub 
End If 
'GETTING STRING IN ONE LINE 
strMsg = System.Text.ASCIIEncoding.ASCII.GetString(marDataOne, 0, intCount) 
strMsg = strMsg & "~" & "DoReadOne" 
'calling displayText() thru delegate 
Dim obj As New callDisplayText(AddressOf DisplayText) 
obj.BeginInvoke(strMsg, Nothing, Nothing) 
'read data from port 
mobjClient.GetStream.BeginRead(marDataOne, 0, 1024, AddressOf DoReadOne, Nothing) 
Catch e As Exception 
MarkAsDisconnected() 
End Try 
End Sub 
<p style="TEXT-ALIGN: justify">Look at this code "System.Text.ASCIIEncoding.ASCII.GetString(marDataOne, 0, intCount) </p>

<p style="TEXT-ALIGN: justify">GetString function accepts three arguments first is the byte array, second is index, third is number of bytes to read. </p>

<p style="TEXT-ALIGN: justify">BeginRead() function accepts five parameters </p>

<p style="TEXT-ALIGN: justify">First: byte array </p>

<p style="TEXT-ALIGN: justify">Second: offset </p>

<p style="TEXT-ALIGN: justify">Third: size i.e. number of bytes to be read </p>

<p style="TEXT-ALIGN: justify">Fourth: a callback delegate </p>

<p style="TEXT-ALIGN: justify">Fifth: object </p>

<p style="TEXT-ALIGN: justify">Here this last i.e. fifth parameter in BeginRead() function is set to nothing but this can be used to retrieve additional information of socket. After executing BeginRead() function I again invoke BeginRead() function just to make sure that whenever there is data available to socket it can be retrieved instantly. </p>

<p>Elaborating Function 'DoReadSec()' </p>

<p>Whole thing is same in this function as they are in DoReadOne() except it is meant for second TcpClient class's object mobjClientSec() that is to communicate with second server. </p>

<p>Elaborating Function 'DisplayText ()' </p>

<p>This function is used to show data on two different list box depending upon from which server it is coming. That is if it is coming from first server this will show data on first list box else in second. This categorization is done with the help of incoming data as a parameter. As this data is concatenated with identifier "DoReadOne" and "DoReadSec" in DoReadOne() and in DoReadSec() function respectively. </p>

<pre>'function to display message in text box 
Private Sub DisplayText(ByVal t As String) 
Dim strMsg As String 
Dim arrMsg(2) As String 
arrMsg = t.Split("~") 
strMsg = arrMsg(0) 
If arrMsg(1) = "DoReadOne" Then 
lstBoxOne.Items.Add(strMsg) 
Else 
lstBoxSec.Items.Add(strMsg) 
End If 
End Sub 
<p>Elaborating Function 'btnSendFirst_Click()' </p>

<p style="TEXT-ALIGN: justify">This block of code is invoked whenever user click ToFirst button from GUI. This code take data from text box and call sendForFirst() function to send data to First server. After sending data to server text box is cleared. The complete code is as under </p>

<pre>'code to send message to first server 
Private Sub btnSendFirst_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles btnSendFirst.Click 
Try 
'validation to enter some text to send 
If txtSend.Text = "" Then 
MessageBox.Show("Please Enter Some Text In Message Box Before Sending") 
Exit Sub 
End If 
'calling send function to send data to server 
SendForFirst(txtSend.Text) 
txtSend.Text = "" 
Catch ex As Exception 
MessageBox.Show(ex.ToString) 
End Try 
End Sub 
<p>Elaborating Function 'SendForFirst ()' </p>

<p style="TEXT-ALIGN: justify">Send function is responsible to send data to network stream to first server. This is achieved with the help of object of IO.StreamWriter class. This function gets invoked whenever ToFirst button is clicked. Use of flush() is to clears all buffers for current writer. The complete code goes like this </p>

<pre>'function to send data to first server 
Private Sub SendForFirst(ByVal t As String) 
Try 
Dim w As New IO.StreamWriter(mobjClient.GetStream) 
w.Write(t) 
w.Flush() 
Catch ex As Exception 
MessageBox.Show(ex.ToString) 
End Try 
End Sub 
<p>Elaborating Function 'btnSendSecond_Click()' </p>

<p>Same conceptual theory as btnSendFirst_Click() except it is meant for sending data to second server. </p>

<p>Elaborating Function 'SendForSecond ()' </p>

<p>Same conceptual theory as SendForFirst except it is meant for sending data to second server. </p>

<p>Elaborating Function 'btnAllServer_Click()' </p>

<p>This function is invoked wherever client click on ToAllServer button. This is used to send data of text box to both the servers. The code is self explanatory as it invoke sendForFirst() & sendForSecond() function to send data to both servers. </p>

<pre>'code to send message to all servers 
Private Sub btnAllServer_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles btnAllServer.Click 
Try 
'validation to enter some text to send 
If txtSend.Text = "" Then 
MessageBox.Show("Please Enter Some Text In Message Box Before Sending") 
Exit Sub 
End If 
'calling send function to send data to all servers 
SendForFirst(txtSend.Text) 
SendForSecond(txtSend.Text) 
txtSend.Text = "" 
Catch ex As Exception 
MessageBox.Show(ex.ToString) 
End Try 
End Sub 
<p>Elaborating Function 'MarkAsDisconnected()' </p>

<p style="TEXT-ALIGN: justify">This function is simply invoked in any case when there is a need to exit the application uninterruptedly. The complete code goes like this </p>

<pre>'function to mark connection as disconnected 
Private Sub MarkAsDisconnected() 
Try 
'declare string variable for displaying error message 
Dim strErrMsg As String 
'concatenating error string msg 
strErrMsg = "Unable To Communicate With Server, Server Is Not Responding" 
MessageBox.Show(strErrMsg, "Connection Error") 
'exiting from application properly 
Me.Close() 
Application.Exit() 
Catch ex As Exception 
MessageBox.Show(ex.ToString) 
End Try 
End Sub 
<p>Order Execution Analysis– Client<br />As sound professional of software engineering one should know the order of execution of application. One should know how function is invoked how they call each other and how whole process came to end for a particular process in application. That is why I am doing with Client application. </p>

<p>Following are the categories that I will explore in terms of their order of execution for client</p>

<p>i) when client starts<br />ii) when message send to first server<br />iii) when message send to second server<br />iv) when message send to all server<br />v) when message receive from first server<br />vi) when message receive from second server<br />vii) when first server close<br />viii) when second server close<br />ix) when client close</p>

<pre>i) When client starts
<p>form_load() calls DoReadSec() and DoReadOne() asynchronously. which in turn DisplayText function is called by both.</p>

<pre>ii) when message send to first server
<p>This called sendForFirst() function.</p>

<pre>iii) when message send to second server
<p>This called sendForSecond() function.</p>

<pre>iv) when message send to all server
<p>This called sendForFirst() & sendForSecond() function.</p>

<pre>v) when message receive from first server
<p>This calls first for DoReadOne() and then DisplayText() function.</p>

<pre>vi) when message receive from second server
<p>This calls first for DoReadSec() and then DisplayText() function.</p>

<pre>vii) when first server close
<p>This call DoReadOne() which in turn call DoReadOne() and finally MarkAsDisconnected() functions.</p>

<pre>vii) when second server close
<p>This call DoReadSec() which in turn call DoReadSec() and finally MarkAsDisconnected() functions.</p>

<pre>vii) when client close
<p>Nothing happens.</p>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Unknown
Er. Nishant Singh Chauhan is presently working in H.T.N. Co. Ltd. This organization is located in between of magnificent, mesmerizing & mischievous hills of Phuket, Thailand.

Er. Nishant Singh Chauhan belongs to Gurgaon-India. But he has moved himself to this location in the month of September 2007. He is found of playing programm (playing; because of sporadical physical work). His first love was (yet it is retained to continue) computer P-II with 64 MB Ram and 10 GB hard disk. He likes writting & has been publishing his documents, articles & knowledge in electronic form at Google from few time in past. His vast knowledege of socket programming including multi socket proramming seperates him from other computer geek.

He holds knowledge in VB.Net, C#.Net, Asp.Net, Java, Javascript, HTML, C, C++. Even his professional tincture bends himself towards .NET yet he has been providing coaching in others languages since 2003.

He holds bachelor degree in computer science & engineering with Hons. & was pursuing his master in magnagement in Information Technology Managenet & International Business Managenent from India but due to moving out from India to Thailand he had to hang his master. He is member of prestigious Association of Computer Machinery, New York, USA. You can mail your problems (and suggestions too) to this author at following mail address:
nishant.c@hoteltravel.com

Comments and Discussions

 
GeneralMy vote of 1 Pin
Pablo Castillo26-Mar-10 11:26
Pablo Castillo26-Mar-10 11:26 
GeneralHorizontal scroll. Pls format Pin
Paras Shah4-Oct-07 8:33
Paras Shah4-Oct-07 8:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.