310 likes | 324 Views
Implement multi-routing in a Microsoft .NET environment using hard-coded routes in the initial phase, with plans to expand to any server in the future. Calculate bandwidth from source server to destination server using multiple routes.
E N D
Multi-routing the '.Net' Gigax, Kevin Edward Torres, Francisco Javier CS522 – Computer Communications
Project Goals • The Goal of this project is to enable multi-routing in a Microsoft .NET environment using the C# language. • The projects initial phase is to implement multi-routing using routes that are hard-coded into the servers. • In the future, the project plans to implement multi-routing to any server running the .NET framework.
Description • The project calculates the bandwidth from a source server through n different routes to a destination server. • The project will be expanded to route through multiple servers in the future.
Function Call: getBandwidth Finds the fastest route from S1 to D1 using specific servers R1 S1 R2 D1 Rn
Function Call: getBandwidth2 Finds the fastest route from S1 to D1 using x amount of servers R12 R13 R11 S1 D1 R21 R22 Rn1 Rnn
Implementation • The software design uses a software created packet of x bytes. This packet is transmitted across the network using a socket connection as an ICMP packet. • The software times the amount of time for the packet to be transmitted and for a reply to be received. This timing is the estimated bandwidth of that route. • The routers are hard-coded into the servers as character arrays. Each router in the array is tested for its bandwidth abilities.
Implementation • Function Calls • getBandwidth(sourceServer, destinationServer, routingServer) • For each proxy server available, call getBandwidth function • Once an acceptable bandwidth is found exit call and use this proxy server • findBandwidth(sourceServer, destinationServer) • Execute a packet send and receive to the source and to the destination • Time the packet’s total round trip time
Drawbacks • The primary drawbacks of the project are as follows: • Congesting the network: if we continual send out queries for bandwidth, we start to cause serious network congestion • Estimated Bandwidth: available bandwidth is a large topic and we cannot insure that this is the best way to find the bandwidth of a connection
Performance Analysis • Waiting for multiple queries highly degrades performance: • We need to implement multi-threaded queries to solve this • Getting the absolute best bandwidth can also degrade performance • We can set an acceptable bandwidth value and stop the queries once we find a bandwidth that is >= to this value
Socket connections in .NET How to create both ends of a TCP/IP socket connection between two or more applications?
Why use sockets in .NET? .NET uses sockets in many instances such as Web services and remoting, but here the low level stuff is done for us. However, when interfacing with other non .NET system, sockets are a necessary and simple communication method.
Why use Stream Sockets? • Stream Sockets (TCP) Session (or connection) based service Guarantees that packets are sent without errors, sent (and received) in sequence and without duplication Unlimited data size in packets • Communication between server and client occurs through streams The base class for this communication is NetworkStream . Streams can be used for binary, text, and string data or serialized objects. • Datagram sockets are supported by class System.Net.Sockets.UDPClient
Scenario for Stream Sockets • Sockets are created by both client and server, Server specifies a port number Server may customize aspects of connections (wait queue, etc) Client specifies the internet address and port in creating its socket. In C#, when the client creates its TCPClient instance, a connection is established with the TCPListener
Scenario… (continued) • Server listens for arriving requests ( AcceptSocket or AcceptTcpClient method) to establish a session (connection) with a client. If no connections are pending, the server blocks . If one or more clients are waiting, they are queued and in turn, the server creates a new thread (stay tuned) to service each client. The parent thread re-issues the accept to service another client on the same socket. • The client and server identify input and outputstreams for passing information according to a protocol they both agree to. The input and output streams perform the work of the application. • The client and server must both close the connection to allow resources to be used in another connection.
Socket Server: using System.Net.Sockets; using System; /// <summary> /// Example program showing simple TCP socket connections in C#.NET. /// TCPSocketServer is the socket server. /// </summary> public class TCPSocketServer { public static void Main (string [] args) { TcpListener tcpl = new TcpListener(9090); tcpl.Start(); Console.Write("TCPSocketServer up and waiting for connections on 9090"); Socket sock = tcpl.AcceptSocket(); string msg = "Hello Client"; Byte[] msgBytes = System.Text.Encoding.ASCII.GetBytes(msg); sock.Send(msgBytes, msgBytes.Length, SocketFlags.DontRoute); tcpl.Stop(); sock.Close(); } }
Socket Client: using System; using System.IO; using System.Windows.Forms; using System.Net.Sockets; /// <summary> /// Example program showing simple TCP socket connections in C#.NET. /// TCPSocketClient is the socket client. /// </summary> public class TCPSocketClient { public static void Main (string[] args) { TcpClient tcpc = new TcpClient("localhost", 9090); Stream tcpStream = tcpc.GetStream(); StreamReader reader = new StreamReader(tcpStream, System.Text.Encoding.ASCII); MessageBox.Show(reader.ReadLine()); reader.Close(); tcpc.Close(); } }
Thread Concept Why threads?
Threaded Socket Servers • How does a threaded socket server, for protocols such as http or ftp, allow multiple clients to be simultaneously connected to the server?
What is a Thread? • A thread is a single sequential flow of control within a program • A multi-threaded program Two or more threads seemingly active simultaneously All within the overhead of a single executing C# program A single starting point and ending point for all threads in the program and for the program itself.
Other Threading capabilities: • Thread.Join() method - used to wait for another thread to complete. • Sleep -- suspend for a number of milliseconds Thread.Sleep(2000) ; • Thread States ; Thread includes a property to query a state. • Priorities - .NET supports 1 to 10 lowest to highest priorities • Volatile modifier for attributes (don't cache) multi-CPU one RAM.
Synchronization. • What is a Shared Object ? Multiple threads of single program may access or change the same object. • When should access to shared objects be controlled ? When multiple threads only read the object - no synchronization . If any thread modifies the object - synchronization is necessary. • Why must access be controlled when some thread changes the object? consider: stack.push(item) , for example multiple actions are necessary to achieve consistent state, for example: (1) increment stack pointer; (2) place the new item into the collection using the stack pointer. If one thread is interrupted after step 1, but before step 2 , then another thread may see an inconsistent stack state (visualize a = stack.top() ). • Monitor - a lock used to protect a critical section of code Each thread calls Enter() to get the lockbefore push(item) Each thread calls Exit() to release the lock after push(item) completes
C# .NET Monitors • A Monitor protects access to the Object See System.Threading.Monitor class All Monitor methods are static • Basic Monitor methods to lock and unlock an object void Enter(object) and bool TryEnter(object, TimeSpan) lock the object. A thread may be recursive (call multiple Enter's before first Exit) void Exit(object) indicate the calling thread is ready to unlock the object
Issues with Multi-Threaded programs: • Synchronization - "I can't go past this statement until another thread reaches that statement" • Concurrency , parallelism and asynchronous behavior • Deadlock • Threads with shared data (objects) • Scheduling and blocking operations, such as input and output • Underlying support for implementing threads • Performance
Process Creation in C# • To run outside software using C#, we create a PROCESS object using the System.Diagnostics library • To find the bandwidth, we create an outside process for ftp and we then time a download for a file to find the estimated bandwidth
Process Creation Process p = new Process(); //Create a process obj p.EnableRaisingEvents=false; //Disable events from interrupting //Set the directory to find the batch file for ftp’ing p.StartInfo.WorkingDirectory="c:/Inetpub/ftproot/kegigax"; //Set arguments: -s: reads a file of commands for ftp p.StartInfo.Arguments="-s:ftp_batch.txt" + server; //Designate the program to execute p.StartInfo.FileName="ftp"; start = System.Environment.TickCount; //Start the timing p.Start(); //Start the process p.WaitForExit(); //Wait for process finish //Calculate time for ftp call bandwidth = System.Environment.TickCount - start; p.Close(); //Close the process
Conclusions • Our project progress has reached being able to calculate a path from a source to destination server using socket connections when the routing servers are hard-coded • Our continued efforts involve creating actual packets and headers to relay through multiple paths to increase throughput and researching algorithms that locate ‘good’ paths through the internet to a destination server
What is next? • Find a proper way to calculate the bandwidth. • A specific design of threads, server, clients, frame’s headers. • Design a bidirectional communication, in order to implement a multimedia (voice and sound) application.
References • VisualC Ideas: Using Sockets in C# http://www.mctainsh.com/Csharp/SocketsInCS.aspx • Stream Sockets in C#.NEThttp://pooh.east.asu.edu/Cet556/ClassNotes/Serial/cnSerialThreadSocket-18.html • The Code Project http://www.codeproject.com/csharp/workerthread.asp Programing with threads in C# http://pooh.east.asu.edu/Cet556/ClassNotes/Serial/cnSerialThreadSocket-25.html • O'Reilly Network: Multithreading with C# http://www.ondotnet.com/pub/a/dotnet/2001/08/06/csharp.html • C# Primer – A Practical Approach Stanley B. Lippman, Addison-Wesley, 2002 Pearson Education • C# Essentials Ben Albahari, Peter Drayton & Brad Merril, 2002 O’Reilly