Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Network Distributed Systems TCP IP Programming Demo

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

Network, Distributed Systems and TCP/IP

Programming Professional

With Microsoft .Net Framework 2


C# and VB.NET 2003/2005

2


)(2006/5/1328
005


/ .
:.2006
)( .
. (2006/5/1328) :

/: // // //

) ( ISBN 9957-09-255-3

:

.

Warning:
All Rights reserved, No Part of this book may be reproduced, or transmitted in any
form or by any means, electronic or mechanical, including photocopying, recording or
by any information storage retrieval system, without the prior permission in writing of
the author

3
:


TCP/IP Models Client/Server
Socket ProgrammingTransport Layer Protocols
Network Layer Programming Multicasting
Voice Chat Systems Voice
Over IP Programming TAPI Telephony Remote Desktop
Packets
Row Programming Packet Sniffing
Application Layer
.

4
:


.

:

Voice Over IP
Video Conference Systems Remote eLearning
Systems
.

5
:
Part1 Networks & TCPIP Programming Overview:
Chapter 1: TCP/IP Layers & Message Encapsulation Overview
Chapter 2: IPv4 & IPv6 Architecture Overview
Chapter 3: IP Multicasting Overview

Part2 Streaming:
Chapter 4: Streaming (Classes & Members)
Chapter 5: Applied Streaming in Dot Net

Part3 Transport & Network Layer Programming:


Chapter 6: Transport TCP & UDP (Classes & Members)
Chapter 7: Synchronous Socket Programming
Chapter 8: Asynchronous Socket Programming
Chapter 9: Advanced Multicasting Systems (Architecture & Conference
Systems)
Chapter 10 VOIP - Voice Over IP Programming
Chapter 11 Row Socket & Packet Sniffing Programming

Part4 Application Layer Programming:


Chapter 12 DNS Programming
Chapter 13 HTTP Programming
Chapter 14 Web Services & XML Programming
Chapter 15 Remotting & Distributed Systems Programming & Design
Chapter 16 SMTP & POP3 Programming
Chapter 17 FTP Programming

Part5 Network Security Programming:

Chapter 18 Cryptography
Chapter 19 Socket Permissions

Part6 Multithreading
Chapter 20 Multithreading (Using & Managing)

6
:
Part 1 Networks & TCPIP Programming Overview:
Chapter 1: TCP/IP Layers & Message Encapsulation Overview

- TCP/IP Layers Encapsulation Overview


- TCP / UDP Connection Establishment
- TCP & UDP Header Encapsulation
- Using TCP Connection Oriented in Dot Net to Send Unicast Messages
- Introduction to Binary Streaming in Dot Net
- Using UDP Connectionless in Dot Net to Send Uni & Broadcast Messages

Chapter 2: IPv4 & IPv6 Architecture Overview ..

- IPv4 Architecture
- Classful IP Address
- Unicast IP
- Broadcast IP
- Multicast IP
- CIDR Nation Overview
- IPv6 Architecture Overview

Chapter 3: IP Multicasting Overview .

- IP Multicasting Overview
- Using IP Multicasting in Dot Net to Create a Multicast Groups

Part2 Streaming:

Chapter 4: Streaming (Classes & Members) ..


- Stream Classes
- Stream Members
- Stream Manipulation

Chapter 5: Applied Streaming in Dot Net ..

- Create a Simple Remote Control Application Using Stream Reader & Writer Classes
- Create a Simple Remote Desktop Application
- Create a Simple Application to Store & Read Images (Binary Data) in Microsoft
Access & Microsoft SQL Server Database Management System By Using Streams
Library & ADO.NET

7
Part3 Transport & Network Layer Programming:

Chapter 6: Transport TCP & UDP (Classes & Members) ..


- TCP Classes Members
- UDP Classes Members

Chapter 7: Synchronous Socket Programming ...

- Introduction to Socket Programming


- Synchronous Socket Programming
- Synchronous Socket Classes & Members

Chapter 8: Asynchronous Socket Programming .

- Asynchronous Socket Class and its members


- Applied Asynchronous Socket in Dot Net

Chapter 9: Advanced Multicasting Systems ...

- Architecture of Multicast Socket


- Using Multicast Socket with .NET
- Multicast Conferencing Systems:
- Full/Half Duplex Multicast Video Conferencing System.
- Full/Half Duplex Multicast Desktop Conferencing System.
- Full/Half Duplex Multicast Text Conferencing System

Chapter 10 VOIP - Voice Over IP Programming ..

- The Concept & Requirements of Voice Communication Systems


- How to Create a Voice Chat Throw Dot Net Using Unmanaged APIs Functions
- Testing UDP Multicasting, TCP and Thinking in SCTP to Transfer Voice Throw
Networks
- How to Create a Voice Conference System Using Microsoft Direct Play 9
- Create an Advanced Video/Voice Conference System By Using API & TAPI
Telephony & Managed Socket Library in Dot Net.

Chapter 11 Row Socket & Packet Sniffing Programming

- Introduction to Row Socket & Row Protocols


- Row Socket Programming
o ICMP Internet Control Message Protocol Programming (Ping &
Tracing)
o Using ARP Protocol to Get The MAC of a Remote Machine in
Dot Net
- Introduction to Packet Sniffing Applications

8
Part 4 Application Layer Programming:

Chapter 12 DNS Programming ....

- Synchronous DNS Members


- Asynchronous DNS Members

Chapter 13 HTTP Programming .....

- The Concept of HTTP Protocol


- Using HTTP in Dot Net
- Advanced HTTP Programming
- Using HttpWebRequest
- Using HttpWebResponse

Chapter 14 Web Services & XML Programming ...

- Introduction to Web services & XML


- Create A Simple Web Service Application
- Create A Simple GIS System Using (ADO.NET With Web Services)

Chapter 15 Remotting & Distributed Systems Programming & Design

- The Distributed Systems Concept & Design


Design a Distributed eCommerce System by Using ASP.NET and Web
Services.

- Serialization Programming
The Serialization (Classes & Members)
Using BinaryFormatter & SoapFormatter to Serialize Objects & Images Throw
Network

- Remotting Programming
Remotting (Classes & Members)
Using Remotting Applications in Dot Net
Create an Advanced Distributed eLearning System (Remote Class Room)
Create an Advanced Remote Desktop Application With Remote
(Mouse/Keyboard) Control Features

9
Chapter 16 SMTP & POP3 Programming ..

- SMTP Protocol
- SMTP Concept
- Using SMTP in Dot Net
- Advanced SMTP Programming
- POP3 Protocol Programming

Chapter 17 FTP Programming ...

- Introduction to FTP File Transfer Protocol


- Create a Simple Application to Transfer Files By Using COM Components
- Create a Simple Application to Transfer Files By Using Web Classes
Components
- Create a Simple Application to Transfer Files By Using Socket Programming &
Streaming Libraries

Part5 Network Security Programming:


Chapter 18 Cryptography ....

- Cryptography in Dot Net:


Symmetric Encryption
DES (Data Encryption Standard)
Asymmetric Encryption
RSA (Rivest Shamir Adleman)
Digital Signature Algorithms
Hashing Algorithms
Using Security Encryption Standards With Network Applications
Create an Advanced RSA Client Server Chat System.

Chapter 19 Socket Permissions ....

- Permission Namespace Overview


- Security Action
- Socket Access property

Part6 Multithreading
Chapter 20 Multithreading Using & Managing ...

- Introduction to Threading in Dot Net


- Threading Classes & Members
- Multithreading & Network Applications

10
Appendixes (A): Classes, Members & Functions For Network Programming ...

- System.Net Namespace
- System.Net.Socket Namespace
- Socket Option Members
- System.Threading Namespace
- TAPI Telephony Functions
- Remotting TCP / HTTP Channels Members

Appendixes (B): ASCII Code Table, References, Useful Sites.

11
Part1
Networks & TCP/IP Programming Overview

Chapter 1 TCP/IP Layers & Message Encapsulation


Overview & Introduction to Network Programming

Chapter2 IPv4 & IPv6 Architecture Overview

Chapter3 IP Multicasting Overview

12
Chapter 1
TCP/IP Layers & Message Encapsulation Overview &
Introduction to Network Programming

- TCP/IP Layers Encapsulation Overview

- TCP / UDP Connection Establishment

- TCP & UDP Header Encapsulation

- Using TCP Connection Oriented in Dot Net to Send Unicast


Messages

- Introduction to Binary Streaming in Dot Net

- Using UDP Connectionless in Dot Net to Send Uni &


Broadcast Messages

13
TCP/IP Layers Encapsulation Overview : 1.1


OSI
Headers . TCP/IP
:

Application Layer
Data Chat
ASCII Code Binary Code Bits
Socket Transport Layer Network Layer Internet
Layer : Socket

14
Port Transport Layer Destination IP Network Layer
Server Client Socket Server
Port Socket Option Port
Authentication
.

OSI upper Layers :

Application User Interface

Presentation
Classes Encoding Class System.Text
Namespace :ASCIIEncoding Class

C#
;)(String str=Console.ReadLine
;)(ASCIIEncoding asen= new ASCIIEncoding
;)byte[] ba=asen.GetBytes(str

VB.NET
Dim str As String = Console.ReadLine
Dim asen As ASCIIEncoding = New ASCIIEncoding
)Dim ba As Byte() = asen.GetBytes(str

Session ) (
TCP Three Way Hand Shake UDP

TCP/IP Application Layer


OSI Session Layer :

-1 Server IP Port
UDP .TCP
-2 Authentication Server
-3
-4 Server Listening Port
: TCP Protocol
C#:
;)TcpClient tcpclnt = new TcpClient("192.168.0.2",8001

15
VB.NET:
)Dim tcpclnt As TcpClient = New TcpClient("192.168.0.2", 8001

Client
Transport TCP -
Connection Protocol UDP - Connectionless Protocol
Port UDP
Broadcast Port
) Multicasting (Group Broadcast IP Multicast
IP Port ) Socket Broadcast IP Multicast IP
(

TCP Class
NetworkStream
Stream Data Socket :

C#:
;)( NetworkStream mynetsream = tcpclnt.GetStream
;)StreamWriter myswrite = new StreamWriter (mynetsream
;)"myswrite.WriteLine("Your Message
;)( myswrite.Close
;)( mynetsream.Close
;)( tcpclnt.Close

VB.NET:
Dim mynetsream As NetworkStream = tcpclnt.GetStream
)Dim myswrite As StreamWriter = New StreamWriter(mynetsream
)"myswrite.WriteLine("Your Message
)( myswrite.Close
)( mynetsream.Close
)( tcpclnt.Close

Network Layer
Physical Tunnel Network Layer
IP,IPv6,ARB-Address Resolution Protocol
...
Server
Bits Data link Layer Network Layer Transport Layer
Application Layer Binary ASCII ASCII Text
Stream Classes .. : Server

C#:

16
using System.IO;
using System.Net.Sockets;
using System.Threading;
TcpListener mylistener;
NetworkStream myntl;
public void Listener()
{
mylistener = new TcpListener(8001);
mylistener.Start();
Socket mysocket;
StreamReader str;
while (true)
{
mysocket = mylistener.AcceptSocket();
myntl = new NetworkStream(mysocket);
str = new StreamReader(myntl);
textbox1.Text = str.ReadToEnd();
}
}
}

VB.NET:
Imports System.IO
Imports System.Net.Sockets
Imports System.Threading

Private mylistener As TcpListener


Private myntl As NetworkStream
Public Sub Listener()
mylistener = New TcpListener(8001)
mylistener.Start()
Dim mysocket As Socket
Dim str As StreamReader
Do While True
mysocket = mylistener.AcceptSocket()
myntl = New NetworkStream(s)
str = New StreamReader(myntl)
textbox1.Text = str.ReadToEnd()
Loop
End Sub

17
Infinity Loop Loop
Thread Form Loop
Infinity Loop Multithreading
Session
..Multitasking
Infinity Loop :

Thread Loop Global Area


Thread Session Pipe Loop
Thread : System.Threading Namespace

C#:
;using System.Threading

VB.NET:
imports System.Threading

Instance Thread Delegate :

C#:
;Thread myth
;))myth= new Thread (new System.Threading .ThreadStart(Listener
;)( myth.Start

18
VB.NET:
Imports System.Threading
Dim myth As Thread
myth = New Thread(New System.Threading.ThreadStart(Listener))
myth.Start

: Closing Form myth.Aport()

C#:
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
mylistener.Stop();
myth.Abort();
}

VB.NET:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)
Try
mylistener.Stop()
myth.Aport() ()
Catch ex As Exception
Msgbox(ex.Message)
End Try
End Sub

Thread Thread
... Multithreading

Server MAC Address


ARP-Address Resolution Protocol
Broadcast Request MAC Table MAC Address
IP Address ARB Message
MAC Address Unicast ARP Replay Message
MAC Cash MAC Table
:

19
Encapsulation Internet Encapsulation Ethernet
Encapsulation Data Link Layer :

20
Packet :

Ethernet Internet Serial


Connection Ethernet MAC Address
ARP Internet PPP Point to Point Protocol
:

Header Data Control Data


Header Transport Network Layer 20 60 Options
.Header

21
Ethereal Packet Sniffing
:

http://www.ethereal.com/download.html

ARB DNS: HTTP

Ethereal
Network Interface Card Google Internet
:Explorer

MAC Address Gateway 10.0.0.138


Broadcast Message Router ARP Request
MAC Address Client
Google DNS Request Query IP Address
Google DNS Table Google
IP Address Google DNS Table
Google 8 20
Google .HTTP Protocol

22
TCP / UDP Connection Establishment : 1.2



TCP Three-way hand-shake
:

-1 Sequence Number Server


) ( increment By One
.

-2 Sequence Number Acknowledgment


.

-3
Acknowledgment .

23
UDP Acknowledgments UDP
TCP .

24
TCP & UDP Header Encapsulation : 1.3
Transport Layer TCP UDP
Stream Unicast TCP
Broadcast Multicast UDP
TCP ... UDP

TCP Encapsulation : 1.3.1


Data Flow Buffer
Delivered on Sequence
Header :

: Source & Destination Port 1.3.1.1


Port Source Port Destination Port

Port Port Address Port
0 1023
HTTP 80 Port
FTP 21 Port Port 49.151
:

25
: Sequence & Acknowledgment Number 1.3.1.2
32 Bits Packet
Acknowledgment Packet
:

: Header Length & Validation Controls 1.3.1.3

Header TCP 32 Bits Windows Size 16


Bits 16Bits Header Length + Validation Controls :

Header Length Header TCP 4


Header ) HLEN (Header Length 4 Validation Controls 6 Controls
1 Bit 0 1
:

26
: Window Size 1.3.1.4
Packet SYN
Packet ACK Packet Packet
.

: Check Sum 1.3.1.5


16 Bits TCP Header
) TCP Header 16 Bits( Check Sum
Checksum HLEN 4 Checksum
Checksum IP Protocol TCP
:UDP

: Urgent Pointer 1.3.1.6


Data TCP Buffer
Data Buffer Pointer Data

27
Buffer Urgent Pointer Data
Buffer :

: Dot Net Binary Streaming Overview : 1.3.2


Stream Classes Text Binary Data Socket
Text Stream Reader Stream Writer
) Object Image Voice
(.. Binary Data Text Text ASCII Code
Binary Object ) Serialization Object (
Binary Reader Binary Writer
Stream Reader Stream Writer Text FileStream
MemoryStream Buffer
Stream Bits Bits Stream
Stream :

28
Image
: Client/Server

: Namespaces

C#:
using System.Net.Socket;
using System.IO;

VB.NET:
Imports System.Net.Socket
Imports System.IO

MemoryStream Instance
Binary
:Server Socket NetworkStream

C#:
try
{
openFileDialog1.ShowDialog ();
string mypic_path =openFileDialog1.FileName ;
pictureBox1.Image = Image.FromFile(mypic_path);
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,pictureBox1.Image.RawFormat);
byte[] arrImage = ms.GetBuffer();
ms.Close();
TcpClient myclient = new TcpClient (txt_host.Text,5020);//Connecting with The
server
NetworkStream myns = myclient.GetStream ();
BinaryWriter mysw = new BinaryWriter (myns);
mysw.Write(arrImage);//send the stream to The above address

29
mysw.Close ();
myns.Close ();
myclient.Close ();
}
catch (Exception ex){MessageBox.Show(ex.Message );}

VB.NET:
openFileDialog1.ShowDialog
Dim mypic_path As String = openFileDialog1.FileName
pictureBox1.Image = Image.FromFile(mypic_path)
Dim ms As MemoryStream = New MemoryStream
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat)
Dim arrImage As Byte() = ms.GetBuffer
ms.Close
Dim myclient As TcpClient = New TcpClient(txt_host.Text, 5020)

Try
Dim myns As NetworkStream = myclient.GetStream
Dim mysw As BinaryWriter = New BinaryWriter(myns)
mysw.Write(arrImage)
mysw.Close
myns.Close
myclient.Close
Catch ex As Exception
Msgbox(ex.Message)
End Try

Socket Stream Port Server


) ( Object Binary Reader
Controls
Picture Box Image.FromStream Method
Stream Stream
: Picturebox

30
:
C#:
using System.Net.Socket ;
using System.IO;

TcpListener mytcpl; // Declare TCP Listener


Socket mysocket; // Declare an object from Socket Class
NetworkStream myns; //
StreamReader mysr;
void Image_Receiver()
{
mytcpl = new TcpListener (5000);// Open The Port
mytcpl.Start ();// Start Listening
mysocket = mytcpl.AcceptSocket ();
myns = new NetworkStream (mysocket);
pictureBox1.Image = Image.FromStream(myns); // Show The Image that Resaved as
Binary Stream
mytcpl.Stop();// Close TCP Session
if (mysocket.Connected ==true)//if Connected Start Again
{
while (true)
{
Image_Receiver();// Back to First Method
}
}
}

31
VB.NET:
Private mytcpl As TcpListener
Private mysocket As Socket
Private pictureBox1 As System.Windows.Forms.PictureBox
Private mainMenu1 As System.Windows.Forms.MainMenu
Private menuItem1 As System.Windows.Forms.MenuItem
Private saveFileDialog1 As System.Windows.Forms.SaveFileDialog
Private myns As NetworkStream

Sub Image_Receiver()
mytcpl = New TcpListener(5000)
mytcpl.Start()
mysocket = mytcpl.AcceptSocket
myns = New NetworkStream(mysocket)
pictureBox1.Image = Image.FromStream(myns)
mytcpl.Stop()
If mysocket.Connected = True Then
While True
Image_Receiver()
End While
End If
End Sub

Thread Thread Image_Receiver Method


: Thread Class Thread Delegate Instance

C#:
Thread myth;
myth= new Thread (new System.Threading .ThreadStart(Image_Receiver));
myth.Start ();

VB.NET:
Imports System.Threading
Dim myth As Thread
myth = New Thread(New System.Threading.ThreadStart(Image_Receiver))
myth.Start

32
: Form Closing Event Socket Thread

C#:
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
try
{
mytcpl.Stop ();
thread.Aport();
}
catch (Exception ex) {MessageBox .Show (ex.Message );}
}

VB.NET:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)
Try
mytcpl.Stop()
thread.Aport()
Catch ex As Exception
Msgbox(ex.Message)
End Try
End Sub

: Save Button

C#:
try
{
saveFileDialog1.Filter = "JPEG Image (*.jpg)|*.jpg" ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string mypic_path = saveFileDialog1.FileName;
pictureBox1.Image.Save(mypic_path);
}
}
catch (Exception)
{
}

33
VB.NET:
Try
"saveFileDialog1.Filter = "JPEG Image (*.jpg)|*.jpg
If saveFileDialog1.ShowDialog = DialogResult.OK Then
Dim mypic_path As String = saveFileDialog1.FileName
)pictureBox1.Image.Save(mypic_path
End If
Catch generatedExceptionVariable0 As Exception
End Try

UDP-User Datagram Protocol- Encapsulation : 1.3.3


UDP
Multicast Broadcast TCP Unicast
Data Flow Buffer
Delivered on Sequence
:

Check Sum Length Header Length TCP


Buffer Management Delivered On Sequence Header
UDP Fragmentation Packets
Acknowledgment
: UDP

34
UDP System.Net Namespace
System.Net.Socket TCP Port
UDP TCP Broadcast IPAddress.Any
IPAddress.Broadcast IPEndPoint
Port 0 Bind Method Socket
.

5020 Port Port


:

)1
C#:
;)IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5020

VB.NET
)Dim ipep As IPEndPoint = New IPEndPoint(IPAddress.Any, 5020

Socket :
)2
C#:
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
)ProtocolType.Udp

VB.NET
Dim newsock As Socket = New Socket(AddressFamily.InterNetwork,
)SocketType.Dgram, ProtocolType.Udp

IPEndpoint Object ... Send


Bind Method IP Address
Port : Socket
)3
C#:
;)newsock.Bind(ipep

VB.NET:
)newsock.Bind(ipep

Server Port
IPEndPoint Object :
)4

35
C#:
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);

VB.NET:
Dim sender As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
Dim Remote As EndPoint = CType((sender), EndPoint)
recv = newsock.ReceiveFrom(data, Remote)

:
5)
C#:
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

VB.NET:
Console.WriteLine("Message received from {0}:", Remote.ToString)
Console.WriteLine(Encoding.ASCII.GetString(Data, 0, recv))

Server
:
C#:
string welcome = "Welcome Customer ...";
data = Encoding.ASCII.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);

VB.NET:
Dim welcome As String = "Welcome Customer ..."
Data = Encoding.ASCII.GetBytes(welcome)
newsock.SendTo(Data, Data.Length, SocketFlags.None, Remote)

: Infinity Loop
... Server
.. infinity loop True

C#:
while(true)
{

36
data = new byte[1024];
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
server.Close();

VB.NET:
While True
Data = New Byte(1024)
recv = newsock.ReceiveFrom(Data, Remote)
Console.WriteLine(Encoding.ASCII.GetString(Data, 0, recv))
newsock.SendTo(Data, recv, SocketFlags.None, Remote)
End While
server.Close()

Infinity Loop Socket


Infinity Loop Break
Socket Loop
Server Server
...
C#:
using System;
using System.Net;
using System.Net.Socket;
using System.Text;

class SimpleUdpSrvr
{
public static void Main()
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 5020);
Socket newsock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");

37
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string welcome = " Welcome Customer ...";
data = Encoding.ASCII.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true)
{
data = new byte[1024];
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
}
}

VB.NET:
Imports System
Imports System.Net
Imports System.Net.Socket
Imports System.Text

Class SimpleUdpSrvr

Public Shared Sub Main()


Dim recv As Integer
Dim data(1024) As Byte
Dim ipep As IPEndPoint = New IPEndPoint(IPAddress.Any, 5020)
Dim newsock As Socket = New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)

newsock.Bind(ipep)
Console.WriteLine("Waiting for a client...")
Dim sender As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
Dim Remote As EndPoint = CType((sender), EndPoint)
recv = newsock.ReceiveFrom(data, Remote)
Console.WriteLine("Message received from {0}:", Remote.ToString)
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv))
Dim welcome As String = " Welcome Customer ..."
data = Encoding.ASCII.GetBytes(welcome)
newsock.SendTo(data, data.Length, SocketFlags.None, Remote)

38
While True
data = New Byte(1024)
recv = newsock.ReceiveFrom(data, Remote)
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv))
newsock.SendTo(data, recv, SocketFlags.None, Remote)
End While
End Sub
End Class

Server Client Client


... Port IPEndPoint

C#:
using System;
using System.Net;
using System.Net.Socket;
using System.Text;
class SimpleUdpClient
{
public static void Main()
{
byte[] data = new byte[1024]; string input, stringData;
IPEndPoint ipep = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 5020);
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,
ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

while(true)
{
input = Console.ReadLine();
Exit
if (input == "exit")
break;

39
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
data = new byte[1024];

40
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("Stopping client");
server.Close();
}
}

VB.NET:
Imports System
Imports System.Net
Imports System.Net.Socket
Imports System.Text
Class SimpleUdpClient
Public Shared Sub Main()
Dim data(1024) As Byte
Dim input As String
Dim stringData As String
Dim ipep As IPEndPoint = New IPEndPoint(IPAddress.Parse("127.0.0.1"), 5020)
Dim server As Socket = New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim welcome As String = "Hello, are you there?"
data = Encoding.ASCII.GetBytes(welcome)
server.SendTo(data, data.Length, SocketFlags.None, ipep)
Dim sender As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
Dim Remote As EndPoint = CType(sender, EndPoint)
data = New Byte(1024)
Dim recv As Integer = server.ReceiveFrom(data, Remote)
Console.WriteLine("Message received from {0}:", Remote.ToString)
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv))
While True
input = Console.ReadLine
If input = "exit" Then
' break
End If
server.SendTo(Encoding.ASCII.GetBytes(input), Remote)
data = New Byte(1024)
recv = server.ReceiveFrom(data, Remote)
stringData = Encoding.ASCII.GetString(data, 0, recv)
Console.WriteLine(stringData)

41
End While
Console.WriteLine("Stopping client")
server.Close()
End Sub
End Class

Socket Options UDP Socket Broadcast Address


( Appendixes (A) SocketOption Class )
: Broadcast Message

C#: - Client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class Broadcast
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 5000);
byte[] data = Encoding.ASCII.GetBytes(Hello All);
sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
sock.SendTo(data, iep);
sock.Close();
}
}

42
VB.NET:- Client
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Broadcast
Public Shared Sub Main()
Dim sock As Socket = New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As IPEndPoint = New IPEndPoint(IPAddress.Broadcast, 5000)
Dim data As Byte() = Encoding.ASCII.GetBytes(Hello All)
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast,
1)
sock.SendTo(data, iep)
sock.Close()
End Sub
End Class

C#: - Server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class RecvBroadcst
{
public static void Main()
{
Socket sock = new
Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 5000);
sock.Bind(iep);
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive...");
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",stringData,
ep.ToString());
}
}

43
VB.NET:- Server
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class RecvBroadcst
Public Shared Sub Main()
Dim sock As Socket = New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As IPEndPoint = New IPEndPoint(IPAddress.Any, 5000)
sock.Bind(iep)
Dim ep As EndPoint = CType(iep, EndPoint)
Console.WriteLine("Ready to receive...")
Dim data As Byte() = New Byte(1023)
Dim recv As Integer = sock.ReceiveFrom(data, ep)
Dim stringData As String = Encoding.ASCII.GetString(data, 0, recv)
Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString())
End Sub
End Class

... UDP TCP TCP/IP


IPv4 Network Layer Encapsulation
... IPv6

44
Chapter 2
IPv4 & IPv6 Architecture Overview

- IPv4 Architecture

- Classful IP Address
i. Unicast IP
ii. Broadcast IP
iii. Multicast IP

- CIDR Nation Overview

- IPv6 Architecture Overview

45
The Internet Protocol:
Network Layer Packet IP
Transport Layer TCP
Unicast UDP
Unicast Multicast Broadcast
IPv4 Unicast Multicast Broadcast:

IPv4 Architecture : 2.1


: IPv4

Header IPv4 20 60 Bytes Options


Options Header 20 Bytes
Data TCP :

)Data = total Length (20 Bytes for IPv4 Header+ 20 Bytes For TCP Header

20 Bytes IPv4 Header :

4 Bits Version IPv4 0100 . Binary


4 Bits Header Length Header 4
8 Bits Type of Services
Application Layer Minimum Delay Maximum Services...
32 Bits Fragmentation Fragments

46
8 Bits TTL Time to Live Hops Routers
Packet 16 hops Router
1 .
8 Bits Upper Layer TCP ... UDP
16 Bits Checksum TCP UDP
.

32 Bits
32 Bits

Unicast Broadcast Unicast


Classful : CIDR - Classless InterDomain Routing

Classful IP Address : 2.1.1


Classful
Network ID Host ID :

Class A 1 126 Bit Binary 0


Class B 128 191 Bit Binary 1
Class C 192 223 Bit Binary ... 1 1
Class D 224 Bits 111 Multicast Group
...
:

47
Broadcast Bits Host ID
Broadcast Address 10.0.0.1 Class
Class A Network ID 10 1
126 Host ID 0.0.1 Broadcast Address 255
Host ID 10.255.255.255 : Broadcast Address
Class A ... Classful Nation

Classful Nation Class


A 2 32-8 Host Subnets Host Part
... Classless

CIDR - Classless InterDomain Routing IP Address : 2.1.2

Bits Network Part Host Part


1024 192.168.1.0/24
2 Bits 24 Bits Network ID Subnet :
192.168.1.0/22 32-22 = 10 Bits Host ID 2 10 Addresses
:

Broadcast 192.168.1.0/22 22 Bits


8 Bits :

48
8 8 6
192 168 1 0
11111111 11111111 11111100 00000000

ones 252 Addresses Range


192.168.252.0 192.168.255.254
Broadcast IP ... 192.168.255.255 :

CIDR
IPv6 32 Bits
128 Bits .

IPv6 Architecture : 2.2

IPv6 IPv4 IPv6 IPv4


:

- IPv4 Address
IPs .
- IPv4 Routing Table IPv6
- Security IPv4 Authentication
Network Layer IPv4 IPSec
IP Port Socket Security
. Network Layer
- Broadcast any cast
Router
Bandwidth .

IPv6 2128 Addresses


Routing Table Routing Authentication
Network Layer Type of Services Priority
Header : IPv6

49
IPv6 hexadecimal Decimal IPv4
:

69dc:8864:ffff:ffff:0:1280:8c0a:ffff

4 IPv4 .16 Bits


Hexadecimal 4 Digits :

IPv6 Provider-Based Addresses Geographic-based


Addresses ISPs Clients Standard Provider-
Based Addresses Geographic-based Addresses
:
- Provider-Based Addresses
)a Registry ID
)b Provider ID
)c Subscriber ID
)d Subscriber Subnet
)e Host Number

50
- Geographic-based Addresses
)a Registry ID
)b World Zone
)c Country, City, etc.

IPv4 IPv6 Unicast Broadcast


Classful . CIDR Nation

IP Multicasting Multicast Group


...

51
Chapter 3
IP Multicasting Programming Overview

- IP Multicasting Overview

- Using IP Multicasting in Dot Net to Create a Multicast


Groups

52
: IP Multicasting Overview :3.1

UDP
Server Port
IPAddress.Broadcast
Server
Port Broadcast

IP Multicasting Multicast Group
) (
Conferences Multicast
Managed Client/Server Server
Client
:

Clients Server
IP Address List IP Multicast
IP Multicast Class D
.
peer-to-peer Technique - unmanaged server
client Server

:

53
Multicasting IP Multicast Address :
224.0.0.0 224.0.0.255 LAN
224.0.1.0 224.0.1.255 Internetwork
224.0.2.0 224.0.255.255 AD-HOC Network block
...

IP Multicasting :3.2 Multicasting Group

IP Multicast Socket Namespace


SetSocketOption IGMP Protocol
( leave & join) multicast group
AddMembership DropMembership Multicast
Routing UdpClient Object Port
IP Multicasting
Port IP Multicast
5020 Port Group
224.100.0.1:

C#:
;using System
;using System.Net
;using System.Net.Socket
;using System.Text
class MultiSend
{
)(public static void Main
{
Socket server = new Socket(AddressFamily.InterNetwork,
;)SocketType.Dgram, ProtocolType.Udp

54
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 5020);
byte[] data = Encoding.ASCII.GetBytes("This is a test message");
server.SendTo(data, iep);
server.Close();
}
}

VB.NET:
Imports System
Imports System.Net
Imports System.Net.Socket
Imports System.Text

Class MultiSend

Public Shared Sub Main()


Dim server As Socket = New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As IPEndPoint = New IPEndPoint(IPAddress.Pars("224.100.0.1"), 5020)
Dim data As Byte() = Encoding.ASCII.GetBytes("This is a test message")
server.SendTo(data, iep)
server.Close()
End Sub
End Class

Socket )Socket
... Socket ( IP Multicast Group

UdpClient Object Socket


: Port
C#:
using System;
using System.Net;
using System.Net.Socket;
using System.Text;

class UdpClientMultiRecv
{
public static void Main()
{
UdpClient sock = new UdpClient(5020);
sock.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);

55
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);

byte[] data = sock.Receive(ref iep);


string stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine("received: {0} from: {1}", stringData, iep.ToString());
sock.Close();}}

VB.NET:
Imports System
Imports System.Net
Imports System.Net.Socket
Imports System.Text

Class UdpClientMultiRecv

Public Shared Sub Main()


Dim sock As UdpClient = New UdpClient(5020)
sock.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50)
Dim iep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
Dim data As Byte() = sock.Receive(iep)
Dim stringData As String = Encoding.ASCII.GetString(data, 0, data.Length)
Console.WriteLine("received: {0} from: {1}", stringData, iep.ToString)
sock.Close()
End Sub
End Class


Binary
Byte Array memory Stream
Shutdown Clients
Multicast Group
Advanced Multicast
... Systems

56

More Topics About Voice Over IP Programming Including TAPI :


. Telephony

.Advanced Web Services Programming & Remotting Services :

Packet Sniffing & Row Socket :

Remote Desktop Applications With Keyboard/Mouse Control :


.Features

: Multithreading
.

: Network Security DES RSADSA


SSL Socket Permissions .

: Keyboard Listening
Mouse ...

: .Remotting

: Real Applications
.

: CD

57

-1 - - -
0096265350649

-2 - - - 4611548

-3 - - - 5330211

-4 - - -
5340383 5345711

-5 - - -
5335837

http://www.fadidotnet.org/full_version.htm

+962 796 284475

fadi@fadidotnet.org

58

You might also like