Embed
Email

NET

Document Sample
NET
Shared by: HC111110232828
Categories
Tags
Stats
views:
0
posted:
11/10/2011
language:
Turkish
pages:
36
Network Programming





Microsoft .NET



Özgür Zeytinci

Main Topics





• .NET Overview

• .NET Code Development

• .NET Networking Namespaces

• .NET Socket Programming Example

.NET Overview





Aim : Individual devices and Web sites are

simply connected through the Internet to

one in which devices, services, and

computers work together to provide richer

solutions for users.

.NET Overview

• .NET spans clients, servers and services and

consists of:

– XML Web services and applications

– a set of servers, including Windows 2000, SQL

Server and BizTalk Server

– client software, such as Windows XP and

Windows CE

– tools, such as Visual Studio.NET

.NET Overview

.NET Framework, the programming model

of the .NET platform:



XML Web Web

Services Forms Windows

ASP.NET

Forms

Data and XML Classes

Base Framework Classes

Common Language Runtime

.NET Overview

• The .NET infrastructure refers to all the

technologies that make up the new

environment :

– Common Language Runtime (CLR), the

virtual machine in which .NET applications

function.

– Base Class Library (BCL), include support

for everything from file I/O and database I/O

to XML and SOAP.

.NET Application Development

• You write source code in C#

• You compile it using the C# compiler into an

EXE (MSIL - Microsoft intermediate language),

and metadata is created

• During execution, since MSIL code cannot

be executed directly, the CLR compiles the

MSIL by using a just-in-time (JIT) compiler

into native CPU instructions

.NET IL Disassembler

Parses the application's

metadata and displays

information about the

application in a treelike

hierarchy.

.NET vs J2EE

.NET Networking Namespaces





• System.Threading

• System.Net

• System.Net.Sockets

• System.Runtime.Remoting

System.Threading

• Provides classes that enable multi-threaded

programming

– Synchronize mutually-exclusive threads

– Manage groups of threads

– Enable timing

System.Threading

• Primary Classes

– Monitor, Mutex : Provides the synchronization

of threading objects using locks and wait/signals _

– ReaderWriterLock : Defines the lock that

implements single-writer and multiple-reader

semantics _

– Thread : Represents threads that execute within

the runtime to create and control threads _

System.Threading

• Primary Classes

– ThreadPool : Posts work items to the thread

pool and queues work items for execution on the

first available thread from the thread pool

– Timeout : Contains timeout value for methods

System.Net

• Provides a simple programming interface to

– Many of the protocols found on the network

today

– An implementation of network services that

enables you to develop applications that use

Internet resources

System.Net

• Primary Classes

– Dns : Provides simple domain name resolution

functionality

– SocketAddress : Identifies a socket address

– IPAddress : Provides an Internet Protocol (IP)

address

– DnsPermission : Controls rights to access

Domain Name System (DNS) servers on the

network

System.Net

• Primary Classes (cnt.)

– NetworkCredential : Credentials for password-

based authentication schemes

– HttpWebRequest, HttpWebResponse : HTTP

implementation of request and response objects

– WebClient : Provides common methods for

sending data to and receiving data from a resource

identified by a URI

System.Net.Sockets

• Provides

– A managed implementation of the Windows

Sockets interface for developers that need to

tightly control access to the network

– Encapsulations for TCP and UDP clients and

listeners

System.Net.Sockets

• Primary Classes

– MulticastOption : Contains IP address values

for IP multicast packets _

– Socket : Implements the Berkeley sockets

interface _

– TCPClient, TCPListener : Provides client

connections and listeners for TCP network _

– UDPClient : Provides UDP network services _

.NET Socket Programming Example





UDP Multicast

Chat Client Group









Send Msg UDP

UDP

Recieve Msg Chat Client

Chat Client

public class Chat {

private static UdpClient m_Client;

private static int ListenerPort = 8080;

private static int SenderPort = 8080;

private static int LocalPort, RemotePort;

private static string m_szHostName;

private static IPAddress m_GroupAddress;

private static IPHostEntry m_LocalHost;

private static IPEndPoint m_RemoteEP;

private static bool m_Done = false;

using System;

public static void Main() {...}

using System.Net;

public static void Initialize() {...}

public static void Listener() {...} using System.Net.Sockets;

public static void Terminate() {...}

using System.Threading;

}

using System.Text;

public static void Main( String [] args ) { (BACK TO CLASS)

LocalPort = SenderPort;

RemotePort = ListenerPort;



m_szHostName = Dns.GetHostName();

m_LocalHost = Dns.GetHostByName(m_szHostName);



Console.WriteLine("Local Port: {0}, Remote: {1}", LocalPort, RemotePort);

Console.WriteLine("Initializing...");



Initialize();



Console.WriteLine("Starting Listener thread...");

Thread t = new Thread(new ThreadStart(Listener));

t.Start();



Byte [] buffer = null;

Encoding ASCII = Encoding.ASCII;

bool m_ShuttingDown = false;

....

}

while(!m_ShuttingDown) { (BACK TO CLASS)

String s = Console.ReadLine();

if( s.Length == 0 ) continue;



if(String.Compare(s,0,"@",0,1) == 0) {

m_Done = true;

// send a terminator to ourselves, receiving thread can shut down

s = m_szHostName + ":@";

m_ShuttingDown = true;

} else {

s = m_szHostName + ":" + s;

}



buffer = new Byte[s.Length + 1];

// send data to remote peer

int len = ASCII.GetBytes( s.ToCharArray(), 0, s.Length, buffer, 0);

int ecode = m_Client.Send(buffer, len, m_RemoteEP);

if(ecode 0 ) {

// we received a termination indication now we have to decide if it is

// from our main thread shutting down, or from someone else

Char [] separators = {':'};

String [] vars = strData.Split(separators);



if( vars[0] == m_szHostName ) {

// this is from ourselves, therefore we end now

Console.WriteLine("shutting down Listener thread...");

m_Done = true;

}

else { (BACK TO MAIN)

// this is from someone else

Console.WriteLine("{0} has left the conversation", vars[0]);

}

}else {

// this is normal data received from others as well as ourselves

// check to see if it is from ourselves before we print

if(strData.IndexOf(":") > 0) {

Char [] separators = {':'};

String [] vars = strData.Split(separators);

if( vars[0] != m_szHostName ) {

Console.WriteLine(strData);

}

}

}

} // while



Console.WriteLine("Listener thread finished...");

return;

}

public static void Terminate() { (BACK TO MAIN)



m_Client.DropMulticastGroup(m_GroupAddress);

}

Network Programming





Microsoft .NET



Özgür Zeytinci

(BACK)

Monitor

[C#] public sealed class Monitor



Monitor exposes the ability to take and release the sync block lock on

an object on demand via Enter, TryEnter and Exit.



Mutex

[C#] public sealed class Mutex : WaitHandle



Mutex is a synchronization primitive that allows exclusive access to the

shared resource to only one thread.



Wait can be used to request ownership of the mutex.

The thread must call ReleaseMutex the same number of times to release ownership

of the mutex lock.

(BACK)

ReaderWriterLock

[C#] public sealed class ReaderWriterLock



Use in large numbers, such as per object synchronization.

Timeout. This is a valuable feature to detect deadlocks.

Nested locks by readers and writers.

Spin counts for avoiding context switches on multiprocessor machines.

(BACK)

Thread

[C#] public sealed class Thread



AllocateDataSlot Allocates an unnamed data slot on all the threads.

AllocateNamedDataSlot Allocates a named data slot on all of the threads.

FreeNamedDataSlot Frees a previously allocated named data slot.

GetData Retrieves the value from the specified slot on the

current thread, for that thread's current domain.

GetDomain Returns the current domain in which the current

thread is running. The get and set accessors work on the hard thread, not the logical

thread. Therefore, they are package-protected and should not be available for

general consumption.

GetNamedDataSlot Looks up a named data slot.

ResetAbort Resets an Abort.

SetData Sets the data in the specified slot on the currently

running thread, for that thread's current domain.

Sleep Suspends the current thread for a specified time.

(BACK)

MulticastOption

[C#] public class MulticastOption



Sets IP address values when joining or leaving an IP multicast group.



Example:

[C#]



Socket sock = new Socket(AddressFamily.InterNetwork,

SocketType.Dgram, ProtocolType.Udp );



sock.SetSocketOption( SocketOptionLevel.IP,

SocketOptionName.AddMembership,

new MulticastOption( groupIP ));

(BACK)

Socket

[C#] public class Socket : IDisposable



The Socket class creates a managed version of an Internet transport service.

Primary methods: Bind, Connect, Send, SendTo, Recieve, RecieveFrom,

Shutdown, Close.



Example:

[C#]

IPAddress hostadd = Dns.Resolve(server).AddressList[0];

IPEndPoint EPhost = new IPEndPoint(hostadd, 80);



Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,

ProtocolType.Tcp );



s.Connect(EPhost);

s.Send(ByteGet, ByteGet.Length, 0);

(BACK)

TCPClient

[C#] public class TcpClient : Idisposable



The TcpClient class builds upon the Socket class to provide TCP services at

a higher level of abstraction.



Example:

[C#]



// Connect to a TCP Server

TcpClient myClient = new TcpClient("time.contoso.com",13);



// Get the response stream

Stream myStream = myClient.GetStream();

(BACK)

TCPListener

[C#] public class TcpListener



The TcpListener class builds upon the Socket class to provide TCP services

at a higher level of abstraction.



Example:

[C#]



TcpListener myListener = new TcpListener(13);



myListener.Start();



// Program blocks on Accept() until a client connects.

Socket mySocket = myListener.AcceptSocket();

(BACK)

UDPClient

[C#] public class UdpClient : IDisposable



The UdpClient class provides access to UDP services at a higher level of

abstraction than the Socket class does.



UdpClient connects to remote hosts and receives connections from remote

clients.


Related docs
Other docs by HC111110232828
Application 20Support 20Matrix
Views: 0  |  Downloads: 0
film_library
Views: 0  |  Downloads: 0
A Consistent Life 8 31 08
Views: 0  |  Downloads: 0
Adliterature1
Views: 0  |  Downloads: 0
PLATOM
Views: 0  |  Downloads: 0
list
Views: 0  |  Downloads: 0
catalogue2010_pricelist
Views: 0  |  Downloads: 0
cvMartinSchuele
Views: 0  |  Downloads: 0
WILIONaugust2009
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!