P. 1
[Arabic] Dot Net Networks & TCPIP Programming عربى

[Arabic] Dot Net Networks & TCPIP Programming عربى

4.5

|Views: 715|Likes:
Published by hayssam
[Arabic] Dot Net Networks & TCPIP Programming عربى
[Arabic] Dot Net Networks & TCPIP Programming عربى

More info:

Published by: hayssam on Jun 16, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/09/2014

pdf

text

original

Sections

1

Version 1.0, 2005 - 2006
ª--و·-´'`ا ª=--'ا و ª-·ر·'ا ª=--' .='- ¸ه =-'´-'- ª--'=- ª=-- ¸ه
'+·یز·ﺕ '--و·-´'إ ء'-ﺕ '-آ ، .- .´ﺵ ي'- ª--و·-´'`ا ª=--'ا ·-- ·--ی
ª-·رو ةر·-- '+·-- ·--ی '-آ ل'´ﺵ`ا ...
-ا ¸ه ª--ر:-ا ª=~--ا :----ا ª-'~إو ó-~=- ¿- ب'---ا ó- ة---·--ا ª=~-
ª----·--ا سور--ا ó- ~-ﻥ تو--ا ت'·-- ¿--=- ª=-:--ا ---- ف:ﺱ '-آ
VB.NET & C#.NET ó--را-~V'- 2003 و 2005 ...
ª--'--ا óیو'-·- -ﺡا _-- ل'~-Vا _ﺝ:ی ¿یز:--ا وأ ر'~--ﺱVاو أ ~-=-
Mobile : +962796284475
ُPhone: +96265055999
E-mail: fadi822000@yahoo.com
BOX: 311 Mail Code 11947 Tariq—Amman—Jordan

My online CV: http://spaces.msn.com/members/csharp2005/
Mobile : +962796284475
ُPhone: +96265055999
E-mail: fadi822000@yahoo.com
¤nlìl: لl>oìا اíه L8 لوVا بl.Sìا
¤.:,ﻉìا
— ¤.:و,.SìVا ¤>a.ìا —

By FADI Abdel-
qader, Jordan
Dot Net Networks 8
TCP/!P Programming
ل:آ:-و:-و ت'--~-ا ª=-:- ف:-=ا TCP/IP — ª--و:---Vا ª=~--ا
With Microsoft Visual C# & VB.NET
ردlaìا ﺩ.c يدl8 m.ìÌ: - ندرVا

2
¤oﺩaoìا :

... توiIا تlaI ماi>..l. تlî..Iا ¤>o,.. ¤sIa.oIا ر«oVا »هأ بl.îIا اiه ¸ºl..
مis.oIا _Iإ L.«.oIا _Iإ ئi..oIا ى«.ﺱoIا _o :. _s... ذإ L.ﺱ.و ﺱI. ب«I.l.
> ب«I.l. ×.ooو _.o ، و أi.. بl.îIا _c ¤olc ¤oiso. TCP/!P Nodels تls..L.و
Client/Server _c _±so ح,. _Ic ي«.ﺡ.و loآ ... توiIا تlaII ماi>..l. Socket
Programming لاو Network Layer Protocols لا ماi>..l. ¤ois.o ¤oL.أ ءl..و
Nulticasting لا تl.>o,.و تا,o.åoIا ¤oL.lآ Remote Desktop _c »îﺡ.Iا ¤oL.أو
lه,.cو ia. ، لا تV«آ«.,. »هV _±so ح,. _Ic ي«.ﺡ.و loآ Application Layer
تlî..Iا تl.>o,. .º lç.loاi>..او ، ا,.>أو ×±وو ¤.loﺡIا ق,L _c _±so ح,.
تlî..Iا تl.>o,. .º تl.l.ﺱIاو تl.ﺡV±Iا ...



ءاﺩهVا :

اiه يiها _Iإ بl.îIا VLIا ب _.>o,.oIاو »IlaIا ءlﺡ.أ ×.o> .º ب,aIا ...
































3
_>lo لuæaìا ¤.8رuìا ¤>a.ìاو ¤.:و,.SìVا ¤>a.ìا L8 :

¤.:و,.SìVا ¤>a.ìا :
Chapter 1: An Overview on Networks & TCP/IP Programming

Chapter 2: Managed I/O: Streams, Readers, and Writers

Chapter 3: The Socket & Network Layer Programming

Chapter 4: Advanced Multicasting Systems

Chapter 5: Application Layer Programming

Chapter 6: Network Security Programming

¤.8رuìا ¤>a.ìا :
¤.ºر«Iا ¤>ﺱ.Iا ي«.ﺡ. ف«. ةi.i>Iا تlºl±Vا _o ,..îIا _Ic ، إ ¤.V. _o ن«î.. ذ
.I. loآ ءا,>أ :

ء,>Iا لوVا : ن«î..و _o _.I±º : »ço .>o,.oI لا vB6 لاو C++6 لاو Java ×.o>و
... توiIا _Iإ _.Is..oIا _.>o,.oIا
Part 1: Preparation to Dot Net

Chapter 1: Dot Net Infrastructure & OOP

Chapter 2:ADO.NET

ء,>ìا L:l.ìا : ن«î..و o _ ل«±º ¤..lo. : ¤ois.oIا تlî..Iا ¤oL.أ ءl.. ف,.ﺡا
... توiIا تl..s. ماi>..l.
Part 2: .Net Networks & TCP/IP Programming

Chapter 3: An Overview on Networks & TCP/IP Programming

Chapter 4: Managed I/O: Streams, Readers, and Writers

Chapter 5: The Socket & Network Layer Programming

Chapter 6: Advanced Multicasting Systems

Chapter 7: Application Layer Programming

Chapter 8: Remotting & Web Services

Chapter 9: .Net Security Overview & Network Security Programming

Chapter 10: Performance Improvement & Multithreading

ء,>ìا Cìl.ìا : ¤oL.أ _Ic ¤.Ioc ×.رl.oو تls..L. ... توiIا ماi>..l. تlî..Iا .

Part 3: .Net Networks Applications & Real Projects

_ìإ ¤8læVl: CD _lc يu.ﺡ: ¤l.oVا g:رlaoìاو بl.Sìا L8 ¤>و,boìا ...
4
¤.:و,.SìVا ¤>a.ì تl:u.ﺡoìا س,q8 :


Chapter 1: An Overview on Networks & TCP/IP Programming ……… Page 6

Introduction to Network and TCP/IP Programming

A. Introduction to TCP/IP Layers
B. Connection Oriented Via TCP Overview
C. Connection Less Via UDP Overview
D. Streaming & Threading Overview
E. IP Multicasting Overview

Chapter 2: Managed I/O: Streams, Readers, and Writers ………...…… Page 31

Managed I/O: Streams, Readers, and Writers

F. Stream Classes
G. Stream Members
H. Stream Manipulation
I. Simple Remote Control Application Using StreamReader &
StreamWriter Classes

Chapter 3: The Socket & Network Layer Programming …………….… Page 44

The Socket & Network Layer Programming

A. Socket Programming
B. Socket Class Members
C. TCP & UDP Classes Members
D. Asynchronous Sockets


Chapter 4: Advanced Multicasting Systems ………………………..…… Page 70

Advanced Multicasting Systems

A. Architecture of Multicast Sockets
B. Using Multicast Sockets with .NET
C. Multicast Conferencing Systems :
1.Full/Half Duplex Multicast Video Conferencing System.
2.Full/Half Duplex Multicast Desktop Conferencing
System.
3.Full/Half Duplex Multicast Text Conferencing System









5

Chapter 5: Application Layer Programming …………………………… Page 98

Application Layer Programming

A. DNS Programming
B. SMTP Programming
C. POP3 Programming
D. HTTP Programming
E. Web Services & XML Programming
F. FTP Programming



Chapter 6: Network Security Programming ………………………….… Page 147

Network Security Programming

Dot Net Security Namespaces Overview
1. Cryptography
2. Permission
































6
Dot Net Networks 8 TCP/!P Programming
Chapter 1
An Overview on Network &
TCP]IP Programming

!ntroduction to Network and TCP/!P Programming

A. !ntroduction to TCP/!P Layers
Programming
B. Connection Oriented via TCP Overview
C. Connection Less via UDP Overview
D. Streaming 8 Threading Overview
E. !P Nulticasting Overview















7
¤ا oa: o.>,ìا _o>,ìا

1.1 : لuآu:و,: و تlS.aìا ¤>o,: L8 ¤oﺩao IP ] TCP
لl±.ا ¤I..و ,.c lç±a. ×o ¤I±.o ة,ç>Vا _o ¤c«o>o .ه ¤î..Iا نأ فو,aoIا _o
¤oL.oI فو,aoIا ».ﺱs.Iا l..iI جرi... l.ه _oو ¤..ao OS! ,o. ..Iاو لl±.Vا ¤.IoaI
.و ¤..ao ¤s.Lو lç.o ¤s.L _îI تls.L ¤a.ﺱ. تls.L ¤a.رأ _Iإ lهرl±.>ا » ) ¤ﺱo>
..îIا ¸a. .º ( ل«آ«.,. .º TCP/!P تls.LIا eiه ¤sº,oIا ةر«±Iا _...و :





_.. لl±.Vا ¤.Ioc ءا,>V Client و Server .I. lo م,I. :
_.,oIا زlç>Iا .º Client :
لا .º ¤I.,oIا ¤Il.,Iا ..I«. ¤.Ioc أi.. Application Layer _ola.Iا l.ه lç.s.Lوو
_Iإ ¤.±. ¤a.ﺹ _o lçI.«ﺡ.و lçﺱs. ¤Il.,Iا ×o Data ¤î..Iا ,.c lçIl.رإ _îo. ،
¤.درiIا تl.>o,. .sº Chat _Iإ ب«.îoIا ﺹ.Iا _.«ﺡ. ».. ASC!! Code _Iإ ».
_o ¤c«o>o Binary Code ,.c lçIl.رإو lه,.ç>.I ¤º«s±o .º ×±«. Socket يiIاو
لا ¤s.L L.,. Application Layer لا تls.L ¤.s. ×o TCP/!P ة«L>Iا eiه r.±«. l.هو
...



8
لا ماi>..l. ﺹ.آ ¤.«.îoIا ¤Il.,Iا _.«ﺡ. ¤s.,L _...و ASC!!Encoding Class _Iإ
Byte Array :
# C
String str=Console.ReadLine();
ASC!!Encoding asen= new ASC!!Encoding();
byte|| ba=asen.GetBytes(str);

NET . VB
Dim str As String = Console.ReadLine
Dim asen As ASC!!Encoding = New ASC!!Encoding
Dim ba As Byte() = asen.GetBytes(str)
جذ«o. .º OS! . » لا ».ﺱs. upper Layers تls.L ¤.V. _Iإ
Application _oﺱ. lo وأ ﻩﺱs. rol.,.Iا ×o _ola.I User !nterface
Presentation _Iإ تl.l..Iا _.«ﺡ.. ls.l. ت,çL loآ .هو ¤I.,oIا تl.l..Iا _..o.
لا ASC!! .
Session Iا »ç±a.. ..,a.Iا و _.زlç>Iا _.. .Ll>.Iا ¤.Ioa. ءi.Iا lç.ºو ¸a. ) r.º
¤ﺱI>Iا ( …
لا ل«آ«.و,. .º loأ TCP/!P ¤s.L د«>«. ls.îº Application تls.LIا _oa. م«s. ..Iاو
.º _IوVا ثV.Iا OS! ، .º session Layer تا«L> ةia. ¤ﺱI>Iا r.ºو ف,a.Iا »..
.I. loآ .هو :
1 - ءا,>إ Vا . لl± زlç>. ..i.oIا server لا ,.c !P و لا Port :Iذو دiﺡoIا i.iﺡ. ia.
,.c ءا«. لl±.Vا ¤.Ioc UDP ,.c وأ TCP
2 - لا _ocو ﻩﺱs.. ..,a.Iا Authentication لا زlç> .IL. اذإ Server :Iذ
3 - l籺ر وأ ¤ﺱI>Iا r.º _Ic ¤sºا«oIا لl.ر|. :Iذ »..و ¤ﺱI>Iا ¸ºر وأ ل«.º
+ - لا مl.ºو ¤ﺱI>Iا أi. Server _oa. Listening لا _Ic Port ol.,.Il. صl>Iا r

_.,oIا زlç> م«s. .Ll>.Iا ¤.Ioa. ءi.Iاو ¤ﺱI>Iا r.º _Ic ¤sºا«oIا ».. loi.c
Client ¤s.L l.ه .هو ى,>Vا ¤s.LIا _Iإ ¤Il.,Iا _.oﺡ.. Transport eiه .ºو
,.c ءا«. لl±.Vا ¤a..L i.iﺡ. ».. ¤s.LIا TCP - Connection Protocol لا ,.c وأ
UDP - Connectionless Protocol sº loهو _.º,L i.iﺡ. ».. لوVا ل«آ«.و,.Iا .
و _.s.ﺱoIاو _.,oIا Port لا loأ لl±.Vا UDP و _.,oIا ف,LIا i.iﺡ. »..º
_.s.ﺱoIا ) يرl..>ا ( لا _oc _îo. ﻩ.ا يأ Broadcast ¤..ao ¤ç> i.iﺡ. نوi.
9
لا اiه ,.c ..±.. م«s. ﺹ>. يأ نأ يأ ¤Il.,Iا لl.s..V Port Listening ×.L.ﺱ.
l.s..ا ¤Il.,Iا ل ، ¤s.LIا eiه _oc r±«. لl.o l.هو لا ماi>..l. TCP Protocol :

: # C
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect("192.168.0.2",8001);

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

o _sﺡ.Iا ¤.Ioc »..و لا .º ل«ﺹ«Iا _ TCP «ه loآ .º r±«o .Il.Iا _î.Iا :


در لl.رإ ».. لl.رإ ¤.Ioc _آ .º ﻩ.أ ذإ Acknowledgment lç.º e,.>. _.,oIا _Iإ
¤Il.,Iا ل«ﺹ«. ، لا .º _.,.و Header Acknowledgment لا »ºر Packet ». يiIا
حl>.. ﻩIl.s..ا لا _oﺱ.و ACK !D .

.Iا ,.c ¤Il.,Iا لl.رVو .. توiIا .º مi>.ﺱ. ¤î. Class ¤.IoaIا eiç. م«s. ,هl>
_oﺱ.و NetworkStream لl.رإو لl±.Vا ¤I..و ×o _ola.Iا _c ل«.ﺱoIا «هو
_î.. ..aoIا ف,LIا _Iإ ¤Il.,Iا Stream Data ، لا ماi>..l. وأ Socket ﻩﺱs.
) _±sIا ,L.ا .Il.Iا ( :Iذ _Ic لl.oآو :


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

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

_Iإ »Iﺱ. :Iذ ia.و Network Layer _.,oIا نا«.c ×±وو ¤Il.,Iا ¤.«.c ».. ذإ
لا ,.c lçIl.رإ »..I _.دVا ¤s.LIا _Iإ »Iﺱ.و lç.Ic _.s.ﺱoIاو Physical Tunnel
lç.Ic ¤I.oVا _oو !P,!Pv6,ARB-Address Resolution Protocol ...
_.s.ﺱoIا زlç>II ¤.ﺱ.. loأ لا Server º ,oIl. م«s. _îIو تls.LIا ﺱs. _Ic رو
لا ¤î..Iا ت,آ »I.ﺱ. ..ﺡ ﺱîaIl. Bits _Iإ ل«ﺡ.I Data link ». Network ».
Transport ». Application _o ل«ﺡ. lç.oو Binary _Iإ ASC!! _oو ASC!! _Iإ Text ..
لا _oc أi.o r±«. د«îIا eiهو Server :

: # C
TcpListener myList=new TcpListener("12/.0.0.1",8001);
myList.Start();
Socket s=myList.AcceptSocket();
byte|| b=new byte|100|;
int k=s.Receive(b);
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(b|i|));
s.Close();


VB.NET:

Dim myList As TcpListener = New TcpListener("12/.0.0.1", 8001)
myList.Start()
Dim s As Socket = myList.AcceptSocket
Dim b(100) As Byte
Dim k As !nteger = s.Receive(b)
Dim i As !nteger = 0
While i < k
Console.Write(Convert.ToChar(b(i)))
System.Nath.Nin(System.Threading.!nterlocked.!ncrement(i), i - 1)
End While
s.Close()


11
1.2 : Connectionless Sockets Via UDP

لا _c ls.l. l..iﺡ. TCP – Connection Oriented Protocol l...و نأ لا ل«آ«.و,. TCP
لا ءا«.ﺡا م,I. ﻩ.ا ..a. اiهو ﻩ>«o ل«آ«.و,. «ه Header نا«.c _Ic ﻩ. صl>Iا
م,I. loآ _.s.ﺱoIا نا«.c و _.,oIا l±.أ Iا تl.Ioa. مl.sIا _sﺡ. Authentication و
ل«ﺹ«Iا _o _sﺡ.Iا تl.Ioc »ci. لا ماi>..l. Acknowledgment _î.. ».Iﺱ.Iا و
r.ﺡ±Iا .... :I ¤.ﺱ.. »ço ,.c :Iذ _آ نlآ «I اذlo _îI ذإ :>ol.,. _o i.,. نأ م«s.
.. ¤.Ioa. .cاذإ Broadcast و ¤Il.,Iا »I.ﺱ. ف«. _o :oç. Vو :.Il.,I نأ
.رVا .º ¤c,ﺱIا و لl لl.s..Vا ..l.Vا فiçIا .ه اذإ ل«آ«.و,. ك,. :.Ic .>و
لا TCP لا «ﺡ. ﻩ>«.Iاو UDP User Datagram Protocol _oﺱ.و l±.أ لl.
Connectionless Protocol لl. _oﺱ. lo _oc ×.L.ﺱ. ل«آ«.و,.Iا اiه .º
Broadcast لا و Nulticast ) Broad - لl.رVا ..a. _Iإ و _îIا Nulti - Vا ..a. لl.ر _Iإ
لاو ¤c«o>o Uni - Lsº iﺡا«I لl.رVا ..a. ( م,I. i.ﺡو ط,. i>«. نأ _.a. ei>l.
رl..cVا لا ماi>..ا i.c UDP ..Iا ¤.IoaI اi>..l. م Broadcast «هو نأ ..Iا ¤î..Iا
,.l.o _î.. lçao _±.. lçI .. _oc i.,. Direct Connection يأ د«>و نوi. Router
_.s.ﺱoIا _..و :... ذإ نأ لا Router .cاذVا ..Iا تl.Ioc ×.o. Broadcast ..ﺡ
م,I. نأ لا _o± ¤î..Iا ن«î. Range Class ءا«. A وأ B وأ C ، لا نأ فو,aoIا _oو
!P Address _Iإ »ﺱso ...,> _ ¤î..I ﺹ±>o لوVا Network _I ﺹ±>o ..l.Iاو
HOST .Il.Iا _î.Iا .º r±«o «ه loآو :



_oaI V.o Broadcast إ لا _I Address : 10.0.0.0 – 255.0.0.0 :Iذ ».. .I. loآ :
10.255.255.255 _IوVا ¤.l>Iا نأ ..ﺡ ) 10 ( لا .ه Network !D _s.. نأ .>.و
لl. صl>Iا ء,>Iا Network !D لا ×±«.و «ه loآ 255 لl. ¤ﺹl>Iا تl.l>Iا ×.o> .º
HOST !D ...



12
ماi>..V لا UDP ... توiIا .º م,I. Vوأ .,a. . Name Space Net . System لا و
System.Net.Socket لا .º ﻩ.ا LﺡV TCP لا »ºر ..,a. م,I. نlآ Port زlç>II نا«.aIاو
_.s.ﺱoIا loأ لا .º UDP .º «ه loآ ﻩs.,a. ×.L.ﺱ.º TCP _oc ×.L.ﺱ.و loآ
Broadcast اi>..l. م !PAddress.Any سVîIا _o _.lآ قls..ا ia. !PEndPoint
) Ls. ..a.و فiçIا ¤ ( ×.L.ﺱ.و l±.أ لا »ºر i.iﺡ. مic Port اi>..l. م لا Nethod
Bind lçs.,a. ».. ..ﺡ ب 0 ...
.Il.Iا لl.oIا .º ».. لا r.º Port 5020 ..±.Iاو اiه ,.c ¤Il.,Iا مV..ا ». lç.Ic
لا Port »ºر i.iﺡ. نوi. _îIا _Ic lça.ز«.و لا Port لا _Ic lço.Iﺱ. ».. ..ﺡ Port
¤.IoaI ﺹ±>oIا .lآ دو,.Iا . لا «هو Port ,sﺹ :

: # C
!PEndPoint ipep = new !PEndPoint(!PAddress.Any, 5020);

NET . VB
Dim ipep As !PEndPoint = New !PEndPoint(!PAddress.Any, 5020)

.I. loآ :Iذ ».. مi>.ﺱoIا ل«آ«.,.Iا ع«. i.iﺡ.I :
: # C
Socket newsock = new Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)

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

». ,.,o. لا »ºرو فiçIا ¤Ls. Port د«..oIا _Iإ Send ...
لا Bind Nethod Lsº _.s.ﺱoIا ف,LIا .º lça±و ».. لا L.,. ذإ !P Address »ºرو
لا Port لl. Socket :
: # C
newsock.Bind(ipep);

VB.NET:
newsock.Bind(ipep)

لا ×o _±.. _o _آ _Iإ lç.. i.,.و ¤Il.,Iا لl.s..ا ». نVا Server لا _Ic Port
.I. loآ فiçIا ¤Ls. ..,a. Vوأ م,I. :Iذ _oaIو ¤s.lﺱIا :
: # C
!PEndPoint sender = new !PEndPoint(!PAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
13
VB.NET:
Dim sender As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim Remote As EndPoint = CType((sender), EndPoint)

«ه فiçIا ¤Ls. نا«.c نأ LﺡV Any لا »ºرو Port ا لl.رإ ..a. اiهو ,sﺹ ¤Il.,I
لا و ¤Il.,Iا _.,o ﺹ>.Iا »ç.º lo.و _îIا _Iإ ¤oI.ﺱoIا Server

لا _o ¤Il.,Iا مV..ا ».. l.ه Server لا _Iإ Server ¤î..Iا ,.c ى,>أ ة,o :

: # C
recv = newsock.ReceiveFrom(data, ref Remote);

VB.NET:
recv = newsock.ReceiveFrom(data, Remote)

ا _.,o نا«.c ¤cl.LI lçﺱs. ¤Il.,Iا و ¤Il.,I :
: # C
Console.WriteLine("Nessage received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv));

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

لا _Ic :... i.i> زlç> _îI ¤...ﺡ,. ¤Il.ر لl.ر|. l.ه م«s. Server ﻩ.ا lç. e,.>.
ة,ç>Vا _o± ﻩI«>د _Ic ¤sºا«oIا ». :
: # C
string welcome = "Welcome Customer ...";
data = Encoding.ASC!!.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
VB.NET:
Dim welcome As String = "Welcome Customer ..."
Data = Encoding.ASC!!.GetBytes(welcome)
newsock.SendTo(Data, Data.Length, SocketFlags.None, Remote)

l.ه !nfinity Loop _o .ºو يأ .º ¤Il.ر يأ لl.s..ا i.c «ه ﻩ.o فiçIا يأ _.º
لا م«s. زlç> Server ¤î..Iا _Ic «ه _o _آ _Iإ lço.Iﺱ.و lçoV..l. ... تدرأ اذإ
,..a. ×.L.ﺱ. ¤oI.ﺱoIا _.l.,Iا _o _.ao دic i.iﺡ. True لا .º infinity loop _Iإ
ei.,. »ºر يأ ..


14
: # C
while(true)
{
data = new byte|102+|;
recv = newsock.ReceiveFrom(data, ref Remote);

Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
server.Close();

VB.NET:

While True
Data = New Byte(102+) {}
recv = newsock.ReceiveFrom(Data, Remote)
Console.WriteLine(Encoding.ASC!!.GetString(Data, 0, recv))
newsock.SendTo(Data, recv, SocketFlags.None, Remote)
End While
server.Close()

قVcإ ».. l.ه لا Socket _o جو,>Iا ». اذإ ¤Ilﺡ .º !nfinity Loop و ل«ﺹ«Iا ».. _I
اذإ Vإ ¤Ls.Iا eiه _Iإ ×±«. ﻩ.aLlso ». Break _o± لا !nfinity Loop ط,. _ºو
_.ao يأ ¤..ao ¤Il.ر ﺹ. وأ ¤Il.ر لl.s..ا ¤Ilﺡ .º ﻩ.ا جو,>Iا »... _o Loop
»...و قVcإ لا Socket ..a. اiهو ×.L.ﺱ. :.ا لا قVcإ Server ia. _c loآ
لا _Ic يi.s.. .Io يأ _.a.. ¤Io> ×±و :.îo. Server _.ao ﺹ. دورو ¤Ilﺡ .º
اiîهو .

C#:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class SimpleUdpSrvr
{
public static void Nain()
{
int recv;
byte|| data = new byte|102+|;
!PEndPoint ipep = new !PEndPoint(!PAddress.Any, 5020);
Socket newsock = new Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");
15
!PEndPoint sender = new !PEndPoint(!PAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine("Nessage received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv));
string welcome = " Welcome Customer ...";
data = Encoding.ASC!!.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);

while (true)
{
data = new byte|102+|;
recv = newsock.ReceiveFrom(data, ref Remote);

Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
}
}


VB.NET:

!mports System
!mports System.Net
!mports System.Net.Sockets
!mports System.Text

Class SimpleUdpSrvr

Public Shared Sub Nain()
Dim recv As !nteger
Dim data(102+) As Byte
Dim ipep As !PEndPoint = New !PEndPoint(!PAddress.Any, 5020)
Dim newsock As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)

newsock.Bind(ipep)
Console.WriteLine("Waiting for a client...")
Dim sender As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim Remote As EndPoint = CType((sender), EndPoint)
recv = newsock.ReceiveFrom(data, Remote)
Console.WriteLine("Nessage received from {0}:", Remote.ToString)
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv))
Dim welcome As String = " Welcome Customer ..."
data = Encoding.ASC!!.GetBytes(welcome)
newsock.SendTo(data, data.Length, SocketFlags.None, Remote)
While True
data = New Byte(102+) {}
recv = newsock.ReceiveFrom(data, Remote)
16
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv))
newsock.SendTo(data, recv, SocketFlags.None, Remote)
End While
End Sub
End Class

لl. صl>Iا ء,>Iا نVا Client ، لا مl.º _Ic l.ه _oaIا ,±.s. Client ءl..|. ×o ¤ﺱI>
لا Server ﻩs.,a. ia. :Iذو لl. !PEndPoint لا »ºرو Port نأ Vإ _.lﺱIا .º ». loآو
لا _o ¤Il.,Iا لl.s..ا _Ic Lsº ,±.s. اذإ ¤s.L«Iا .º «ه فV.>Vا Server لl.رإو
لا ,.c ﻩI ﻩIl.ر يأ Port .Il.Iا د«îIا ,L.ا ¤.IoaIا eiç. مl.sII ﺹ±>oIا :

C#:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class SimpleUdpClient
{

public static void Nain()

{

byte|| data = new byte|102+|; string input, stringData;

!PEndPoint ipep = new !PEndPoint( !PAddress.Parse("12/.0.0.1"), 5020);

Socket server = new Socket(AddressFamily.!nterNetwork,SocketType.Dgram,
ProtocolType.Udp);

ناisº ¤Ilﺡ .º لl±.Vا لا ×o Server ¤Il.,Iا ,çL. .Il.Iا ¤
string welcome = "Hello, are you there?";

data = Encoding.ASC!!.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
!PEndPoint sender = new !PEndPoint(!PAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;

data = new byte|102+|;
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Nessage received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv));

while(true) _.l.,Iا _o دiﺡo ,.c دic لl.رإ ×.L.ﺱ. .îI
{

17
input = Console.ReadLine();
Exit ..آا ¤ﺱI>Iا ءlç.إ تدرأ اذإ ¤Ilﺡ .º

if (input == "exit")
break;

server.SendTo(Encoding.ASC!!.GetBytes(input), Remote);
data = new byte|102+|;

recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.ASC!!.GetString(data, 0, recv);
Console.WriteLine(stringData);

}

Console.WriteLine("Stopping client");
server.Close();

}

}

VB.NET:
!mports System
!mports System.Net
!mports System.Net.Sockets
!mports System.Text

Class SimpleUdpClient

Public Shared Sub Nain()
Dim data(102+) As Byte
Dim input As String
Dim stringData As String
Dim ipep As !PEndPoint = New
!PEndPoint(!PAddress.Parse("12/.0.0.1"), 5020)
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim welcome As String = "Hello, are you there?"
data = Encoding.ASC!!.GetBytes(welcome)
server.SendTo(data, data.Length, SocketFlags.None, ipep)
Dim sender As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim Remote As EndPoint = CType(sender, EndPoint)
data = New Byte(102+) {}
Dim recv As !nteger = server.ReceiveFrom(data, Remote)
Console.WriteLine("Nessage received from {0}:", Remote.ToString)
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv))
While True
input = Console.ReadLine
!f input = "exit" Then
18
' break
End !f
server.SendTo(Encoding.ASC!!.GetBytes(input), Remote)
data = New Byte(102+) {}
recv = server.ReceiveFrom(data, Remote)
stringData = Encoding.ASC!!.GetString(data, 0, recv)
Console.WriteLine(stringData)
End While
Console.WriteLine("Stopping client")
server.Close()
End Sub
End Class

اiه .º l... ء,>Iا لا ×o _ola.Iا ¤.s.آ UDP Connectionless Protocol ق,sIا l...و
_..و ﻩ... TCP Connection Oriented Protocol ...































19
1.3 : Threading Overview & Streaming

l.º,a. ءا,>أ _Ic ls.l. OS! و TCP/!P .º تls.LIا eiه ×o _ola.Iا ¤.s.آ l...و
تlî..Iا تl.>o,. ، اiه .ºو ء,>Iا _... ف«. ×o _ola.Iا ¤.s.آ لا Stream Library
لl.رV Binary Data لا ماi>..ا _Iإ ¤ºl±Vl. Thread l.>o,. .º ¤î..Iا ت ...

Vوأ : لا Socket : نأ ls.l. l.Iº لا Socket lçIV> _o تl.l..Iا _s. ».. ..Iا ةادVا .ه
..,a. ¤.اi.Iا .º م,I. lçoاi>..Vو ,>أ _Iإ زlç> _o System.Net.Sockets ..ﺡ
اiه ي«.ﺡ. لا Namespaces _o »>± دic _Ic لا Classes lçoاi>..ا ».. ..Iاو
..Iا تl.>o,. .º ¤î _±sIا ,L.ا .Il.Iا .

:.îo. لا Classes Stream _s. _o Text وأ Object ، _o _ola.Iا ¤.s.آ ls.l. l... ..ﺡ
لا Socket _s.I Text ماi>..l. Stream Reader لاو Stream Writer اiه .ºو ء,>Iا
_s.I ﻩao _ola.Iا ¤.s.آ _.... Object ) ةر«ﺹ ن«î. نأ _îo.و تl.l..Iا _o ,>أ ع«. يأ
!mage ت«ﺹ وأ voice _Iإ ل«ﺡ. نأ _îo. ,>l ء.. يأ وأ Binary Data (.. ، «ه loآو
لا _s. .º لlﺡIا Text ل«ﺡ. l.آ Text _Iإ Code ASC!! _Iإ ». Binary .º loأ Object
لا ماi>..l. ﻩao _ola.Iا »..º Stream Classes _o lç.Iإ ل«ﺹ«Iا ».. ..Iاو Name
Spaces System.!O eiه ي«.ﺡ.و Classes لا _Ic Binary Reader و لا Binary
Writer :.îo. ..Iاو يأ ×o _ola.Iا _o Object لاو Stream Reader لاو Stream
Writer لا ×o _ola.Iا _o :.îo. ..Iاو Text لاو File Stream ×o _ola.Iا _.çﺱ.I
تlsIoIا ºl±Vl. ¤ لا _Iإ Nemory Stream ك مi>.ﺱ. ..Iاو Buffer ..Iا LsﺡI تl.l
lçIl.s..ا ia. وأ lçIl.رإ _.º ..l.Iا _±sIا ,L.ا .



يأ _.«ﺡ. _Ic ¤..îoIا eiه كiclﺱ. ..ﺡ Object _Iإ Binary ماi>..l. لا Binary
Writer ماi>..l. ¤î..Iا ,.c ﻩIl.رإ _çﺱ.I Network Stream ى,>أ ة,o ﻩI.«ﺡ. ».
_Iإ Object ماi>..l. Binary Reader ، .s..L. لl.oآو ءl... م«s. ف«. اiه _Ic
_s. ¤.Ioa. م«s. rol.,. !mage ,>أ _Iإ زlç> _o Client/Server ءl..|. »º ءi.IIو
عو,.o .Il.Iا _î.Iا .º loآ i.i> :
20


¤ºl±|. »º ¤.اi.Iا .º لا Namespaces .Il.Iا ¤ :
: # C
using System.Net.Sockets;
using System.!O;

VB.NET:
!mports System.Net.Sockets
!mports System.!O

قls..ا _o Vوأ i. V لl.رVا ¤.Ioc ءا,>VI !nstance سVîIا _o NemoryStream
ة,آاiIا _>اد ةر«±Iا _.,>.I lçoi>.ﺱ. ف«. ..Iاو _î.. .ºåo lsﺡV lçI«ﺡ. .îI
¤º«s±o _Iإ Binary lçIl.رإ ». ماi>..l. NetworkStream لا ,.c Socket _Iإ
لا زlç> Server :
: # C
try
{
ةر«±. صl>Iا ثl.Iا i.iﺡ.
openFileDialog1.ShowDialog ();
string mypic_path =openFileDialog1.FileName ;
pictureBox1.!mage = !mage.FromFile(mypic_path);

NemoryStream ms = new NemoryStream();
pictureBox1.!mage.Save(ms,pictureBox1.!mage.RawFormat);
..l. ع«.Iا _o ¤º«s±o .º lça±وو ةر«±Iا _.,>.
byte|| arr!mage = ms.GetBuffer();
ms.Close();
لا زlç>. لl±.Vا Server لاو نا«.aIا ,.c Port دiﺡoIا
TcpClient myclient = new TcpClient (txt_host.Text,5020);//Connecting with
server


21
VB.NET:
openFileDialog1.ShowDialog
Dim mypic_path As String = openFileDialog1.FileName
pictureBox1.!mage = !mage.FromFile(mypic_path)
Dim ms As NemoryStream = New NemoryStream
pictureBox1.!mage.Save(ms, pictureBox1.!mage.RawFormat)
Dim arr!mage As Byte() = ms.GetBuffer
ms.Close
Dim myclient As TcpClient = New TcpClient(txt_host.Text, 5020)

: # C
لا زlç> _Iإ ¤.,>oIا ةر«±Iا لl.رإ Server
NetworkStream myns = myclient.GetStream ();
BinaryWriter mysw = new BinaryWriter (myns);
mysw.Write(arr!mage);//send the stream to above address

قVcإ لا Socket لا و ¤ﺱI>Iاو Stream
mysw.Close ();
myns.Close ();
myclient.Close ();

}
catch (Exception ex){NessageBox.Show(ex.Nessage );}

VB.NET:
Try
Dim myns As NetworkStream = myclient.GetStream
Dim mysw As BinaryWriter = New BinaryWriter(myns)
mysw.Write(arr!mage)
mysw.Close
myns.Close
myclient.Close
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try

l.:l: : لا Server
لl. صl>Iا ء,>Iا ح,. ء,>Iا اiه .º أi.ا ف«. Server ..±.Iا ¤.Ioa. م«s. يiIاو
لا _Ic Port لا لl.s..او Stream ,.c لا Socket ماi>..l. lç.ءا,º و لا Binary
Reader .î>.وا _Iإ ﻩI.«ﺡ.و ) لl.رVا _.º lç.Ic نlآ ..Iا ﻩ.a.ﺹ ( ى,>أ ة,o ، .º
iه i.,. لl.oIا ا ةر«ﺹ لl.s..ا ةi.i> ﺹ.l±> ... توiIا l..iI ت,ºو ¤IlﺡIا eiه .ºو
لا .º Controls ¤.ﺹl> lç.o± _oو lç.º ةد«>«oIا !mage.FromStream لا ب ¤ﺹl>Iا
PictureBox لV> _o ¤I.,oIا ةر«±Iا ض,c ¤..lîoإ l..Ic _çﺱ. ..Iاو Stream .îI
_o lçI.«ﺡ. ».. Binary Stream _Ic ض,a. ةر«ﺹ _Iإ لا Picturebox لl.oIا ,L.ا
.Il.Iا :






22
: # C
using System.Net.Sockets ;
using System.!O;

// Objects Declaration
TcpListener mytcpl; // Declare TCP Listener
Socket mysocket; // Declare an object from Socket Class
NetworkStream myns; //
StreamReader mysr;
void !mage_Receiver()
{

mytcpl = new TcpListener (5000);// Open The Port
mytcpl.Start ();// Start Listening on That Port
mysocket = mytcpl.AcceptSocket ();// Accept Any Request From Client and
Start The Session

myns = new NetworkStream (mysocket);// Receive The Binary Data From
Port
pictureBox1.!mage = !mage.FromStream(myns); // Show The !mage that
Resaved as Binary Stream
mytcpl.Stop();// Close TCP Session

if (mysocket.Connected ==true)//if Connected Start Again
{

while (true)
{
!mage_Receiver();// Back to First Nethod
}
}
}

VB.NET:
Private mytcpl As TcpListener
Private mysocket As Socket
Private pictureBox1 As System.Windows.Forms.PictureBox
Private mainNenu1 As System.Windows.Forms.NainNenu
Private menu!tem1 As System.Windows.Forms.Nenu!tem
Private saveFileDialog1 As System.Windows.Forms.SaveFileDialog
Private myns As NetworkStream

Sub !mage_Receiver()
mytcpl = New TcpListener(5000)
mytcpl.Start()
mysocket = mytcpl.AcceptSocket
myns = New NetworkStream(mysocket)
pictureBox1.!mage = !mage.FromStream(myns)
mytcpl.Stop()
!f mysocket.Connected = True Then
While True
23
!mage_Receiver()
End While
End !f
End Sub

ءl..|. »º _..L.Iو عو,.o .Il.Iا _î.Iا .º loآ i.i> :



.±أ لا method ﺱIا .º ¤s.l class lç.lci..l. »º ». rol.,.Iا . ×±« لا
!mage_Receiver() لا .º loا Constructer _.a..Iا أi. ثiﺡ. وأ rol.,.Il. صl>Iا
صl>Iا لl. Form ، و ثiﺡ .º ¤.Il.Iا د«..oIا لا Closing صl>Iا لl. Form :
: # C
private void Form1_Closing(object sender,
System.ComponentNodel.CancelEventArgs e)
{
try
{
mytcpl.Stop ();
Application.Exit();
}
catch (Exception ex) {NessageBox .Show (ex.Nessage );}
}

VB.NET:
Private Sub Form1_Closing(Byval sender As Object, Byval e As
System.ComponentNodel.CancelEventArgs)
Try
mytcpl.Stop()
Application.ExitThread()
Application.Exit()
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try
End Sub
قVcإ _o iآl.I :Iذو لا Socket rol.,.Iا ءlç.إ i.c ، ..
24
لا _Iإ .Il.Iا د«îIا ¤ºl±|. »º Save Button ¤I.s.ﺱoIا ةر«±Iا _.,>. _o _îo.. .îI :

: # C
try
{

saveFileDialog1.Filter = "JPEG !mage (*.jpg)]*.jpg" ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string mypic_path = saveFileDialog1.FileName;
pictureBox1.!mage.Save(mypic_path);
}
}
catch (Exception){}

VB.NET:

Try
saveFileDialog1.Filter = "JPEG !mage (*.jpg)]*.jpg"
!f saveFileDialog1.ShowDialog = DialogResult.OK Then
Dim mypic_path As String = saveFileDialog1.FileName
pictureBox1.!mage.Save(mypic_path)
End !f
Catch generatedExceptionvariable0 As Exception
End Try
Threading Overview :
لا يدå. ف«. !nfinity Loop لا نأ ..ﺱIاو rol.,.Iا _.Ia. _Iإ el.a±و يiIاو Loop
لا ¤sL.o _Ic _oa. Global Area _I ¤±±>oIاو Form ia. Vإ ء.. is.. _I ذإ
لا ءlç..ا Loop ﻩ.ا ذإ اi.أ ثiﺡ. _I lo «هو !nfinity Loop ، l.I .oiº _ﺡIا ... توiIا
. ماi>..l. .هو ¤Iî.oIا eiçI لا l.>«I«.î Threading ¤>IlaoIl. roﺱ. ..Iاو
_ocو rIlaoIا _Ic مlçoIا ».ﺱs. لV> _o :Iذو rIlaoIا ﺱs. _Ic ¤.زا«.oIا
Session لl. _oﺱ. lo «هو rol.,. _îI ¤I±s.o Nultitasking .. rol.,.Iا ,.å. V l.هو
,..آ _î.. مlL.Iا درا«o _Ic لا نأ loآ Loop .º _oa.. Thread c _±s.o لا _
Thread لl. صl>Iا Form .Il.Iا _î.Iا ,L.ا :

25


لا ¤ºl±إ _.º ﻩ.ا LﺡV Thread نlآ Loop لا ¤sL.o _Ic _oa. Global Area «ه اiهو
لا ماi>..ا ia.و i.i.Iا ءL.Iا ... Thread _oc ». Session صl> _I Loop ..ﺡ.
×o يزا«.o _î.. _oa. rol.,.Iا ..


لا ماi>..Vو Thread I. ..,a. Vوأ م, لا Namespace System.Threading :

: # C
using System.Threading;


VB.NET:
imports System.Threading


قls..ا ». !nstance _oc i.,. ..Iا د«..oIا ».ا جاردإو ﻩ.o Thread lçI لا .º
Delegate lç. صl>Iا .I. loآ :


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


VB.NET:
!mports System.Threading

Dim myth As Thread
myth = New Thread(New System.Threading.ThreadStart(!mage_Receiver))
myth.Start


¤ºl±|. »º نVأ Application.ExitThread لا ثiﺡ .º Closing Form .I. loآ


26
: # C
private void Form1_Closing(object sender,
System.ComponentNodel.CancelEventArgs e)
{
try
{
mytcpl.Stop ();
Application.ExitThread ();
Application.Exit();
}
catch (Exception ex) {NessageBox .Show (ex.Nessage );}

}


VB.NET:
Private Sub Form1_Closing(Byval sender As Object, Byval e As
System.ComponentNodel.CancelEventArgs)
Try
mytcpl.Stop()
Application.ExitThread()
Application.Exit()
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try
End Sub



لا ة,.o Thread _o ,.آأ _.a.. _o :.îo. ذإ اi> ¤a.ار Thread .º«Iا ﺱs. .ºو
لl. _oﺱ. lo «هو rol.,.Iا ﺱs. .ºو Nultithreading ﻩﺡ,. _Ic ..l. يiIاو
بl.îIا _o ¤.ºر«Iا ¤>ﺱ.Iا .º _.±s.. .



















27
1.4 : IP Multicasting _oﻉì lqoاﺩ>.aاو Multicasting Group

ﺡ. لا ل«آ«.,. _c ls.l. l..i UDP ..lآ دو,. _oaI ﻩoاi>..ا ¤.s.آ l.ﺡ,.و ..ﺡ
ماi>..l. loإ _..s.,L. ..lآ دو,.Iا _oc ×.L.ﺱ. !PAddress.Any ﻩo,I. يiIاو
د«>و Server لا _Ic ..±.Iا ¤.Ioa. م«s. Port يأ ﻩIV> _o _.s.ﺱ. ..ﺡ دiﺡoIا
>..l. وأ ة,ç>Vا _آ _Iإ lç... م«s. ». ¤Il.ر ماi !PAddress.Broadcast _o يiIاو
زlç> د«>«I ةرو,± V ..ﺡ ة,ç>Vا _آ _Iإ .. _oc _îo. ﻩIV> Server نأ ..ﺡ.
_Ic ..±.Iا ﻩ.îo. _îIا لا Port و دiﺡoIا _Iإ ¤Il.ر يأ lçIV> _o _.,.و _.s.ﺱ.
¤.Ioc ﻩ...و ة,ç>Vا _آ لا Broadcast ×o.ﺱ. ×.o>Iا نأ ..ﺡ .cاذVا ..Iا ¤.Ioc
o _ _îIا _Iإ _.,.و _îIا ، lهi.c تlc«o>o _Iإ لl.رVا ».ﺱs. l.درأ اذإ loأ .>.
ماi>..ا لا !P Nulticasting لا _oaI ﻩoاi>..ا فiç. :Iذو Nulticast Group ،
ﻩI .±±> اiçIو تlî..Iا تl.>o,. .º اi> ¤oçoIا ×.±ا«oIا _o ع«±«oIا اiه ,..a.
_±º ¤.s.Iا _c _±s.o ) Iا _±sIا ,L.ا ×.ا, ( لا تl.>o,. .Icا نأ ذإ Conferences
ف,a.و ,..آ _î.. ﻩ.Ic io.a. Nulticast _o ¤c«o>o _Iإ لl.رVا ﻩ.ا _Ic
نlآ ءا«. _.oi>.ﺱoIا Nanaged ماi>..l. Client/Server زlç> :Il.ه ن«î. ..ﺡ
Server لا _o _.l.,Iا لl.s..ا ﻩ.s.Lو ¤î..Iا .º Group Clients _Iإ lçIl.رإ ».
olآ .Il.Iا _î.Iا _Iإ ,L.ا ى,>أ ة,o ¤c«o>oIا _ :



لا _.º _o ¤c«o>oIا _Iإ مlo±.Vا .IL لl.رإ ».. ﻩ.ا LﺡV Clients _ºاو اذإو
لا Server لا _Iإ زlç>Iا نا«.c »±. م«s. .ILIا _Ic !P Address List ﻩ. ¤ﺹl>Iا
لا ﺱs.. ¤c«o>o _آ ك,...و !P Nulticast ×.o> _Iإ لl.رVا »..و أ ءl±c
لا .s.. ك,... ..Iا ¤c«o>oIا !P Nulticast لا _o± ×s. يiIاو Class D _..lo «هو
ls.l. .

لl. _oﺱ.و ..l.Iا ع«.Iا unmanaged - peer-to-peer Technique _آ نأ ..ﺡ
ك _oa. زlç> server و client زlç>I د«>و Vو .º«Iا ﺱs. .º Server ﺹ±>o ي,آ,o
Iاو لl.s..Vا ¤.IoaI ¤c«o>oIا _Iإ مlo±.Vا .IL _Ic ¤sºا«oIا ».. ..ﺡ ×.ز«.
و لl.s..Vا ». مlo±.Vا .º _ﺡIا ﻩI ¤c«o>oIا .º زlç> يأو ..lsI. _î..
.Il.Iا _î.Iا LﺡV ¤c«o>oIا _olآ _Iإ لl.رVا :
28


_I ¤ﺹl> _.وl.c ﺹ.±>. ». Nulticasting لl. _oﺱ. lo «هو !P Nulticast Address
loآ .هو .I. :

_o ىioIا 22+.0.0.0 _Iإ 22+.0.0.255 ¤.IﺡoIا تlî..I LAN
_o ىioIا 22+.0.1.0 _Iإ 22+.0.1.255 _I !nternetwork
_o ىioIا 22+.0.2.0 _Iإ 22+.0.255.255 _I AD-HOC Network block

_I ,..آ »cد ... توiIا .oiº !P Nulticast لا ماi>..l. Socket Namespace ..ﺡ
. ».. د«..oIا لا ماi>..l. lçs.,a SetSocketOption تl.Ioc ةراد|. م«s. ..Iاو
¤c«o>oIا _Iاو _o جو,>Iاو مlo±.Vا multicast group ) join 8 leave ( . loآ مi>.ﺱ
¤.«±aIا ءlaIإو ¤ºl±V AddNembership و DropNembership و . مi>.ﺱ لا
UdpClient Object لا »ºر i.iﺡ.I Port يiIاو .. ». ﻩIV> _o تl.l..Iا لl.s..ا
لا ..,a. _Iإ ¤ºl±Vl. !P Nulticasting ف«. ..Iا تlç>Iا دiﺡ. ﻩIV> _o يiIاو
لا i.iﺡ. لV> _o ¤Il.,Iا _.s.ﺱ. Range !P ¤.IﺡoIا تlî... صl>Iا LAN ..ﺡ
لا اiه _Ic .±... ﺹ>. يأ ×.L.ﺱ. Port لا ﺱs. مi>.ﺱ.و !P Nulticast
Il.,Iا eiه لl.s..ا ¤ ، ..ﺡ. تlç> ةic _Iإ ¤Il.ر لl.رV .Il.Iا د«îIا مi>.ﺱ.
لا »ºر مi>.ﺱ. Port 5020 لا _o± و Group 22+.100.0.1 لl.oآ :
: # C
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class NultiSend
{
public static void Nain()
{
Socket server = new Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse("22+.100.0.1"),
5020);
byte|| data = Encoding.ASC!!.GetBytes("This is a test message");
server.SendTo(data, iep);
server.Close();
}
}

29
VB.NET:
!mports System
!mports System.Net
!mports System.Net.Sockets
!mports System.Text

Class NultiSend

Public Shared Sub Nain()
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Pars("22+.100.0.1"),
5020)
Dim data As Byte() = Encoding.ASC!!.GetBytes("This is a test message")
server.SendTo(data, iep)
server.Close()
End Sub
End Class

..,a.. l.oº ¤.اi.Iا .º لا Socket .هو ¤Il.,Iا _.s.ﺱ. ف«. ..Iا ¤ç>Iا i.iﺡ..
) ¤î..Iا _Ic .±... ﺹ>. يأ لا ماi>..l. !P Nulticast Group دiﺡoIا ( i.iﺡ. ».
ع«. لا Socket مi>.ﺱoIا ل«آ«.,.Iاو ، لا ×±«. :Iذو فiçIا ¤Ls. i.iﺡ. :Iذ ia.و !P
Nulticast ا »ºر ﻩa...و i.,. يiIا ل Port ﻩIV> _o تl.l..Iا لl.s..ا »... ..Iا :


..,a. مi>.ﺱ. ف«. لl.s..Vا rol.,. ءl..Vو لا Socket لا ..±.و ﻩﺱs.
UdpClient Object ºر ﻩI i.ﺱ.و لا » Port ﻩ.Ic ..±.Iا i.,. ..Iا :

: # C
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class UdpClientNultiRecv
{
public static void Nain()
{
UdpClient sock = new UdpClient(5020); // لا »ºر _Ic ..±.IاPortاiه
sock.JoinNulticastGroup(!PAddress.Parse("22+.100.0.1"), 50);
دiﺡoIا ىioIا _Ic ..±.. ف«. :.ا ..a. اiهو

!PEndPoint iep = new !PEndPoint(!PAddress.Any, 0);

// ..l. ع«.Iا _o ¤º«s±o .º ¤Il.,Iا ¤..a.و تl.l..Iا لl.s..ا
byte|| data = sock.Receive(ref iep);

// _Iإ _.«ﺡ.Iا .î.أ ¤.l.Iا _Ic ¤Il.,Iا ¤cl.L ». د«آ
string stringData = Encoding.ASC!!.GetString(data, 0, data.Length);
Console.WriteLine("received: {0} from: {1}", stringData, iep.ToString());
sock.Close();}}
30
VB.NET:
!mports System
!mports System.Net
!mports System.Net.Sockets
!mports System.Text

Class UdpClientNultiRecv

Public Shared Sub Nain()
Dim sock As UdpClient = New UdpClient(5020)
sock.JoinNulticastGroup(!PAddress.Parse("22+.100.0.1"), 50)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim data As Byte() = sock.Receive(iep)
Dim stringData As String = Encoding.ASC!!.GetString(data, 0,
data.Length)
Console.WriteLine("received: {0} from: {1}", stringData, iep.ToString)
sock.Close()
End Sub
End Class


.د«îIا ماi>..ا _îo. loآ lçIl.رإ و تl.l..Iا لl.s..V ةدia.o ق,L i>«. ﻩ.ا LﺡV _.
لl.رإ :.îo. loآ لl.s..Vا و لl.رVI rol.,.Iا ﺱs. .º _.s.lﺱIا !mage _Iإ
ﺹ.Iا ..l> ) ×.ا,Iا _±sIا ,L.ا ( _Iإ ﻩI.«ﺡ. _îo. ,>أ ء.. يأ وأ Binary :.Ic lo ذإ
لا ¤ºl±إ ى«. memory Stream لاو Binary Reader لاو Binary Writer _Iإ
:.îo. loآ لl.s..Vا و لl.رVا د«آ c تlç> _Iإ ا,.olîIا ,.c ةر«ﺹ لl.رV rol.,. _o
. ..Iاو ¤.ﺹl>Iا ﺱs. ماi>..l. ةدia.o .º lçﺡ,. _Ic ..l ×.ا,Iا _±sIا ...

























31
Dot Net Networks 8 TCP/!P Programming
Chapter 2
Streaming in Dot Net


2- Nanaged !/O: Streams, Readers, and Writers

A. Stream Classes
B. Stream Nembers
C. Stream Nanipulation
D. Simple Remote Control Application Using
StreamReader 8 StreamWriter Classes

















32
o.>,ìا _o>,ìا ¤ا oa:

2.1 : and Writers , Readers , Streams : O ] Managed I :

c _î.. لوVا ء,>Iا .º ls.l. l..iﺡ. لا تloاi>..ا _c مl Streams Library
لl.رV lçoاi>..او Binary Data و Text Data لl.ر|. l.oº لl.oآو ,>l _Iإ زlç> _o
لا _o ةر«ﺹ Client لا _Iإ Server لا ماi>..l. Binary Reader 8 Binary Writer ..
لا تl..îo ءl..إ _o فiçIا نإ Stream _o تl.l..Iا _s. ¤.Ioc _.çﺱ. «ه _Iإ نlîo
وأ تlsIoIا ×o _ola.. لlﺡIا «ه loآ زlç>Iا ﺱs. _>اد وأ ¤î..Iا ,.c ءا«. ,>أ
l..Ic _çﺱ. ..ﺡ ,.«..oîIl. ل«ﺹ«o ,>l زlç> وأ ¤.º,L يأ وأ ¤a.lLIا ×o _ola.Iا
_Iإ lçI.«ﺡ. ¤.Ioc Byte Array ..lآ ..Iا _آl.oIا _o ,..îIا _ﺡ lo «هو lçIl.رإو
.Iا .º _.>o,.oIا ﻩ>ا«. ×o _ola Binary Data ..
لا ×o _ola.Iا _îo. Stream _oا,.oIا _..«I.l. Synchronous _oا,.o ,.aIاو
Asynchronous لا ×.o> _oa. .±ا,.ºا _î..و !O Streams _oا,.oIا ب«I.Vl.
لا قVc|. م«s. ذإ مlL.Iا ¤..ادأ _Ic i.i.Iا e,..l. «ه ﻩ.º ..aIا _آV Processing
Unit لا .º Thread ia. Vإ ,>l ,oأ يأ i.s... roﺱ. V ..ﺡ. rol.,.II ¤±±>oIا
..آ اذإ ¤Ilﺡ .º _oا,.oIا ب«I.Vا ماi>..ا li.ا r±.. Vو ¤.رl>Iا ¤.IoaIا _o ءlç..Vا
لا _.o l..ﺱ. ¤..L. ¤.l.آو ةءا,º ة,ç>أ ×o _ola.. Floppy Disk لا وأ Nagnetic Tape
.Iا تl.>o,.Il. اi> ¤oço lç.îI لا وأ .s.sﺡIا _o,Iا ¤oL.أ _Ic io.a. . Real Time
Systems «هو تl.l..Iا لl.s..او لl.رإ ¤.Ioc .º _oا,.oIا ب«I.Vا io.a. lç.أ ..ﺡ
lç.Ic ¤I.oVا _oو ,oVا i.s.. _o ءlç..Vا _.ﺡ _Iإ ى,>أ ¤.Ioc يl. مl.sIا ×.o. lo
>ﺡ ¤oL.أ وأ .î..Iا i.ﺹ,Iا .º عاi.Vا وأ .ﺡﺱIا ¤oL.أ ¤ºlL. _ﺡ. وأ ,آاi.Iا ,
lه,.cو ..lçIا .. ¤s.lﺱIا صا«>Iا د«>و _Iإ جl.ﺡ. V :>ol.,. نlآ اذإ ¤Ilﺡ .º la.L
_oا,.o ,.aIا ب«I.Vا ماi>..l. r±.. lهi.c Asynchronous _o ×.L.ﺱ. ..ﺡ
¤.IoaIا ءlç.إ رlL..V ¤>lﺡIا نوi.و ¤>IlaoIا ةiﺡو .º ى,>أ تl.Ioc i.s.. ﻩIV>
¤.رl>Iا ءl..إ ».. ذإ Separate thread جا,>إ وأ لl>دإ .IL ¤.Ioc _îI ,.å. V loo
لV> _o ى,>. ¤.l.îIا وأ ةءا,sIا ¤.Ioc ..lآ اذإ ﻩoاi>..l. r±..و مlL.Iا ¤..ادأ _Ic
... توiIا .º _oا,.o ,.aIا _c _oا,.oIا د«..oIا ,.o. _îo.و l..ﺱ. ¤..L. ة,ç>أ
¤oIآ د«>«. Begin وأ End .º lç.Ic لl.oآو _oا,.o ,.aIا د«..oIا ».ا ¤.اi.
BeginWrite و BeginRead لا و EndWrite لا و EndRead ..


Vوأ : Stream Classes

لا تl.Ioc ... توiIا »ci. Streams لا _o ¤c«o>o. Classes .ﺡ. ¤>رi.oIاو
Name Space System.!O تl.IoaI مi>.ﺱ. ..Iاو I جا,>Vا و لl>دVا s. تl.l..Iا _ .
لا ¸a. مi>.ﺱ. Stream Classes ، Backing storage ، lç.Ic ¤I.oVا _oو
FileStream لا و BufferedStream لاو NemoryStream V lç±a. ن|º :Iiآو
يأ مi>.ﺱ. Back Storage لا lç.Ic ¤I.oVا _oو NetworkStream مi>.ﺱ. ..Iاو
لا _s.I Stream .ا نوi.و ¤î..Iا ,.c ماi>. Backing Storage ، لا »ﺱs. و
Stream Classes .Il.Iا _î.Iا .º loآ ... توiIا .º :

33


1 - BufferedStream Class : ة,آاiIا _o _.ao راiso ,>ﺡI ..l.أ _î.. مi>.ﺱ.و
لا تl.>o,.Iا ¸a. مi>.ﺱ. loآ ¤..ao ¤.Ioc i.s..I .ºåo _î.. Buffering _.ﺱﺡ.I
اiآ ن«î. ..ﺡ ¤..ادVا lç.Ic لl.oآو لl.s..Vا وأ لl.رVا و ¤>IlaoIا _.. ¤L..و ة,آ
lç.cl.L دا,oIا تl.l..Iا _.,>.I ¤L..و ة,آاذ ¤a.lLIا مi>.ﺱ. ..ﺡ ¤cl.LIا تl.>o,.
.ºåo _î.. ، لا ماi>..ا _o ..l.Vا فiçIا _oî. Buffering ..Iا تl.IoaIا .º
>Vا و لl>دVا تl.Ioc _o ع,.أ rIlaoIا lç.º ن«î. تl.l..Iا ¤>Ilao ».. ..ﺡ جا,
لا .º lça±وو Buffer _î.. ¤..ادVا _.ﺱﺡ. .º »هlﺱ. lo «هو lçIl.رإ رlL..ا .º
,..آ ، لا مi>.ﺱ.و BufferedStream لا ×o تlî..Iا تl.>o,. .º l.دlc
NetworkStream V ..ﺡ ة,آاiIا .º ¤î..Iا ,.c lçIl.رإ دا,oIا تl.l..Iا _.,>.I
VîIا اiه مi>.ﺱ. س Backing storage ls.l. l.,آذ loآ ..

,>ﺡ ».. .±ا,.ºا _î.. +096 bytes لا ماi>..ا i.c BufferedStream _îo.و
¤>lﺡIا .ﺱﺡ lçI.Is. وأ lç.دl.ز .. لا مi>.ﺱ.و BufferedStream .I. loآ لl.oآ :

# C
using System;
using System.Text;
using System.!O;
namespace Network_Buffering
{
class Program
{
static void Nain(string|| args)
{
ASC!!Encoding asen = new ASC!!Encoding();
byte|| xx = asen.GetBytes("Hello Buffering");

34
NemoryStream ms = new NemoryStream(xx);
readBufStream(ms);
}
public static void readBufStream(Stream st)
{
// Compose BufferedStream
BufferedStream bf = new BufferedStream(st);
byte|| inData = new Byte|st.Length|;

// Read and display buffered data
bf.Read(inData, 0, Convert.To!nt32(st.Length));
Console.WriteLine(Encoding.ASC!!.GetString(inData));
}
}
}


VB.NET:
!mports System
!mports System.Text
!mports System.!O
Namespace Network_Buffering

Class Program

Shared Sub Nain(Byval args As String())
Dim asen As ASC!!Encoding = New ASC!!Encoding
Dim xx As Byte() = asen.GetBytes("Hello Buffering")
Dim ms As NemoryStream = New NemoryStream(xx)
readBufStream(ms)
End Sub

Public Shared Sub readBufStream(Byval st As Stream)
Dim bf As BufferedStream = New BufferedStream(st)
Dim inData(st.Length) As Byte
bf.Read(inData, 0, Convert.To!nt32(st.Length))
Console.WriteLine(Encoding.ASC!!.GetString(inData))
End Sub
End Class
End Namespace

_Iإ ﺹ. _.«ﺡ.. l.oº ..ﺡ Byte Array لا ماi>..l. ASC!!Encoding .º ﻩI.oﺡ.و
لا ,.c NemoryStream د«..oIا _Iإ el.I.را ». readBufStream ..ﺡ lهl.l..ا ..Iاو
لا lçIV> _o l.I.s..ا Stream لا سVîIا ماi>.ﺱ. ¤.ºåo ة,آاذ .º el.Ioﺡو
BufferedStream ﻩ.l.«.ﺡo ¤cl.L. l.oº ». ماi>.ﺱ. ى,>ا ة,o ﺹ. _Iإ ﻩI.«ﺡ. ia.
لا Encoding.ASC!! ﻩ.cl.Lو ..

2 - NemoryStream Class : لا ¤.Ioa. ﻩ... «هو Buffring i.> _ﺡآ ,..a. ذإ ¤s.lﺱIا
_c :..a. ..ﺡ لl.s..Vا وأ لl.رVا _.º ة,آاiIا .º .ºåo _î.. تl.l..Iا _.,>.I
.. ¤.IoaIا ع,ﺱ. loo .Io _î. _Ic lç..,>. لl.oآ .I. loآ مi>.ﺱ.و ,..آ _î
ة,آاiIا .º ةر«ﺹ _.,>.I lç.oi>..ا ..ﺡ :
35
# C
NemoryStream ms = new NemoryStream();
pictureBox1.!mage.Save(ms, System.Drawing.!maging.!mageFormat.Jpeg);

byte|| arr!mage = ms.GetBuffer();
ms.Close();

VB.NET:
Dim ms As NemoryStream = New NemoryStream
pictureBox1.!mage.Save(ms, System.Drawing.!maging.!mageFormat.Jpeg)
Dim arr!mage As Byte() = ms.GetBuffer
ms.Close

3 - NetworkStream Class : ls.l. lçoاi>..l. l.oº loآو ، لا ×o _ola.. م«s. ..ﺡ
Stream لا ماi>..l. ¤î..Iا ,.c ﻩIl.رV Socket _o lç.lci..ا »..و Name
Spaces System.Net.Sockets ,..a.و سVîIا NetworkStream ﻩ.l. unbuffered V ذإ
_Ic ي«.ﺡ. Backing Storage لا ماi>..ا _±s.و BufferedStream Class ﻩao
ءادVا _.ﺱﺡ.I lç..,>.. l.oº ..Iا ةر«±Iا لl.رإ i.,. ..ﺡ لl.oآ .I. loآ مi>.ﺱ.و
Iا .º لا ,.c ,>أ زlç> _Iإ ة,آاi. _.lﺱIا لl.o Socket :

# C
TcpClient myclient = new TcpClient (“localhost”,5020);//Connecting with
server

NetworkStream myns = myclient.GetStream ();

BinaryWriter mysw = new BinaryWriter (myns);
mysw.Write(arr!mage);//send the stream to above address
mysw.Close ();
myns.Close ();
myclient.Close ();

VB.NET:
Dim myclient As TcpClient = New TcpClient(localhost, 5020)
Dim myns As NetworkStream = myclient.GetStream
Dim mysw As BinaryWriter = New BinaryWriter(myns)
mysw.Write(arr!mage)
mysw.Close
myns.Close
myclient.Close

+ - FileStream : ماi>..l. lç.lci..ا ».. Name Spaces System.!O مi>.ﺱ.و
.Io _o ةءا,sIا وأ .Io _Iإ ¤.l.îII ءا«. تlsIoIا ×o _ola.Iا .º ..l.ا _î..
سVîIا eiه ,..a.و Backing Storage Class ة,آاذ مi>.ﺱ. ..ﺡ Buffer _.,ﺡ.I
¤.l.îIا ¤.Ioc _o ءlç..Vا _.ﺡI ة,آاiIا .º .ºåo _î.. تl.l..Iا ر«oVا _oو ةءا,sIا وأ
.I. loآ مi>.ﺱ.و ﻩ.Ic ¤.l.îIا وأ ﻩ.o ةءا,sIا دا,oIا .IoIا رlﺱo i.iﺡ. lç.º ¤olçIا :




36
# C
FileStream FS = new FileStream(@"C:\NyStream.txt",
FileNode.CreateNew);// Any Action For Example CreateNew to Create Folder

VB.NET:
Dim FS As FileStream = New FileStream("C:\NyStream.txt",
FileNode.CreateNew)

لا ماi>..ا l..îo. Enumeration لا ×o ¤.Il.Iا FileNode :


1 - Append Vﺹا د«>«oIا .IoIا _Iإ lo ﺹ. ¤ºl±V
2 - Create _oa. م«s.و i.i> .Io ءl..V overwriting د«>«o .IoIا نlآ اذإ _Ilﺡ .º
ﺱo _î.. _.
3 - CreateNew لا .º loآ «هو Create .La. ﻩ.ا Vإ Exception .IoIا د«>و ¤Ilﺡ .º
_.ﺱo _î..
+ - Open .La. ..ﺡ lo .Io ةءا,sI Excption دiﺡoIا .IoIا د«>و مic ¤Ilﺡ .º
5 - OpenOrCreate م«s. eد«>و مic ¤Ilﺡ .ºو ﻩ.ءا,s. م«s. .IoIا i>و اذإ ¤Ilﺡ .º
ﻩ.l..|. .
6 - Truncate و lcرlº ﻩIa>و .IoIا تl.«.ﺡo فiﺡI مi>.ﺱ.


l.:l: : Stream Members :
لا تl..îo lç. ك,... ..Iا سد«...oIا و «ﺹا«>Iا _o ¤c«o>o :Il.ه Stream .هو
.I. loآ :


1 - CanRead و CanWrite لا نlآ اذإ ¤º,aoI مi>.ﺱ.و Stream _.s. مi>.ﺱoIا
..ﺡ V مأ ¤.l.îIا وأ ةءا,sIا ¤.Ioc ¤o.º ×>,. True و _.s. نlآ اذإ ¤Ilﺡ .º False .º
ىio ﺹﺡsI ¤.l.îIا وأ ةءا,sIا ¤.Ioc ءا,>إ _.º ةدlc مi>.ﺱ.و _.s. V ﻩ.أ ¤Ilﺡ
¤IوlﺡoIا _.º ¤.ﺡV±Iا ..
2 - CanSeek لا مi>.ﺱ. ..ﺡ Seeking لا ׺«o i.iﺡ.I ةدlc Current Stream
مi>.ﺱ. ..Iا تl.VîIا »ci. ةدlaIاو Backing Storage لا _.o ¤.IoaIا eiه
37
FileStream ¤o.º ×>,. lهi.cو True ¤o.º ×>,.و false لا نlآ اذإ ¤Ilﺡ .º Stream
Class _Ic ي«.ﺡ. V Backing Storage .
3 - CanTimeout ¤o.º ×>,.و True لا نlآ اذإ ¤Ilﺡ .º stream لا ¤.ﺹl> _Ic ي«.ﺡ.
Timeout ¤.IoaII دiﺡo .ºو .La. ..Iاو .
+ - Length لا »>ﺡ ¤º,aoI مi>.ﺱ.و Stream لl. Byte ¤º,aoI lç.o ةدls..Vا _îo.و
لا ¤.lç. Stream لا »>ﺡ _Ic ءl.. ¤º«s±oIا »>ﺡ i.iﺡ.I وأ Stream .
5 - Position لا مi>.ﺱ.و Get و Set ل ׺«oIا i.iﺡ. وأ ¤º,aoI Stream
لا تl..îo ك,...و Stream loآ .هو سد«..oIا _o ¤c«o>o. .I. :

1 - ¤.oا,.oìا سدu..oìا Methods Synchronous :

! . Read و ReadByte ةءا,sI مi>.ﺱ.و Stream Data لا .º ﻩ..,>.و Buffer
لا ماi>..l. lç.ءا,º »... ..Iا تl..l.Iا دic i.iﺡ. _îo.و ReadByte
لا ¤.lç. ¤º,ao lçIV> _o ×.L.ﺱ. loآ Stream لا ×>,. ..ﺡ Read
¤o.º 0 لاو ReadByte ¤o.º - 1 لا ءlç..ا ¤Ilﺡ .º Stream .
!! . Write لاو WriteByte لا ,.c لl.رVا ¤.IoaI مi>.ﺱ.و Stream _îo.و
لا ماi>..l. ة,o _آ .º lç..l.آ »... ..Iا تl..l.Iا دic i.iﺡ.
WriteByte .

2 - ¤.oا,.oìا ,.c سدu..oìا Methods Asynchronous :

! . BeginRead لاو BeginWrite ¤.IoaI مi>.ﺱ.و ¤.l.îIا وأ ةءا,sIا
لا ماi>..l. Stream .º loآ تا,..oورl. ¤ﺱo> i>l.و _oا,.oIا ,.aIا
.Il.Iا _î.Iا :


1 - لا Byte Buffer ﻩ.Ic ¤.l.îIا وأ ﻩ.o ةءا,sIا ¤.IoaI مi>.ﺱ. ف«. ..Iاو
2 - لا offset ¤.l.îIا وأ ةءا,sIا ׺«o ﻩ.º دiﺡ. ف«. يiIاو
3 - لا numByte .iﺡ. ﻩ.º ».. ف«. يiIاو »... ..Iا تl..l.Iا _o _±ºVا iﺡIا i
lç.ءا,º وأ lç..l.آ
+ - لا AsyncCallback «هو Optional Delegate ءlç..Vا i.c ﻩ.lci..ا ».. ..ﺡ
¤.l.îIا وأ ةءا,sIا ¤.Ioc _o
5 - لا Stateobject .هو User Provided Object لا ,.o.I مi>.ﺱ.و Read 8
Write Request لا e,.c _c Requests .
,. لا ×> Begin Nethods لا !AsyncResult لا ¤Ilﺡ _.o. يiIاو Stream
Operation .

!! . EndRead لاو EndWrite لا i.s.. l.درأ اذإ ¤Ilﺡ .º مi>.ﺱ.و Stream
Operation لا _o ءlç..Vا ia. Stream Operation .IlﺡIا ، رlL..l. _s.. ..ﺡ
oIا ¤.IoaIا is.. ». ¤s.lﺱIا ¤.IoaIا ءlç..ا ¤.«IL

لا ةرادV مﺩ>.a: L.ìاو دu..oìا _ﻉ: كl.هو Stream Lهو :

1 - Flush لا تl.«.ﺡo ×.,s.I مi>.ﺱ.و Buffer ».. ..ﺡ ةدiﺡoIا ¤.IoaIا مlo.إ ia.
لا تl.«.ﺡo _s. Buffer لا _Iإ Destination .º ei.iﺡ. ». يiIا Stream Object .

2 - Close لا قVcV مi>.ﺱ.و Stream ,ﺡ.و لا ,. Resources لا _.º _o ةز«>ﺡoIا
Stream Object ب صl>Iا ء,>Iا .º lçoاi>..l. r±..و Finally block نأ _o iآl.Iو
لا Stream يأ ثوiﺡ ¤Ilﺡ .º درا«oIا ¤ºlآ ,.,ﺡ.و ﻩºVcإ »... Exception ءl..إ
rol.,.Iا قVcإ ia. ة,آاiIا .º درا«oIا eiه ءls. مic نlo±Iو i.s..Iا .
38

3 - SetLength لا »>ﺡ i.iﺡ.I مi>.ﺱ.و Stream _آV ﻩIl.s..ا وأ ﻩIl.رإ i.,. يiIاو
لا نlآ اذإ ¤Ilﺡ .º Stream لا .º دiﺡoIا _o _ºأ SetLength _Iإ :Iاذ يدå. ف«.
لا عlLs.ا Stream ».I. _î.. ﻩI«ﺹو مicو ، Vإ ¤.ﺹl>Iا eiه ماi>..ا ×.L.ﺱ. _I
_o :IiI ¤.ﺡV±Iا :Io. :.ا تiآl. اذإ ¤.ﺹl>Iا لV> CanWrite و CanSeek r±.. اiI
لا »>ﺡ i.iﺡ. _.º Vوأ ¤.ﺡV±Iا ﺹﺡs. Stream .

l.ìl: : Stream Manipulation :

لا تl..îo ماi>..ا _îo. Stream _s.I Binary Data وأ Text ».. ةدlaIا .ºو
لا ماi>..ا BinaryReader لا و BinaryWriter لا ×o _ola.I Binary Data »..و
لا ماi>..ا StreamReader لاو StreamWriter لا ×o _ola.I Text ،
لا ماi>..ا »..و ASC!!Encoding وأ UnicodeEncoding _o _.«ﺡ.I Stream _Iإ
Text _oو لl.s..Vا i.c Text _Iإ Stream _o ¤c«o>o مi>.ﺱ. ..ﺡ لl.رVا i.c
.Il.Iا _î.Iا .º loآ .هو سد«..oIا :



1 - GetByteCount .هو Overloaded Nethod i>l. ..ﺡ Character Array وأ String
_.ao ﺹ. _s.I lç>l.ﺡ. ف«. ..Iا تl..l.Iا دic ×>,.و ..
2 - GetBytes لا _.«ﺡ.I String _Iإ Byte Array لا ماi>..l. lçIl.رإ ×.L.ﺱ. _.ﺡ
Stream .
3 - GetCharCount i>l. ..ﺡ Byte Array Iا ف,ﺡVا دic ×>,.و لا .º ن«î. ف«. ..
String لا .º وأ Character Array .
+ - GetChars _o _.«ﺡ.I مi>.ﺱ.و Byte Array _Iإ String لl.s..ا i.c مi>.ﺱ.و
لا _o تl.l..Iا Stream ى,>أ ة,o ﺹ. _Iإ lçI«ﺡ. ..ﺡ .

لا go _olﻉ.ìو StreamReader لا و StreamWriter _a.ì Text Vوأ u>:
لا _o lq:lcﺩ.aا System.IO Ll: loآ مﺩ>.a:و _a..a o.: :

StreamReader .Io _o ةءا,sII :
# C
StreamReader str = File.OpenText(openFileDialog1.FileName);
textBox1.Text = str.ReadToEnd();

VB.NET:
Dim str As StreamReader = File.OpenText(openFileDialog1.FileName)
textBox1.Text = str.ReadToEnd


39
StreamWriter .Io _Iإ ¤.l.îII :
# C
string fname = saveFileDialog1.FileName;
StreamWriter fsave = new StreamWriter(fname);
fsave.WriteLine(textBox1.Text);

VB.NET:
Dim fname As String = saveFileDialog1.FileName
Dim fsave As StreamWriter = New StreamWriter(fname)
fsave.WriteLine(textBox1.Text)

لا go _olﻉ.ì و BinaryReader لاو BinaryWriter _a.ì Binary Data o.:
لا _o lq:lcﺩ.aا System.IO Ll: loآ مﺩ>.a:و _a..a o.: :

BinaryReader ةءا,sI Binary Data لا _o Stream :
# C
NetworkStream myns = new NetworkStream(mysocket);
BinaryReader br = new BinaryReader(myns);
BinaryWriterr لl.رV BinaryData لا _Iإ Stream لا ,.c Socket :

TcpClient myclient = new TcpClient("localhost", 5020);
NetworkStream myns = myclient.GetStream();
BinaryWriter mysw = new BinaryWriter(myns);
mysw.Write(arr!mage);

VB.NET:
Dim myns As NetworkStream = New NetworkStream(mysocket)
Dim br As BinaryReader = New BinaryReader(myns)
Dim myclient As TcpClient = New TcpClient("localhost", 5020)
Dim myns As NetworkStream = myclient.GetStream
Dim mysw As BinaryWriter = New BinaryWriter(myns)
mysw.Write(arr!mage)

Remote Control Example لا ماﺩ>.al: Writer & Stream Reader :

ب ﻩ..a ¿ol:,: ﻩ.8 مﺩ>.a: فua ﻁ.a: La..b: لl.o Chatting _آV
لا _ìإ ,oاوأ ءlbcV ﻩoﺩ>.a: فua Server لlaر|: l.o8 اذإ ض,.a: C.>
¤olآ notepad لا _ìإ server لا ﺡ.a: مua: نÌ: notepad V.o l.o8 اذإو ﻩ.8
¤:l.S: Calc إ lqìlaرإو لا _ì Server اíSهو ﻩ.8 ¤.alﺡìا ¤ìVا ﺡ.a: فua :

Vوأ : لl.رVا rol.,. ءl..إ Client : لا rol.,. _c لl.رVا rol.,. .I.>. V Client
Chat لا .º ﻩ.l..|. l.oº يiIا Chapter1 _o _آ ﻩ.º مi>.ﺱ.و TCP Connection
لاو NetworkStream لا و StreamWriter l.رVا ¤.Ioc ءا,>V د«..oIا ماi>..l.º ل
WriteLine لا _o± ةد«>«oIا StreamWriter Object ب«.îoIا ﺹ.Iا _.«ﺡ. ¤.Ioc »..
لا .º Textbox لا _o ¤c«o>o _Iإ Bytes لا ماi>..l. lçIl.رإ »..I
NetworkStream لا ,.c TCP Socket Connection لا rol.,. _Iإ Server »º ءi.IIو
.º loآ i.i> عو,.o ءl..|. .Il.Iا _î.Iا :

40


¤ºl±|. »º ». Name Spaces ¤.Il.Iا :
# C
using System.Net.Sockets ;
using System.!O;
.º Send Button .Il.Iا د«îIا ¤.l.î. »º :
try
{
TcpClient myclient = new TcpClient (txt_host.Text,5020); // ..,a. لا Socket
NetworkStream myns = myclient.GetStream (); // .î>.وا ».,.ﺱIIا _Iإ eدl..إ
StreamWriter mysw = new StreamWriter (myns);
mysw.WriteLine(txt_msg.Text);

mysw.Close ();
myns.Close ();
myclient.Close ();
}
catch (Exception ex) {NessageBox.Show (ex.Nessage );}

VB.NET:
imports System.Net.Sockets ;
imports System.!O;

Try
Dim myclient As TcpClient = New TcpClient(txt_host.Text, 5020)
Dim myns As NetworkStream = myclient.GetStream
Dim mysw As StreamWriter = New StreamWriter(myns)
mysw.WriteLine(txt_msg.Text)
mysw.Close
myns.Close
myclient.Close
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try

لا rol.,. ءl..Vو Server لا لl.s..ا _Ic _oa. يiIاو Stream _Iإ ﻩI.«ﺡ.و Text
ى,>أ ة,o .. .Il.Iا _î.Iا .º loآ i.i> عو,.o ءl..|. »º :

41


¤ºl±|. »º Name Spaces ¤.Il.Iا :
# C
using System.Net.Sockets ;
using System.!O;
using System.Threading;
ا ..رla.Iا ¤ºl±إ ». ¤.Il.I :
TcpListener mytcpl;// Objects Declaration
Socket mysocket;
NetworkStream myns;
StreamReader mysr;
.I. loآ ةi.i> د«..o ءl..|. م«s. ». :
void our_Server ()
{
mytcpl = new TcpListener (5020);// Open The Port
mytcpl.Start ();// Start Listening on That Port
mysocket = mytcpl.AcceptSocket ();// Accept Any Request From Client and
Start a Session
myns = new NetworkStream (mysocket);// Receives The Binary Data From
Port
mysr = new StreamReader (myns);// Convert Received Data to String
string order = mysr.ReadLine();

// you can add any order and Response Here
if (order=="notepad") System.Diagnostics.Process.Start("notepad");
else if (order=="calc") System.Diagnostics.Process.Start("calc");
else NessageBox.Show("Sorry Sir Your Request is not in my hand",order);

mytcpl.Stop();// Close TCP Session

if (mysocket.Connected ==true)// Looping While Connected to Receive
Another Nessage
{
while (true)
{
our_Server ();// Back to First Nethod
}
}
}
VB.NET:
Private mytcpl As TcpListener
Private mysocket As Socket
Private myns As NetworkStream
Private mysr As StreamReader
42

Sub our_Server()
mytcpl = New TcpListener(5020)
mytcpl.Start()
mysocket = mytcpl.AcceptSocket
myns = New NetworkStream(mysocket)
mysr = New StreamReader(myns)
Dim order As String = mysr.ReadLine
!f order = "notepad" Then
System.Diagnostics.Process.Start("notepad")
Else
!f order = "calc" Then
System.Diagnostics.Process.Start("calc")
Else
Nsgbox("Sorry Sir Your Request is not in my hand", order)
End !f
End !f
mytcpl.Stop()
!f mysocket.Connected = True Then
While True
our_Server()
End While
End !f
End Sub

لا _Ic ..±.. د«..oIا eiه م«s. ..ﺡ Socket يأ دورو ¤Ilﺡ .º Request م«s.
ءl..إو ﻩ.Ic ¤sºا«oIl. Session ,.c تl.l.. يأ دورو ¤Ilﺡ .ºو ﻩao ةi.i> لا Socket
لا ماi>..l. lçoIﺱ.. StreamReader _Iإ lçI«ﺡ.و Text ¤Il.,Iا ﺹﺡs. م«s. ».
..lآ اذإ V.oº ¤.L,.Iا _o>Iا ماi>..l. .ه ¤Il.,Iا notepad lç.lci..ا »..
د«..oIا ماi>..l. Start سVîIا _o± ةد«>«oIا Process .º ةد«>«oIاو Name
Spaces System.Diagnostics ...
_o± lçI.a..Iو Thread ..,a. ×±و _o i.V i.i> لا Thread _.a..Iا أi. ثiﺡ .º
_I Form .I. loآ :
# C
private void Form1_Load(object sender, System.EventArgs e)
{
Thread myth;
myth= new Thread (new System.Threading .ThreadStart(our_Server));
myth.Start ();
}

لا ثiﺡ .º .Il.Iا ¤ºl±|. »º ». Form Closing قVcإ _o iآl.I :Iذو لا Socket لاو
Stream rol.,.Iا .º ..
private void Form1_Closing(object sender,
System.ComponentNodel.CancelEventArgs e)
{
try
{
mytcpl.Stop ();
Application.ExitThread ();
Application.Exit();
43
}
catch (Exception ex) {NessageBox .Show (ex.Nessage );}

VB.NET:

Private Sub Form1_Load(Byval sender As Object, Byval e As
System.EventArgs)
Dim myth As Thread
myth = New Thread(New System.Threading.ThreadStart(our_Server))
myth.Start()
End Sub

Private Sub Form1_Closing(Byval sender As Object, Byval e As
System.ComponentNodel.CancelEventArgs)
Try
mytcpl.Stop()
Application.ExitThread()
Application.Exit()
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try
End Sub






























44
Dot Net Networks 8 TCP/!P Programming
Chapter 3
The Socket & Network Layer Programming


The Socket 8 Network Layer Programming

A. Socket Programming
B. Socket Class Nembers
C. TCP 8 UDP Classes Nembers
D. Asynchronous Sockets



















45
o.>,ìا _o>,ìا ¤ا oa:

3.1 : Network Layer Programming & Socket :

لا ¤s.L ¤>o,. _c V.±s. ,.آأ _î.. _... ف«. ء,>Iا اiه .º Network Layer
_Iإ و _o lçI.«ﺡ. ia. تl.l..Iا لl.s..او لl.رV lçao _ola.Iا ».. ..Iا .هو Stream
¤î..Iا ,.c ، لا ماi>..l. ls.l. l.oº TCP و UDP ق,sIا l...و لl.s..VIو لl.رVI
لا _c ثiﺡ.. ف«. ء,>Iا اiه .ºو loç... Socket Programming لاو TCP 8 UDP
Classes _c ثiﺡ.. ف«. ¤.lç.Iا .ºو Asynchronous Socket .

Vوأ : Socket Programming

فو,aoIا _o نأ لا Socket o lçIV> _o تl.l..Iا _s. ».. ..Iا ةادVا .ه _Iإ زlç> _
..,a. ¤.اi.Iا .º م,I. lçoاi>..Vو ,>أ Name Space System.Net.Sockets ..ﺡ
اiه ي«.ﺡ. Name Space _o »>± دic _Ic لا Classes .º lçoاi>..ا ».. ..Iاو
¤î..Iا تl.>o,. «هو loهأ _c ثiﺡ.. ف«.و Socket Class ×o _ola.Iا _o. l..îo. ذإ
لا TCP لا وأ UDP يأ ×o وأ لا ن«î..و ,.l.o _î.. تV«آ«.,.Iا _o ,>أ ع«. Socket
Object Nethod ا,..oورl. ¤.V. _o ت .I. loآ :
: # C
Socket NySocket = new Socket(AddressFamily. , SocketType., ProtocolType.);

VB.NET:
Dim NySocket As Socket = New Socket(AddressFamily, SocketType,
ProtocolType)

لا ¤.c«. i.iﺡ. لوVا ,..oورl.Iا .º ».. ..ﺡ !P Address ﻩao _ola.. ف«. يiIاو
lç.oو تارl.>Iا _o ,..آ دic :.La.و !PX لا تlî.. .º مi>.ﺱoIاو Novel وأ ATN
لا تlî.. .º مi>.ﺱoIاو ATN Networks وأ NetB!OS Address lه,.cو ... _oو
لا تارl.>Iا eiه »هأ !nterNetwork و تl.>o,.Iا ×o ».اد _î.. ﻩoi>.ﺱ. lo «ه
ع«. نأ _Ic ف,a.و تlî..Il. ¤ﺹl>Iا !P ع«.Iا _o «ه !Pv+ مlL. ×o دl.aoIا «هو
»... اi> ..,sIا _.s.ﺱoIا .ºو l.Ilﺡ ¤ºو,aoIا _.a..Iا ¤oL.أ .Icأو .º«.و,î.lo
لا ﻩIﺡo _ﺡ.Iو ﻩ.c ءl.a..Vا !Pv6 ، لا ع«. i.iﺡ. ».. ..l.Iا ,..oورl.Iا .º Socket
مi>.ﺱ. ف«. _ه يأ Stream ماi>..ا ».. lo ةدlcو ,>ا ء.. وأ تl.l..Iا لl.رV
لا Stream ع«.Iا _o _.ا,.Iا ¤.Lo. io.a.. l..ا ..ﺡ ¤oçoIا eiçI Stream ، ا,.>أو
ع«.Iا _o «ه _çº لl±.VI مi>.ﺱoIا ل«آ«.و,.Iا ع«. دiﺡ. UDP وأ TCP تV«آ«.و,. وأ
_.o ى,>أ !CNP !nternet Control Nessage Protocol وأ !GNP !nternet Group
Nanagement Protocol لا ءl..إ V.o i.,. l..ا وأ Socket ..,a.I !P Security Header
رl..>|. !PSecAuthenticationHeader ر«oVا eiه _.o ح,. _Ic ..l. ف«.و lه,.cو
«ا ءl..إ lsﺡV ، لا رl.>. ف«. l.هو TCP وأ UDP ا _oو لا ل«آ«.,. نأ فو,aoI TCP
lo ﺹ>. _Iإ _.ﺹ«.Iاو ل«ﺹ«Iا _o _sﺡ.Iا ¤.Ioc ءا,>إ ..a. اiهو ﻩ>«o ل«آ«.,. «ه
لا ل«آ«.,. loأ دiﺡo UDP _o _sﺡ.Iا ¤.Ioc »ci. V ﻩ.آV و l..ﺱ. ×.,. ل«آ«.,. «çº
.cاذVا ..Iا ¤.Ioc ءا,>V اi> i.so «هو ¤I.,oIا تl.l..II ».IﺱIا ل«ﺹ«Iا
Broadcast ..Iا تlc«o>o ءl..إو Nulticast Group .º el.ﺡ,. lo «هو ء,>Iا لوVا
لا ﻩ.º _...و .Il.Iا _î.Iا _Iإ ,L.أ ..l.Iاو Header لl. صl>Iا TCP لاو Header
لl. صl>Iا UDP loç... ق,sIا LﺡVو :


لا Vوأ TCP Header _o ن«î..و 32 Bits نا«.c _.,>. ﻩ.º ».. ..ﺡ iﺡا«Iا .î.II
oIا .º _., 16 Bits .º _.s.ﺱoIاو 16 Bits .º .î.II .IﺱIﺱ.Iا »º,Iاو 32 Bits
ºl±Vl. _sﺡ.Iا »ºرو ¤ لا _Iإ Checksum تl.l..Il. صl>Iا ء,>Iا ×±و ».. ¤.lç.Iا .ºو
:
46


TCP Header

Data Offset: + bits the number of 32 bit words in the TCP Header. This indicates
where the data begins. The TCP header (even one including options) is an integral
number of 32 bits long.
Window: The number of data octets beginning with the one indicated in the
acknowledgment field which the sender of this segment is willing to accept.


لا l..l. UDP Header _o ن«î..و 32 Bits _Ic ي«.ﺡ.و iﺡا«Iا .î.II تl.l..Iا _o
_.,oIا نا«.c 16 Bits لا و »I.ﺱoIا loأ Checksum .±ا,.ºا _î..و نl.رl..>ا loçº
لl.رVا ¤.Ioc .º »çoاi>..ا ».. V :

UDP Header

The Checksum in UDP Header. 16 bits.
Computed as the 16-bit one's complement of the one's complement sum of a pseudo
header of information from the !P header, the UDP header, and the data, padded as
needed with zero bytes at the end to make a multiple of two bytes. !f the checksum
is cleared to zero, then checksuming is disabled. !f the computed checksum is zero,
then this field must be set to 0xFFFF.







47
لا ماi>..ا Socket Programming ءl..V TCP Connection :
لا ماi>..l. لl±.Vا ¤.Ioc ,o. TCP Socket Connection _ﺡا,oIا _o ¤c«o>o.
.Il.Iا _î.Iا .º loآ .هو :


لا .º ¤.IoaIا أi.. ذإ Client لا و server لا ءl..|. Socket .I. loآ :
: # C
Socket NySocket = new Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp);

VB.NET:
Dim NySocket As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp)
لا L.ر ». Socket د«..oIا ماi>..l. .IlﺡIا ,.«..oîIا ×o Bind i.c Lsº مi>.ﺱ.و
..Vا .I. loآو لl.s :
: # C
!PEndPoint ip = new !PEndPoint(!PAddress.Any, 5020);
NySocket.Bind(ip);

VB.NET:
Dim ip As !PEndPoint = New !PEndPoint(!PAddress.Any, 5020)
NySocket.Bind(ip)
لا _Ic ..±.Iا ¤.Ioa. مl.sIا ». Port د«..oIا ماi>..l. دiﺡoIا listen iﺡ. :.îo.و د
»º,Iا ﻩI ر,o. دiﺡo ,.c دic ×±«Iو lçI«.º »... ..Iا ة,ç>Vا دic - 1 م«s. ».
د«..oIا ماi>..l. لl±.Vا _Ic ¤sºا«oIl. accept .I. loآو :
: # C
NySocket.Listen(-1);
NySocket.Accept();

VB.NET:
NySocket.Listen(-1)
NySocket.Accept



48
V> _o تl.l..Iا لl.s..ا »..و د«..oIا ل Receive _o ¤º«s±o .º تl.l..Iا _.a. ..ﺡ
ع«.Iا Byte .I. loآو :
: # C
byte||Received=new byte|102+|;
NySocket.Receive(Received);

VB.NET:
Dim Received(102+) As Byte
NySocket.Receive(Received)

ءl..|. l.oº l.هو Connection ع«.Iا _o TCP Ic lçs.,a..و لا _ Port ) 5020 لl.oآ (
لl. lçL.ر ».. ..ﺡ Socket اi>..l. م د«..oIا Bind ..,a.. l.oºو Listen ..lç. V
دiaIا - 1 ..


لl.رVا rol.,. ..,a.Iو TCP Client لا ماi>..l. Socket ..,a. _o i.V لا Socket
لا نا«.c دl..إو ى,>أ ة,o Server لا »ºرو Port فiçIا ¤Ls.. !PEndPoint ». لl.رإ
اi>..l. تl.l..Iا م د«..oIا Send لا .º ﻩs.,a. ». lo. لl.رVا ¤.Ioc »..و socket
مi>.ﺱ.. ..ﺡ Stream Socket .I. loآو :
: # C
String str = Console.ReadLine();
ASC!!Encoding asen = new ASC!!Encoding();
byte|| msg = asen.GetBytes(str);

Socket NySocket = new Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp);
!PEndPoint remote = new !PEndPoint(!PAddress.Parse("192.168.1.101"),
5020);
NySocket.Connect(remote);
NySocket.Send(msg);
NySocket.Close();

VB.NET:
Dim str As String = Console.ReadLine
Dim asen As ASC!!Encoding = New ASC!!Encoding
Dim msg As Byte() = asen.GetBytes(str)
Dim NySocket As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Dim remote As !PEndPoint = New
!PEndPoint(!PAddress.Parse("192.168.1.101"), 5020)
NySocket.Connect(remote)
NySocket.Send(msg)
NySocket.Close








49
لا ماi>..ا Socket Programming ءl..V less Connection UDP :
لا ماi>..l. لl±.Vا ¤.Ioc ,o. UDP Socket Connection _ﺡا,oIا _o ¤c«o>o.
.Il.Iا _î.Iا .º loآ .هو :



ﻩ.l...و لا .º loآ لl±.Vا ¤.Ioc TCP لا .º ¤.IoaIا أi.. ذإ Client لا و server
لا ءl..|. Socket .I. loآ :
: # C
Socket NySocket = new Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Udp);

VB.NET:
Dim NySocket As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Udp)

لا L.ر ». Socket د«..oIا ماi>..l. .IlﺡIا ,.«..oîIا ×o Bind i.c Lsº مi>.ﺱ.و
.I. loآو لl.s..Vا :
: # C
!PEndPoint sender = new !PEndPoint(!PAddress.Any, 5020);
NySocket.Bind(sender);

VB.NET:
Dim sender As !PEndPoint = New !PEndPoint(!PAddress.Any, 5020)
NySocket.Bind(sender)

د«..oIا مi>.ﺱ. تl.l..Iا لl.s..Vو ReceiveFrom ¤.اi.Iا .º ف,a. ..ﺡ End Point
Reference ¤s.lﺱIا .º ﻩs.,a. ». lo _Ic ءl.. ك eر,o.و reference لا ¤º«s±o ×o
Byte د«..oIا _Iإ ReceiveFrom oو _Iإ ¤º«s±oIا _.«ﺡ. ×.L.ﺱ. ». _ String _o
د«..oIا لV> GetString سVîIا _o± ةد«>«oIا ASC!! .I. loآو :
: # C
int recv;
byte|| data = new byte|102+|;
EndPoint Remote = (EndPoint) (sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv));

VB.NET:
Dim recv As !nteger
Dim data(102+) As Byte
50
Dim Remote As EndPoint = CType((sender), EndPoint)
recv = newsock.ReceiveFrom(data, Remote)
Console.WriteLine(Encoding.ASC!!.GetString(data, 0, recv))

.رVا .º »..و د«..oIا ماi>..ا لl SendTo _o lçI.«ﺡ. ia. تl.l..Iا lçI ر,o. ..ﺡ
String _Iإ Byte Array د«..oIا لV> _o ﻩ.º,ao l..îo. ذإ ¤I.,oIا تl.l..Iا »>ﺡو
Length لا ع«.و Flags _oc i.,. ..ﺡ Broadcast لا ﻩI ر,o. ا,.>او ¤I.,oIا ¤Il.,I
EndPoint Object .I. loآو :
: # C
string welcome = "Hello All";
data = Encoding.ASC!!.GetBytes(welcome);
newsock.SendTo(data, data.Length, SocketFlags.Broadcast, Remote);

VB.NET:
Dim welcome As String = "Hello All"
data = Encoding.ASC!!.GetBytes(welcome)
newsock.SendTo(data, data.Length, SocketFlags.Broadcast, Remote)

.º دا«آVا اiه ×±و _îo. !nfinity While Loop lهi.iﺡ. _îo. وأ .ç... V ..ﺡ.
لl.s..Vاو لl.رVا تl.Ioc _o _.ao دia. ..

l.:l: : Socket Classes Members :

1 - IPAddress Class : ..,a.I مi>.ﺱ.و !P Address _îo. ..ﺡ لا _Iإ eدl..إ
!PEndPoint I ¤olaIا ¤a.±Iاو لl.oآ .I. loآ ﻩ :
: # C
!PAddress newaddress = !PAddress.Parse("192.168.1.1");

VB.NET:
Dim newaddress As !PAddress = !PAddress.Parse("192.168.1.1")

.I. loآ .هو نا«.aIا i.iﺡ. .º تارl.> ¤a.را _.. رl..>Vا _îo.و :
Any ¤î..Iا _Ic حl.o نا«.c يأ _..o.I مi>.ﺱ.و
Broadcast ¤î..Iا _Ic ة,ç>Vا ×.o>I .cاذVا ..Iا _..o.I مi>.ﺱ.و
Loopback _I فو,aoIا نا«.aIا _..o.I مi>.ﺱ.و loopback «هو 12/.0.0.1
None د«>و مic ¤Ilﺡ .º مi>.ﺱ.و Network !nterfase مlL.Iا .º

lçoهأو د«..oIا _o ¤c«o>o »ci. loآ :
Equals II مlc _î.. د«..oIا اiه مi>.ﺱ. _.. ¤.رlso tow Objects l.هو
×>,.و _..ا«.c _.. ¤.رlsoII مi>.ﺱ.. True و _.ç.l..o l.lآ اذإ False l.lآ اذإ
_.sI.>o .
GetHashCode ¤a.ﺹ _Iإ نا«.aIا عl>رV مi>.ﺱ.و Hash Code
HostToNetworkOrder لl. صl>Iا ء,>Iا ×>,.و Network نا«.aIا _o
NetworkToHostOrder ×>,.و لl. صl>Iا ء,>Iا Host نا«.aIا _o

51
2 - IPEndPoint Class : لاو نا«.aIا i.iﺡ.I el.oi>..ا ..ﺡ Port _I Host
.I. loآ ﻩI ¤olaIا ¤a.±Iاو ﻩ. لl±.Vا i.,. يiIاو :
: # C
!PEndPoint end = new !PEndPoint(!PAddress.Parse("192.168.1.1"), 5020);
VB.NET:
Dim end As !PEndPoint = New !PEndPoint(!PAddress.Parse("192.168.1.1"),
5020)

لا .º »ci. ..Iا صا«>Iا ¤c«o>o Socket Class .I. loآ .هو :
AddressFamily لا _Ic ¤º,aoIا _.وl.aIا ¤c«o>o ×>,.و Socket
Available لا _o ةءا,sII ة,هl>Iا تl.l..Iا »>ﺡ ×>,.و Socket
Blocking .La.و Get وأ Set لا نlآ اذإ ¤º,aoI socket لا مi>.ﺱ. Blocking Node V مأ
Connected نlآ اذإ ¤º,aoI ة,.î. ¤.ﺹl>Iا eiه مi>.ﺱ.و لا Socket لا ×o _±.o Remote
Host V مأ
Handle لا ×o _ola.. يiIا _.a..Iا مlL. ¤º,aoI مi>.ﺱ.و Socket
ProtocolType ﺱ. يiIا ل«آ«.و,.Iا ¤º,aoI مi>.ﺱ.و لا .º مi>. Socket
RemoteEndPoint لا _c تlo«Iao ×>,.و Socket لا ×o مi>.ﺱ. يiIا Remote Host

لl.oآو lq:loاﺩ>.aV :
: # C
using System;
using System.Net;
using System.Net.Sockets;
class Socket_ Properties
{
public static void Nain()
{
!PAddress ia = !PAddress.Parse("12/.0.0.1");
!PEndPoint ie = new !PEndPoint(ia, 8000);
Socket fmo = new Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("AddressFamily: {0}",
fmo.AddressFamily);
Console.WriteLine("SocketType: {0}",
fmo.SocketType);
Console.WriteLine("ProtocolType: {0}",
fmo.ProtocolType);
Console.WriteLine("Blocking: {0}", fmo.Blocking);
fmo.Blocking = false;
Console.WriteLine("new Blocking: {0}", fmo.Blocking);
Console.WriteLine("Connected: {0}", fmo.Connected);
fmo.Bind(ie);
!PEndPoint iep = (!PEndPoint)fmo.LocalEndPoint;
Console.WriteLine("Local EndPoint: {0}",
iep.ToString());
52
fmo.Close();
}
}
VB.NET:
imports System
imports System.Net
imports System.Net.Sockets

Public Shared Sub Nain()
Dim ia As !PAddress = !PAddress.Parse("12/.0.0.1")
Dim ie As !PEndPoint = New !PEndPoint(ia, 8000)
Dim fmo As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Console.WriteLine("AddressFamily: {0}", fmo.AddressFamily)
Console.WriteLine("SocketType: {0}", fmo.SocketType)
Console.WriteLine("ProtocolType: {0}", fmo.ProtocolType)
Console.WriteLine("Blocking: {0}", fmo.Blocking)
fmo.Blocking = False
Console.WriteLine("new Blocking: {0}", fmo.Blocking)
Console.WriteLine("Connected: {0}", fmo.Connected)
fmo.Bind(ie)
Dim iep As !PEndPoint = CType(fmo.LocalEndPoint, !PEndPoint)
Console.WriteLine("Local EndPoint: {0}", iep.ToString)
fmo.Close()
End Sub
¤.ìl.ìا تloulﻉoìا g>,.a C.> :
AddressFamily: !nterNetwork
SocketType: Stream
ProtocolType: Tcp
Blocking: True
new Blocking: False
Connected: False
Local EndPoint: 12/.0.0.1:8000
Press any key to continue . . .












53
l.ìl: : UDP Classes Members & TCP :

Vوأ لا Classes لl. ¤ﺹl>Iا Oriented Protocol TCP Connection :



1 - Class TcpClient : ..ﺡ لا _o ¤c«o>o _Ic ي«.ﺡ. Nethods لاو Properties
.I. loآ .هو :

Vوأ : lç. ¤ﺹl>Iا د«..oIا »هأ TCPClient Nethods :

Connect : لا ×o لl±.Vا ¤.Ioc ءا,>V مi>.ﺱ.و server لا نا«.c lç.º ر,o. ..ﺡ !P
لl. صl>Iا Server لا »ºر و Port .I. loآو :
: # C
TcpClient tcp = new TcpClient();
tcp.Connect(IPAddress.Parse("192.168.1.1"),5020);

VB.NET:
Dim tcp As TcpClient = New TcpClient
tcp.Connect(!PAddress.Parse("192.168.1.1"), 5020)

Close : لا ×o لl±.Vا ءlç.V TCP Socket .
EndConnect : ءlç.V Asynchronies Connection ×>,. ..ﺡ Asynchronies Result .
GetStream : لا ةءا,sI مi>.ﺱ.و Stream لا _o Socket و لl.رVا ¤.Ioc .º
لl.s..Vا .

l..l. : ﺹ.l±>Iا »هأ TCPClient Properties :

LingerState : i>l.و get وأ Set لا ¤º,ao وأ i.iﺡ.I Linger Time
NoDelay : i>l.و get وأ Set ¤º,ao وأ i.iﺡ.I V مأ ,.>l.I _.ao .ºو كl.ه نlآ اذإ
ExclusiveAddressUse : i>l.و get وأ Set ¤º,ao وأ i.iﺡ.I لا Socket roﺱ.
لا ماi>..l. Client Port V مأ .
SendBufferSize و ReceiveBufferSize : i>l.و get وأ Set ¤º,ao وأ i.iﺡ.I »>ﺡ
لا Buffer لا .º مi>.ﺱoIا stream .º ف,aoIاو TCP Client Object .
SendTimeout و ReceiveTimeout : i>l.و get وأ Set ¤º,ao وأ i.iﺡ.I .º«Iا
.La. ..ﺡ لl.s..Vا وأ لl.رVا ¤.IoaI حl.oIا Time Out i>. »I ﻩ.أ ¤Ilﺡ .º
¤..ao ¤..oز ة,.º لV> ,>Vا ف,LIا .
54
2 - Class TcpListener : ..ﺡ لا _o ¤c«o>o _Ic ي«.ﺡ. Nethods لاو Properties
.I. loآ .هو :


Vوأ : «..oIا »هأ lç. ¤ﺹl>Iا د Nethods TcpListener :

AcceptSocket : لا ×o لl±.Vا ¤.Ioc ل«.sI مi>.ﺱ.و Client .
Start : .هو Overloaded Nethod دic i.iﺡ. ».. lç.Iإ »ºر ,.,o. ¤Ilﺡ .º ﻩ.ا ..ﺡ
لا وأ ر«.lLIا .º lهد«>«. roﺱ. ..Iا ة,ç>Vا Oouee r.±. _.ao »ºر i.iﺡ. نوi.و
لا Oouee ,.c دiﺡo .
Stop : لا .º lça±و _±s.و ..±.Iا ¤.Ioc قVcV مi>.ﺱ.و Finally ماi>..ا i.c
لا Try لا و Catch يأ ثوiﺡ ¤Ilﺡ .º ..±.Iا ¤.Ioc ءlç.إ ».. _.ﺡ Exception .

l..l. : .º ﺹ.l±>Iا »هأ TcpListener :

LocalEndpoint : لا ×>,. ..ﺡ !P لا »ºرو Port لا .º مi>.ﺱoIا LocalEndpoint
دiﺡoIا .
Server : لا .º د«..oIا و ﺹ.l±>Iا _آ _Iإ ل«ﺹ«Iا ×.L.ﺱ. lçIV> _oو TCP Server
لا _.o ls.l. lهl.ﺡ,. ..Iاو Accept لاو Sendto لاو Receive و Listen lه,.cو

l..l. لا Classes لl. ¤ﺹl>Iا Protocol UDP Connectionless :

1 - Class UdpClient : ..,a.I مi>.ﺱ.و UDP Datagram Protocol Connection l.oº
lçao _ola.Iاو lçs.,a.. ls.l. اiه .ºو ء,>Iا .I. loآ .هو lç.l.«.ﺡo »هأ _.... :

55


lç. ¤ﺹl>Iا ﺹ.l±>Iاو د«..oIا »هأ _oو :

JoinMulticastGroup و DropMulticastGroup : »±I ءlaIإ وأ ¤c«o>o وأ نا«.c
_.وl.aIا _o لا _o Nulticast Group .
EnableBroadcast : i>l.و Get وأ Set لا _.as.I Broadcasting لا .º socket .
MulticastLoopback : i>l.و Get وأ Set لا i.iﺡ. وأ ¤º,aoI Nlticast Loopback .


2 - Class MulticastOption : لا .º مi>.ﺱ.و Nulticasting ».. ..ﺡ _.,>. !P
Address List .º lçao _ola.I Nulticast Group _oaI Join و Drop يV Nulticast
Group l.s..V ¤.«±c ¤ºl±V لl.oآ .I. loآ مi>.ﺱ.و ل _.l.ر Nulticast :

Vوأ لا ف,a. UDP Socket .I. loآو :
: # C
mcastSocket = new Socket(AddressFamily.!nterNetwork,SocketType.Dgram,
ProtocolType.Udp);

VB.NET:
mcastSocket = New Socket(AddressFamily.!nterNetwork, SocketType.Dgram,
ProtocolType.Udp)

l.:l: ..,a.. م«s. Address List لا lç.Iإ i.ﺱ. ». !P لا .º ﻩIl>دإ i.,. يiIا Group
لا _a>. وأ User د«..oIا ماi>..l. ت«îﺱIl. lçL.,. ﻩﺱs.. نا«.aIا _>i. Bind loآو
.I. :
: # C
!PAddress local!PAddr = !PAddress.Parse(Console.ReadLine());
mcastSocket.Bind(!Plocal);

VB.NET:
Dim local!PAddr As !PAddress = !PAddress.Parse(Console.ReadLine)
mcastSocket.Bind(!Plocal)



56
l.ìl: لا ..,a.. م«s. Nulticast Option loآ دiﺡoIا نا«.aIا lçI i.ﺱ.و .I. :
: # C
NulticastOption mcastOption;
mcastOption = new NulticastOption(local!PAddr);

VB.NET:
Dim mcastOption As NulticastOption
mcastOption = New NulticastOption(local!PAddr)

ا,..oورl. ¤.V. د«..oIا eiه i>l. ..ﺡ لا _Ic ,.a.Iا ..±. ». _oو ت i.iﺡ.I لوVا
_Ic ,..a.Iا ى«.ﺱo !P _Ic وأ !Pv6 _Ic وأ Socket وأ TCP وأ UDP eiه l..Ilﺡ .ºو
_Ic ,.a.Iا مi>.ﺱ. ف«. !P »± «ه ei.,. lo ذإ !P _Iإ Nulticast Group .ºو
¤ºl±إ _.. رl..>Vا _îo.و ¤.«±c ¤ºl±إ i.,. ..ﺡ ,..a.Iا ع«. دiﺡ. ..l.Iا ,..oورl.Iا
ﻩ.«±c AddNembership aIإ وأ ¤.«±c ءl DropNembership لا ﻩ.Iإ i.ﺱ. ا,.>أو
NulticastOption Object .I. loآ و ﻩ.l..|. l.oº يiIاو :
C#:
mcastSocket.SetSocketOption(SocketOptionLevel.!P,
SocketOptionName.AddNembership,mcastOption);

VB.NET:
mcastSocket.SetSocketOption(SocketOptionLevel.!P,
SocketOptionName.AddNembership, mcastOption)























57
3.2 : Programming Asynchronous Sockets :

اiه .º ثiﺡ.. ف«. ء,>Iا لا ماi>..ا _c Asynchronous Socket ,.آأ _î..
ماi>..ا _Ic ¤.IoaIا ¤I.oVا _o ¤c«o>o _.L. ف«.و ls.l. ﻩ. l..iﺡ. loc V.±s.
.Vا تlî..Iا تl.>o,. .º _oا,.o ,.aIا لl± ...
.º _oaIا _Iإ جl.ﺡ. ..Iا تl.>o,.Iا .º اi> »ço _oا,.oIا لl±.Vا نأ فو,aoIا _o
¤.IoaIا ءlç..ا ia. _Iإ ,>l ,oV لl±.Vا ماi>..l. roﺱ. V ..ﺡ .s.sﺡIا _o,Iا
oاi>..او ¤.رl>Iا ﻩ V ر«oVا eiه _.o .IL.. ..Iا تl.IoaIا .º اi> »ço r±.. V _آ
مi>.ﺱ. تl.l..II ¤I.s.ﺱoIا ¤ç>Iا ..lآ اذإ ¤Ilﺡ .º ﻩoاi>..ا اi.أ Slow
Connection لا _Ic ¤î..Iا دlo.clآ Dialup وأ _.s.ﺱoIا ×o _.,oIا _.زlç>Iا L.,I
لا مi>.ﺱ. _.oi>.ﺱoIا _o ة,..آ ¤c«o>o :Il.ه نlآ اذإ ¤Ilﺡ .º Server ..ﺡ
oIا ¤.s. _oا,.oIا ب«I.Vا ×.o. .º لl.رVا ¤.Ioc ءا,>إ _o ¤î..Iا _Ic _.oi>.ﺱ
لا ن«آ ¤Ilﺡ Server ,>l زlç> _o تl.l.. _.s.ﺱ. ، ماi>..l. r±.. ¤IlﺡIا eiه .ºو
ةia. مl.sIا rol.,.Iا _o l.درأ اذإ ¤Ilﺡ .º اi> »ço ,..a. ذإ _oا,.oIا ,.aIا لl±.Vا
لا ﺱs. _Icو مlço Thread اi>..l.و م لا ﺱs. Connection ، ls.l. l.,آذ loآ وأ
ﺱs. ن«oi>.ﺱ. _.oi>.ﺱo دic i>«. ﻩ.ا وأ l..ﺱ. ء.L. لl±.Vا نlآ اذإ ¤Ilﺡ .º
لا Server ..


Vوأ Asynchronous Socket Class and its members :

لا _o ¤c«o>o. _oا,.oIا ,.c لl±.Vا ... توiIا »ci. methods لا _o± ةد«>«oIا
Socket Class .. ..Iاو لا _o lç.lci..ا » System.Net.Socket Namespaces iºو
لا د«>«. سد«..oIا eiه ... توiIا ت,.o Begin د«..oIا ».أ ¤.اi. .º ، _îIو Begin
Nethod i>«. Nethod End عl>رV مi>.ﺱ. ..Iاو lçI ¤I.lso callback result i.c
لا ءlç..ا Begin Nethod .I. loآ .هو i.s..Iا _o :










58
1 - BeginAccept لا ل«.sI مi>.ﺱ. و Client Request لا _Iإ eدl..إو Object
AsyncCallback لا _îo.. ف«. ¤s.,LIا eiه ماi>..l.و Server دic لl.s..ا _o
لا _o Clients Requests ¤.IoaIا _o ءlç..Vا رlL..V ¤>lﺡIا نوi.و .º«Iا ﺱs. .º
ا ءlci..ا ة,o _آ .º ».. ..ﺡ ¤.رl>Iا لا ماi>..l. د«..oI AsyncCallback Delegate
.I. loآ .I. loآ مi>.ﺱ.و :
: # C
m_mainSocket = new Socket(AddressFamily.!nterNetwork,
SocketType.Stream,ProtocolType.Tcp);
!PEndPoint ipLocal = new !PEndPoint (!PAddress.Any, 5020);

m_mainSocket.Bind (ipLocal);
m_mainSocket.Listen (10);
m_mainSocket.BeginAccept (new AsyncCallback (Client_request_method),
null);

VB.NET:
m_mainSocket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Dim ipLocal As !PEndPoint = New !PEndPoint(!PAddress.Any, 5020)
m_mainSocket.Bind(ipLocal)
m_mainSocket.Listen(10)
m_mainSocket.BeginAccept(New AsyncCallback(Client_request_method),
Nothing)

لا ¤ºl±إ »... ..ﺡ Client Request .º Reference Callback _.lﺱIا _c _±s.o
ءl..إ _o i.V l.هو method لا لl.s..V Client Request لا ءlç.إو Client Accepted
Object د«..oIا ماi>..l. EndAccept :
C#:
public void Client_request_method(!AsyncResult ar)
{
Socket listener = (Socket)ar.AsyncState;
Nyclient = listener.EndAccept(ar);
Nyclient.Send(/* data to be send*/ );
listener.BeginAccept(new AsyncCallback(Client_request_method), listener);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString()); }

VB.NET:
Dim listener As Socket = CType(ar.AsyncState, Socket)
Nyclient = listener.EndAccept(ar)
Nyclient.Send
listener.BeginAccept(New AsyncCallback(Client_request_method), listener)
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString)

.º 2005 Dot Net لا .ﺡ.ﺹأ BeginAccept Nethod .I. loآ لlî.أ ةic i>l. :

... توiIا .º لوVا _î.Iا 2003 و 2005 i>l.و AsyncCallBack Delegate و State
Object لا ¤Ilﺡ _c تlo«Iao عl>رV Request لا .º Socket .I. loآو :
NySocket.BeginAccept(AsyncCallback , object state)

59

... توiIا .º ..l.Iا _î.Iا 2005 ¤oI.ﺱoIا تl.l..Iا »>ﺡ i.iﺡ. ﻩ.º :.îo. ..ﺡ
NySocket.BeginAccept(int Data_ Receive_Size , AsyncCallback , object state)


... توiIا .º .Il.Iا _î.Iا 2005 لا i.iﺡ. ﻩ.º _îo. ..ﺡ Accepted Socket
NySocket.BeginAccept(Socket accept_Socket ,int Data_ Receive_Size ,
AsyncCallback , object state)

2 - BeginConnect أi.I مi>.ﺱ.و Asynchronous Connection لا _Ic Socket
لا »ºرو Port دiﺡoIا لا lçI i.ﺱ. ..ﺡ !PEndPoint لاو Asynchronous Callback لاو
State Object I. loآو . :
NySocket.BeginConnect(EndPoint !P,Syncallback Result,object state)

لl.oآ .I. loآ مi>.ﺱ.و :
: # C
Socket NySocket = new Socket (AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp);
!PEndPoint ipend = new !PEndPoint(!PAddress.Parse("192.168.1.101"),
5020);

NySocket.BeginConnect(ipend, new AsyncCallback(Connected), NySocket);

VB.NET:
Dim NySocket As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Dim ipend As !PEndPoint = New
!PEndPoint(!PAddress.Parse("192.168.1.101"), 5020)

لا .º Connected Nethod لا i.iﺡ. ».. CallBack Socket .I. loآ :
: # C
public static void Connected(!AsyncResult iar)
{
Socket sock = (Socket)iar.AsyncState;
try
{
sock.EndConnect(iar);
}
catch (SocketException)
{
Console.WriteLine("Unable to connect to host");
}}

VB.NET:
Public Shared Sub Connected(Byval iar As !AsyncResult)
Dim sock As Socket = CType(iar.AsyncState, Socket)
Try
sock.EndConnect(iar)
Catch generatedExceptionvariable0 As SocketException
Console.WriteLine("Unable to connect to host")
End Try
60
End Sub

3 - BeginReceive لا _o تl.l.. لl.s..V مi>.ﺱ.و Client .º lç..,>.و Byte
Array .I. loآ lçI ¤olaIا ¤a.±Iاو :

NySocket.BeginReceive(Byte|| buffer,int offset, SocketFlags,AsyncCallback,
object sate)

لl.oآ .I. loآ مi>.ﺱ.و :
: # C
byte|| data = new byte|102+|;
NySocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new
AsyncCallback(ReceivedData), NySocket);

void ReceivedData(!AsyncResult iar)
{
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceive(iar);
string receivedData = Encoding.ASC!!.GetString(data, 0, recv);
Console.WriteLine(receivedData);
}

VB.NET:
Dim data(102+) As Byte
NySocket.BeginReceive(data, 0, data.Length, SocketFlags.None, New
AsyncCallback(ReceivedData), NySocket)

Sub ReceivedData(Byval iar As !AsyncResult)
Dim remote As Socket = CType(iar.AsyncState, Socket)
Dim recv As !nteger = remote.EndReceive(iar)
Dim receivedData As String = Encoding.ASC!!.GetString(data, 0, recv)
Console.WriteLine(receivedData)
End Sub

د«..oIا مi>.ﺱ. loآ BeginReceiveFrom دiﺡo ׺«o _o تl.l..Iا لl.s..V ماi>..l.
لا UDP _Iإ فl±. ..ﺡ ..آ,.Iا _.lﺱIا !PEndPoint Refrance Object .

4 - BeginSend لا ,.c _.s.ﺱoIا ف,LIا _Iإ تl.l.. لl.رV مi>.ﺱ.و
Asynchronous Socket .I. loآ lçI ¤olaIا ¤a.±Iاو :

NySocket.BeginSend (Byte|| buffer,int offset, SocketFlags,AsyncCallback,
object sate)

لl.oآ .I. loآ مi>.ﺱ.و :
: # C
private static void SendData(!AsyncResult iar)
{
Socket server = (Socket)iar.AsyncState;
int sent = server.EndSend(iar);
}

61

byte|| data = Encoding.ASC!!.GetBytes("Hello Word");
NySocket.BeginSend(data, 0, data.Length, SocketFlags.None,
new AsyncCallback(SendData), NySocket);

VB.NET:
Private Shared Sub SendData(Byval iar As !AsyncResult)
Dim server As Socket = CType(iar.AsyncState, Socket)
Dim sent As !nteger = server.EndSend(iar)
End Sub
Dim data As Byte() = Encoding.ASC!!.GetBytes("Hello Word")
NySocket.BeginSend(data, 0, data.Length, SocketFlags.None, AddressOf
SendData, NySocket)

د«..oIا مi>.ﺱ. loآ BeginSendto لl.رV تl.l..Iا _Iإ Remote Host دiﺡo
لا ماi>..l. UDP _Iإ فl±. ..ﺡ ..آ,.Iا _.lﺱIا !PEndPoint Refrance Object .


5 - u.: توﺩìا L8 ةﺩ:ﺩ>ìا دu..oìا _o ¤cuo>o ¤8læإ o: loآ 2005 Lهو :
BegonDiconnect و لl±.Vا ءlç.V BeginSendFile V لا و .Io لl.ر
BeginReceiveMessageFrom تl.l..Iا _o دiﺡo دic لl.s..V مi>.ﺱ. ..Iاو
لا .º دiﺡo نlîo .º lç..,>.و Bufer ..

.Il.Iا ..آ,.Iا : BeginSendFile لا i>l.
NySocket.BeginSendFile(string filename,AsyncCallback Asyn,object state)


لاو BeginReceiveMessageFrom .Il.Iا ..آ,.Iا :
NySocket.BeginReceiveNessageFrom(byte Buffer ,int offset,int
size,SocketFlags sf,ref EndPoint,AsyncCallback ascb,object state)


لاو BegonDiconnect .Il.Iا ..آ,.Iا :
NySocket.BeginDisconnect(bool reuseSocket,AsyncCallback ascb,object state)

l.:l: : لا تla..b: Asynchronous Socket u.: توﺩìا L8 :

لا ءl..|. أi.. _ﺡا,oIا _o ¤c«o>o. _oا,.o ,.aIا لl±.Vا ¤.Ioc ,o. Object Socket
لا .º Server Side لا ..,a. ».. :Iذ ia. BeginConnect أi.I Asynchronous
Connection لا _Ic Socket دl..إ ».. ..ﺡ Object !PEndPoint لاو Nethod
Asynchronous Callback لاو State Object لl±.Vا ¤.Ioc ¤IlﺡIا eiه .º أi..و lçI
لl. Socket ، لا _Iإ ر,o. :Iذ ia.و BeginAccept لا ل«.sI Client Request ».. ..ﺡ
_.,.و .ILIا ل«.º Acknowledgement لا _Iإ Client ¤ﺱI>Iا ل«.s. lç.º ﻩoIa.I
Iا ¤..lîoإو لا ×.L.ﺱ. و لl.رVI ءi. Client l.رVl. ءi.Iا ¤ﺱI>Iا _Ic ¤sºا«oIا ia. ل
د«..oIا ماi>..l. BeginSend لا _.s.ﺱ.و Server لا _o ¤Il.,Iا Client ماi>..l.
د«..oIا BeginReceive ¤.Ioc _îI ن|º ls.l. l.,آذ loآو Begin د«..oIا lçI.ls. End
اia..VI د . _Ic ى,>أ ¤.Ioc ءا,>V لا ﺱs Thread لl±.Vا ,.o lo «هو rol.,.Iا .º
_oا,.oIا لl±.Vا _c _oا,.o ,.aIا .


62


rol.,. ءl..|. نVا م«s. ف«. ¤s.lﺱIا ».هlsoIا _Ic ءl..و Client/Server Chatting
لا _Ic io.a. Asynchronous Socket تl.l..Iا لl.s..او لl.رV .

oآ i.i> عو,.o ءl..|. »º ءi.IIو .Il.Iا _î.Iا .º l :



لا مﺩ>.a: فua Namespaces ¤.ìl.ìا :
: # C
using System.Net;
using System.Net.Sockets;
using System.Text;

VB.NET:
!mports System.Net
!mports System.Net.Sockets
!mports System.Text





63
لا L8 Global Declaration } لا m:,ﻉ: ﺩﻉ: يأ Main Class ( ¤8læ|: o8
¤.ìl.ìا m:رlﻉ.ìا :
: # C
public class Form1 : System.Windows.Forms.Form
{
Socket server = new Socket(AddressFamily.!nterNetwork,SocketType.Stream,
ProtocolType.Tcp);
!PEndPoint iep = new !PEndPoint(!PAddress.Any, 5020);
private byte|| data = new byte|102+|;
private int size = 102+;

VB.NET:
Public Class Form1 !nherits System.Windows.Forms.Form

Private server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Private iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 5020)
Private data As Byte() = New Byte(102+) {}
Private size As !nteger = 102+

لا L8 Form Load ف,ﻉ.a C.> Lìl.ìا دuSìا ¤8læ|: o8 Connection
لا _lc ﺩo.ﻉ: TCP لا _lc _oﻉ:و Port 5020 لlæ:Vا لu.8 ¤.loc m:,ﻉ: o:
لا ماﺩ>.al: BeginAccept :
: # C
private void Form1_Load(object sender, System.EventArgs e)
{
server = new Socket(AddressFamily.!nterNetwork,SocketType.Stream,
ProtocolType.Tcp);
!PEndPoint iep = new !PEndPoint(!PAddress.Any, 5020);
server.Bind(iep);
server.Listen(5);
server.BeginAccept(new AsyncCallback(AcceptConn), server);
}

VB.NET:
Private Sub Form1_Load(Byval sender As Object, Byval e As
System.EventArgs)
server = New Socket(AddressFamily.!nterNetwork, SocketType.Stream,
ProtocolType.Tcp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 5020)
server.Bind(iep)
server.Listen(5)
server.BeginAccept(New AsyncCallback(AcceptConn), server)
End Sub

ءla:إ o: Accept Callback Method لا ءlq:إ ﻩ.8 o..a يíìاو Accepted
Request لا ماﺩ>.al: EndAccept Method لlaرإ Oìذ ﺩﻉ:و
Acknowledgement لا _ìإ Client _a,:و ulbìا لu.a: lq.8 o,.>:
لا ماﺩ>.al: BeginSend Method Ll: loآ :


64
: # C
void AcceptConn(!AsyncResult iar)
{
Socket oldserver = (Socket)iar.AsyncState;
Socket client = oldserver.EndAccept(iar);
conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
string stringData = "Welcome to my server";
byte|| message1 = Encoding.ASC!!.GetBytes(stringData);
client.BeginSend(message1, 0, message1.Length, SocketFlags.None,new
AsyncCallback(SendData), client);
}

VB.NET:
Sub AcceptConn(Byval iar As !AsyncResult)
Dim oldserver As Socket = CType(iar.AsyncState, Socket)
Dim client As Socket = oldserver.EndAccept(iar)
conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString
Dim stringData As String = "Welcome to my server"
Dim message1 As Byte() = Encoding.ASC!!.GetBytes(stringData)
client.BeginSend(message1, 0, message1.Length, SocketFlags.None, New
AsyncCallback(SendData), client)
End Sub


ءla:إ o: Send Callback method لا ءlq:V BeginSend Ll: loآو :
: # C
void SendData(!AsyncResult iar)
{
Socket client = (Socket)iar.AsyncState;
int sent = client.EndSend(iar);
client.BeginReceive(data, 0, size, SocketFlags.None,new
AsyncCallback(ReceiveData), client);
}

VB.NET:
Sub SendData(Byval iar As !AsyncResult)
Dim client As Socket = CType(iar.AsyncState, Socket)
Dim sent As !nteger = client.EndSend(iar)
client.BeginReceive(data, 0, size, SocketFlags.None, New
AsyncCallback(ReceiveData), client)
End Sub

ءla:إ o: Receive Callback method لا ءlq:V BeginReceive Ll: loآو :

: # C
void ReceiveData(!AsyncResult iar)
{
Socket client = (Socket)iar.AsyncState;
int recv = client.EndReceive(iar);
if (recv == 0)
{
65
client.Close();
conStatus.Text = "Waiting for client...";
server.BeginAccept(new AsyncCallback(AcceptConn), server);
return;
}
string receivedData = Encoding.ASC!!.GetString(data, 0, recv);
results.!tems.Add(receivedData);
byte|| message2 = Encoding.ASC!!.GetBytes(receivedData);
client.BeginSend(message2, 0, message2.Length, SocketFlags.None,new
AsyncCallback(SendData), client);
}

VB.NET:
Sub ReceiveData(Byval iar As !AsyncResult)
Dim client As Socket = CType(iar.AsyncState, Socket)
Dim recv As !nteger = client.EndReceive(iar)
!f recv = 0 Then
client.Close()
conStatus.Text = "Waiting for client..."
server.BeginAccept(New AsyncCallback(AcceptConn), server)
Return
End !f
Dim receivedData As String = Encoding.ASC!!.GetString(data, 0, recv)
results.!tems.Add(receivedData)
Dim message2 As Byte() = Encoding.ASC!!.GetBytes(receivedData)
client.BeginSend(message2, 0, message2.Length, SocketFlags.None, New
AsyncCallback(SendData), client)
End Sub


لا ¿ol:,: _o ءlq.:Vا o: ﺩ8 l.هو Server لا ¿ol:,: ءla:|: مua: فua نVاو
Client Lìl.ìا _Saìا L8 loآ ﺩ:ﺩ> عو,ao ءla:|: o8 ءﺩ.lìو :





66
لا مﺩ>.a: فua Namespaces ¤.ìl.ìا :
: # C
using System.Net;
using System.Net.Sockets;
using System.Text;

VB.NET:
imports System.Net
imports System.Net.Sockets
imports System.Text

لا L8 Global Declaration } لا m:,ﻉ: ﺩﻉ: يأ Main Class ( ¤8læ|: o8
¤.ìl.ìا m:رlﻉ.ìا :
: # C
public class Form1 : System.Windows.Forms.Form
{
private Socket client;
private byte|| data = new byte|102+|;
private int size = 102+;

لا L8 Connect Button Lìl.ìا دuSìا ¤:l.S: o8 :
: # C
conStatus.Text = "Connecting...";
Socket newsock = new Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse(textBox1.Text), 5020);
newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);

VB.NET:
Private client As Socket
Private data As Byte() = New Byte(102+) {}
Private size As !nteger = 102+


conStatus.Text = "Connecting..."
Dim newsock As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Parse(textBox1.Text),
5020)
newsock.BeginConnect(iep, New AsyncCallback(Connected), newsock)

ءla:|: o8 o: Callback Connect method Ll: loآ :
: # C
void Connected(!AsyncResult iar)
{
client = (Socket)iar.AsyncState;
try
{
client.EndConnect(iar);
conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
67
client.BeginReceive(data, 0, size, SocketFlags.None, new
AsyncCallback(ReceiveData), client);
}
catch (SocketException)
{
conStatus.Text = "Error connecting";
}
}


VB.NET:
Sub Connected(Byval iar As !AsyncResult)
client = CType(iar.AsyncState, Socket)
Try
client.EndConnect(iar)
conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString
client.BeginReceive(data, 0, size, SocketFlags.None, New
AsyncCallback(ReceiveData), client)
Catch generatedExceptionvariable0 As SocketException
conStatus.Text = "Error connecting"
End Try
End Sub

ءla:إ o: Receive Callback method لا ءlq:V BeginReceive Ll: loآو :

: # C
void ReceiveData(!AsyncResult iar)
{
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceive(iar);
string stringData = Encoding.ASC!!.GetString(data, 0, recv);
results.!tems.Add(stringData);
}


VB.NET:
Sub ReceiveData(Byval iar As !AsyncResult)
Dim remote As Socket = CType(iar.AsyncState, Socket)
Dim recv As !nteger = remote.EndReceive(iar)
Dim stringData As String = Encoding.ASC!!.GetString(data, 0, recv)
results.!tems.Add(stringData)
End Sub









68
لا L8 Lìl.ìا دuSìا ¤8læإ o: Send Button :
: # C
try
{
byte|| message = Encoding.ASC!!.GetBytes(newText.Text);
newText.Clear();
client.BeginSend(message, 0, message.Length, SocketFlags.None,new
AsyncCallback(SendData), client);
newText.Focus();
}
catch(Exception ex){NessageBox.Show(ex.Nessage);}

VB.NET:
Try
Dim message As Byte() = Encoding.ASC!!.GetBytes(newText.Text)
newText.Clear
client.BeginSend(message, 0, message.Length, SocketFlags.None, New
AsyncCallback(SendData), client)
newText.Focus
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try

ءla:إ o: Send Callback method لا ءlq:V BeginSend Ll: loآو :

: # C
void SendData(!AsyncResult iar)
{
try
{
Socket remote = (Socket)iar.AsyncState;
int sent = remote.EndSend(iar);
remote.BeginReceive(data, 0, size, SocketFlags.None, new
AsyncCallback(ReceiveData), remote);
}
catch(Exception ex){NessageBox.Show(ex.Nessage);}

}

VB.NET:
Sub SendData(Byval iar As !AsyncResult)
Try
Dim remote As Socket = CType(iar.AsyncState, Socket)
Dim sent As !nteger = remote.EndSend(iar)
remote.BeginReceive(data, 0, size, SocketFlags.None, New
AsyncCallback(ReceiveData), remote)
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try
End Sub

69

ءla:إ o: Receive Callback method لا ءlq:V BeginReceive Ll: loآو :

: # C
void ReceiveData(!AsyncResult iar)
{
try
{
Socket remote = (Socket)iar.AsyncState;
int recv = remote.EndReceive(iar);
string stringData = Encoding.ASC!!.GetString(data, 0, recv);
results.!tems.Add(stringData);
}
catch(Exception ex){NessageBox.Show(ex.Nessage);}
}

VB.NET:
Sub ReceiveData(Byval iar As !AsyncResult)
Try
Dim remote As Socket = CType(iar.AsyncState, Socket)
Dim recv As !nteger = remote.EndReceive(iar)
Dim stringData As String = Encoding.ASC!!.GetString(data, 0, recv)
results.!tems.Add(stringData)
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try
End Sub

لا ¿ol:,: ن|8 l.b>V loآو Client لا ¿ol:,: _c ا,..آ ml.>: V Server C.>
لا L8 ف,ﻉ: Server لا Socket Connection لاو BeginAccept Method loأ
لا L8 Client لا ف,ﻉ.8 Socket Connection لا و BeginConnect Method
L8 lqaa: Lه لl.a.aVاو لlaرVا ¤.loc _a.:و Server او ل Client ...




















70
Dot Net Networks 8 TCP/!P Programming
Chapter 4
Advanced Multicasting Systems


Advanced Nulticasting Systems

A. Architecture of Nulticast Sockets
B. Using Nulticast Sockets with .NET
C. Nulticast Conferencing Systems:
1. Full/Half Duplex Nulticast video
Conferencing System.
2. Full/Half Duplex Nulticast Desktop
Conferencing System.
3. Full/Half Duplex Nulticast Text
Conferencing System















71
o.>,ìا _o>,ìا ¤ا oa:

4.1 : Advanced Multicasting Systems :

لا ..,a.. ls.l. l.oº Nulticasting لا _..و lç... ق,sIا l...و Broadcasting l...و
eiه .ºو ... توiIا .º lçao _ola.Iا ¤.s.آو lçcا«.أ ء,>Iا _î.. lç.c ثiﺡ.. ف«.
لا تl.>o,. ¤ﺹl>و تlî..Iا تl.>o,. .º ة,..îIا lç..oهV :Iذو V.±s. ,.آأ
Conferencing ...

Vوأ : Architecture of Multicast Sockets :

لا ×o _ola.Iا ».. ﻩ.ا فو,aoIا _o Nulticasting لا ل«آ«.,. ,.c UDP لا ماi>..l.و
Class D Subnet Nask لا ل«آ«.,. ماi>..l. تlc«o>oIا ةرادإ ¤.Ioc »..و !GNP –
!nternet Group Nanagement Protocol لا _o ء,> «ه يiIاو !nternet Protocol
Nodel .Iا _î.Iا _o r±.. loآو لا ل«آ«.,. ن|º .Il !GNP تl.Ioc _Ic ي«.ﺡ.
تl.l..II ».IﺱIا ل«ﺹ«Iا _o _sﺡ.Iا ) ¤Il.,I .IîIا تl.l..Iا »>ﺡ لl.رإ ».. ..ﺡ
,sﺹ »º,Iا ×±«. lç.laIإ _îo. ذإ ¤.رl..>ا .هو ( ، لا _Ic l±.أ ي«.ﺡ. و TTL Time to
Live ¤Il.ر _îI .±ا,.ºVا ,oaIا ﻩ.º دiﺡ. يiIاو ، ا ¤.IoaIا ع«.و ¤.رادV ) _Iإ »±
¤c«o>o ، ¤c«o>o _o ءlaIإ ، ¤c«o>oIا _c تlo«Iao عl>رإ وأ Nembership Ouery (
لا _o± l.>o,. lهi.iﺡ. ».. ..Iا ¤c«o>oIا نا«.c ا,.>أو Range _I دiﺡoIا Class D
.



لا ﺹ.±>. ».و Range لا .º Nulticasting _o 22+.0.0.0 _Iإ 239.255.255.255
×.L.ﺱ.و ق,L ¤.V.. ei.iﺡ. يوi. _î.. lo|º Static وأ Dynamic وأ لا سl.أ _Ic
Scope-Relative لl.oآ .I. loآ تla.ز«.Iا eiه مi>.ﺱ. مlc _î..و :
ﺹ.±>.Iا 22+.0.0.1 e,.,o. ».. V ..ﺡ Lsº ¤.IﺡoIا تlî..Iا ×.o> .º مi>.ﺱ.و
لا ,.c ى,>أ ¤î.. _Iإ Router . _Iإ ,.,o.Iا l.درأ اذإ loأ لا ,.c ى,>أ تlî. Router
ﺹ.±>.Iا مi>.ﺱ.º 22+.0.0.2 و لا ﺱs. ماi>..ا ط,.. _آV Subnet .º
ى,>Vا تlî..Iا ... _I تl±.±>.Iا ×.o> ¤º,aoIو Nulticasting .Il.Iا L.ا,Iا ,L.ا :

addresses - multicast / ssignments a / org . iana . www :// http

لا _s. ».. Nulticast Packets لا _.. Backbone Tunnels لا ماi>..l. Unicast
Tunnel لا _Iإ ¤î..Iا _>اد _o lçIl.رإ ».. ..ﺡ Router _o _.,. و Router _Iإ
لا ,.c ,>أ Backbone Tunnel اi>..l. م لا ب«I.أ Unicast لا _o ,..îIا ,º«. lo «هو
Bandwidth لا _Iإ ةiﺡاو ¤>ﺱ. _.,. ..ﺡ ¤î..Iا .º Router lça.ز«.. «ه م«s.و
لا ماi>..l. ة,ç>Vا _Ic Unicast لا .º ةi.ﺡ«Iا ¤Iî.oIا Nulticast io.a. ﻩ.ا «ه
لا ماi>..ا _Ic _olآ _î.. UDP Connectionless Protocol .

لا ماi>..ا l..îo.و Nulticasting ا _o عا«.أ ¤.V. .º لا تlî.. .هو تlî..I Peer
to Peer زlç>I د«>و V ..ﺡ Server لا _Iإ و _o _.,. و _.s.ﺱ. _îIاو Group يiIا
72
ﻩ.º «ه ، ..l.Iا ع«.Iاو Server Based Network لا _Iإ ةiﺡاو ¤Il.ر لl.رإ ».. ..ﺡ
Server لا م«s.و Server ¤î..Iا .º ة,ç>Vا ¤.s. _Ic lça.ز«.. ، .Il.Iا ع«.Iا loأ
º لا لV> _o ».. Router ، ia. ».. لl.رVا ¤.Ioc ن|º .Il.Iا _î.Iا _o r±.. loآو
لا مlo±.ا Client لا :Io. ..Iا ¤c«o>oIا _Iإ !P Nulticast لا _.,.و Client ¤Il.ر
لا _Iإ ةiﺡاو Router لا م«s. ..ﺡ Router ¤c«o>oIا .º ة,ç>Vا _Ic lça.ز«..
oi>.ﺱo l لا Routing Table .




لا ماi>..l. لl.رVا .º لlﺡIا نlآ loآو Broadcasting .º لl.رVا »..
Nulticasting لا .º loآ _îIا _Iإ ﺱ.Iو ¤..ao ¤c«o>o _Iإ دiﺡo زlç> _o
Broadcast ، ة,ç>Vا _o ¤c«o>o _آ ن«îُ. ..ﺡ Group loآ ﺹ.±>.Iا »..و صl>
لا _ºو ls.l. l.,آذ !P Nulticasting o>o _آ :I.o. ..ﺡ لا ﺱs. ¤c« !P Nulticast
_I لlî.أ ةic i>«.و Nulticasting ¤c«o>o _Iإ لl.رVا lç.Ic ¤I.oVا _oو one to
Group ¤c«o>o _o ,.آأ _Iإ لl.رVا و one to Nulti Group :

1 – لl.رVا ¤c«o>o to Group One :
لا :Io. ﻩ.ºو User Sender لا ﺱs. !P Nulticasting لا ﻩîIo. يiIا Receiver Users
لا _>اد _o لl.رVا »..و Group ك _.,. ..ﺡ ﻩ.l±cأ ×.o> _Iإ Unicast لا _Iإ
Access Point لا ب«I.l. ¤c«o>oIا .º ءl±cVا ¤ºlآ _Ic lça.ز«.. م«s. ..ﺡ
Broadcast .Il.Iا _î.Iا .º loآو :

73




2 - لl.رVا ¤c«o>o _o ,.آأ _Iإ s Group - to Nulti One :

. iº ﻩ.ºو لا ن«î !P Nulticasting _I User Sender _c .I.>o Receiver Users »..و
_o لl.رVا User لا _>اد Group تlc«o>o _Iإو lç.o «±c «ه يiIا ¤c«o>oIا _Iإ
ى,>أ ، ماi>..l. lهi.iﺡ. »..و Address List lçI لl.رVا i.,. ..Iا تlc«o>oII ...




74
l.:l: : NET . th Using Multicast Sockets wi :

لا ×o _ola.Iا ¤.s.آ ls.l. l.ﺡ,. Nulticasting لا _Ic l.º,a.و ... توiIا .º
Nembers لاو Classes lç. ¤ﺹl>Iا تl.IoaIا eiه _.±s.Iا _o ء... _... ف«. l.هو
مlL. ءl... م«s.. :Iذ ia.و ¤I.oVا _o ¤c«o>o lç.Ic _.L.و Conference System
لا _Ic اio.ao Nulticasting ...

لا ×o _ola.Iا .º ¤..l.Vا تl.IoaIا _o Nulticasting :

1 - مlo±.Vا وأ _o جو,>Iا ¤c«o>o Group Drop ]] Joining :

لا _Iإ مlo±.Vا ¤.Ioc م,I. V Nulticast Group ..±.Iا ى«. _sﺡ. تl.Ioc يأ
لا _Ic port لاو !P Nulticasting دiﺡoIا ، ..,a. ia. :Iذ »..و udpClient Object
لا ماi>..l.و JoinNulticastGroup Nethod لا ..,a. ».. !P Nulticasting يiIا
ﻩ.Iإ »±.. ف«. .I. loآو :
: # C
UdpClient sock = new UdpClient(5020);
sock.JoinNulticastGroup(!PAddress.Parse("225.100.0.1"), 50);
!PEndPoint iep = new !PEndPoint(!PAddress.Any, 0);

VB.NET:
Dim sock As UdpClient = New UdpClient(5020)
sock.JoinNulticastGroup(!PAddress.Parse("225.100.0.1"), 50)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)

¤c«o>o _o مlo±.Vا ¤.Ioc ءlaIV .I. loآو :
: # C
sock.DropNulticastGroup(!PAddress.Parse("225.100.0.1"));

VB.NET:
sock.DropNulticastGroup(!PAddress.Parse("225.100.0.1"))

لا مi>.ﺱ. ذإ Nethods JoinMulticastGroup و DropMulticastGroup »±I وأ
ءlaIإ _.وl.aIا _o ¤c«o>o وأ نا«.c لا _o Nulticast Group ، و اi>..l. م Class
NulticastOption : l..îo. _.,>. !P Address List .º lçao _ola.I Nulticast Group
_oaI Join و Drop يV Nulticast Group ¤.«±c ¤ºl±V لl.oآ .I. loآ مi>.ﺱ.و
l.s..V ل _.l.ر Nulticast :

Vوأ لا ف,a. UDP Socket .I. loآو :
: # C
mcastSocket = new Socket(AddressFamily.!nterNetwork,SocketType.Dgram,
ProtocolType.Udp);

VB.NET:
mcastSocket = New Socket(AddressFamily.!nterNetwork, SocketType.Dgram,
ProtocolType.Udp)

l.:l: ..,a.. م«s. Address List لا lç.Iإ i.ﺱ. ». !P لا .º ﻩIl>دإ i.,. يiIا Group
لا _a>. وأ User . lçL.,. ﻩﺱs.. نا«.aIا _>i. د«..oIا ماi>..l. ت«îﺱIl Bind loآو
.I. :

75
: # C
!PAddress local!PAddr = !PAddress.Parse(Console.ReadLine());
mcastSocket.Bind(!Plocal);

VB.NET:
Dim local!PAddr As !PAddress = !PAddress.Parse(Console.ReadLine)
mcastSocket.Bind(!Plocal)

l.ìl: لا ..,a.. م«s. Nulticast Option .I. loآ دiﺡoIا نا«.aIا lçI i.ﺱ.و :
: # C
NulticastOption mcastOption;
mcastOption = new NulticastOption(local!PAddr);

VB.NET:
Dim mcastOption As NulticastOption
mcastOption) = New NulticastOption(local!PAddr

_Ic ,.a.Iا ..±. ». _oو SetSocketOption ا,..oورl. ¤.V. د«..oIا eiه i>l. ..ﺡ ت
_Ic ,..a.Iا ى«.ﺱo i.iﺡ.I لوVا !P _Ic وأ !Pv6 _Ic وأ Socket وأ TCP وأ UDP
_Ic ,.a.Iا مi>.ﺱ. ف«. eiه l..Ilﺡ .ºو !P »± «ه ei.,. lo ذإ !P _Iإ Nulticast
Group ±c ¤ºl±إ i.,. ..ﺡ ,..a.Iا ع«. دiﺡ. ..l.Iا ,..oورl.Iا .ºو رl..>Vا _îo.و ¤.«
ﻩ.«±c ¤ºl±إ _.. AddNembership ¤.«±c ءlaIإ وأ DropNembership i.ﺱ. ا,.>أو
لا ﻩ.Iإ NulticastOption Object .I. loآ و ﻩ.l..|. l.oº يiIاو :
: # C
mcastSocket.SetSocketOption(SocketOptionLevel.!P,
SocketOptionName.AddNembership,mcastOption);

VB.NET:
Dim mcastOption As NulticastOption
mcastOption = New NulticastOption(local!PAddr)

2 - ¤c«o>o _Iإ لl.رVا Sending Data to a Nulticast Group :

لا ماi>..l. لl.رVا ×.L.ﺱ. _.ﺡ !P Nulticasting لا ..,a. _o Vوأ i.V Socket
Object لا ماi>..l. UDP Connection لا دl..إو !P Nulticasting لا »ºرو Port _Iإ
لا Object !PEndPoint ... لا ماi>..l. لl.رVا ×.L.ﺱ.و sendto method ..ﺡ
لا lçI i.ﺱ. data as Bytes Array لاو !PEndPoint Object ¤Il.ر لl.رV .I. loآو
¤.±. :

: # C
Socket server = new Socket(AddressFamily.!nterNetwork,SocketType.Dgram,
ProtocolType.Udp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse((“225.100.0.1”), 5020);
byte|| data = Encoding.ASC!!.GetBytes(msg.Text);
server.SendTo(data, iep);
server.Close();
msg.Clear();
msg.Focus();



76
VB.NET:
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Parse("225.100.0.1"),
5020)
Dim data As Byte() = Encoding.ASC!!.GetBytes(msg.Text)
server.SendTo(data, iep)
server.Close
msg.Clear
msg.Focus

لl.رVو Binary Data لا ماi>..ا _o i.V V.o ةر«ﺹ لl.ر|آ Nemory Stream
¤..ه _Ic ة,آاiIا .º ةر«±Iا _.,>.I Stream _Iإ lçI.«ﺡ. ». Byte Array :Iذ ia.و
لا ماi>..l. lçIl.رإ sendto Nethod .I. loآو :
: # C
NemoryStream ms = new NemoryStream();
PictureBox1.!mage.Save(ms,System.Drawing.!maging.!mageFormat.Jpeg);
byte|| arr!mage = ms.GetBuffer();
ms.Close();
Socket server = new Socket(AddressFamily.!nterNetwork,SocketType.Dgram,
ProtocolType.Udp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse(“225.100.0.1”), 5020);
server.SendTo(arr!mage,iep);

VB.NET:
Dim ms As NemoryStream = New NemoryStream
PictureBox1.!mage.Save(ms, System.Drawing.!maging.!mageFormat.Jpeg)
Dim arr!mage As Byte() = ms.GetBuffer
ms.Close
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Parse("225.100.0.1"),
5020)
server.SendTo(arr!mage, iep)

3 - لl.s..Vا _o ¤c«o>o a Nulticast Group From Data ng Receivi :

¤c«o>o _o لl.s..Vا ×.L.ﺱ. _.ﺡ لا i.iﺡ. _o Vوأ i.V !P Nulticast صl>Iا
لا ماi>..l. تl.l..Iا لl.s..ا ». ﻩ.Iإ مlo±.Vا و ¤c«o>oIl. Receive Nethod »..و
.º lç±,cو ¤.±. ¤Il.ر لl.s..V .I. loآ :Iذ list Box :
: # C
UdpClient sock = new UdpClient(5020);
sock.JoinNulticastGroup(!PAddress.Parse(“225.100.0.1”), 50);
!PEndPoint iep = new !PEndPoint(!PAddress.Any, 0);

byte|| data = sock.Receive(ref iep);
string stringData = Encoding.ASC!!.GetString(data, 0, data.Length);
listBox1.!tems.Add(iep.Address.ToString() +" :_ "+stringData );

VB.NET:
Dim sock As UdpClient = New UdpClient(5020)
77
sock.JoinNulticastGroup(!PAddress.Parse("225.100.0.1"), 50)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim data As Byte() = sock.Receive(iep)
Dim stringData As String = Encoding.ASC!!.GetString(data, 0, data.Length)
listBox1.!tems.Add(iep.Address.ToString + " :_ " + stringData)
لا مi>.ﺱ. ةر«ﺹ لl.s..Vو memory Stream لا _o تl.l..Iا لl.s..V Receive
Nethod ¤..ه _Ic ة,آاiIا .º lç..,>.و Stream Data ة,o ةر«ﺹ _Iإ lçI.«ﺡ. ».
لا ماi>..l. ى,>أ image.FromStream Nethod .I. loآو :
: # C
UdpClient sock = new UdpClient(5020);
sock.JoinNulticastGroup(!PAddress.Parse(“225.100.0.1”));
!PEndPoint iep = new !PEndPoint(!PAddress.Any, 0);

byte|| data = sock.Receive(ref iep);
NemoryStream ms = new NemoryStream(data);
pictureBox1.!mage = !mage.FromStream(ms);
sock.Close();

VB.NET:
Dim sock As UdpClient = New UdpClient(5020)
sock.JoinNulticastGroup(!PAddress.Parse("225.100.0.1"), 50)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim data As Byte() = sock.Receive(iep)
Dim stringData As String = Encoding.ASC!!.GetString(data, 0, data.Length)
listBox1.!tems.Add(iep.Address.ToString + " :_ " + stringData)

تlb>Vo ¤olه لا ماﺩ>.aا L8 Multicasting تlS.aìا تl.>o,: L8 :

1 - لا ماi>..ا ×.L.ﺱ. V l..أ LﺡVoIا _o Network Stream لا لl.رإ ¤.IoaI
Nulticasting د«>و lçoاi>..ا .IL.. ذإ TCP Socket Connection .º حl.o ,.c «هو
لا Nulticasting لا ماi>..l. lç.c ضla.ﺱ.و memory Stream لl.رV Binary
Stream لا ,.c sendto method ...

2 - لا ماi>..ا :.îo. V Nulticasting ك loopback وأ ¤î.. د«>و مic ¤Ilﺡ .º
لا تls..L. _o يأ ¤.,>. ×.L.ﺱ. _I :IiI لl±.ا Nulticasting :Il±.ا مic ¤Ilﺡ .º
î..Il. ¤ .

3 - ¤c«o>o _o ,.آأ _Iإ »±.. نأ زlç> _îI _îo. ةدia.o تlç> _o _.s.ﺱ. ..ﺡ. ،
تlc«o>o ةic _Iإ لl.رVا ×.L.ﺱ. :Iiآ .

+ - لا لl.رV ¤ﺡ«oﺱoIا ¤aﺱIا ن«î. ةدlaIا .º Nulticasting Data لا ,.c sendto
Nethod لا ماi>..ا :.îo. :IiI ةدوiﺡo Binary Reader 8 Writer لاو Stream
Reader 8 Writer lç.o Vi. لl.s..Vاو لl.رV . ..

5 - لا رl..>ا ¤.Ioc ».. !P Nulticast _I _ºو Network Topology :IiI lçîIo. ..Iا
ls.l. ﻩ.... lo «هو ةدiﺡoIا _.وl.aIl. i.s.Iا _o i.V ..





78
تا,o:uoìا مlb: عو,ao _..b: l.ìl: Conferencing Systems Multicasting :

..ﺡ ¤.sﺹ ¤º,c د«>و ض,.s. ف«. _..L.Iا اiه .º ة,±lﺡoIا ءlsI|. ,±lﺡoIا م«s.
¤.ؤر ذl..Vا ×.L.ﺱ. loآو ذl..Vا نو,. بVLIا _a> l.ه i.,. ذإ ﻩ.VL مloأ ia. _c
ºl±Vl. ﻩ.VL ¤ لا _Ic ة,±lﺡoIا ض,c ¤..lîoإ _Iإ Power Point Slides ×.L.ﺱ. loآ
ماi>..l. ذl..Vا ×o ثiﺡ.Iا بVLIا Text Chatting ...

ﺱs.. l.ه م«s. ف«. تا,o.åo مlL. .هو ¤.ﺱ..ر ¤oL.أ ¤.V. _Iإ تا,o.åoIا مlL. ».
¤.±.Iا تا,o.åoIا مlL.و ..îoIا rL. تا,o.åo مlL.و «.i.sIا ، م«s. ف«. ¤.اi.Iا .º
.Il.Iا _î.Iا .º loآ و rol.,.II ¤.ﺱ..,Iا ¤.l.Iا _oa. :




1 - Duplex Multicast Video Conferencing System Half ] Full :

ºو l.I ت, Nicrosoft لا _o ¤c«o>o Classes لا ×o _ola.. ..Iاو ¤.>رl>Iا DirectX 9
,.olîIا ×o _ola.I lçoاi>..ا ×.L.ﺱ. ..ﺡ ة,.l.o ا لا وأ Scanner يأ وأ ت«±Iا وأ
و ى,>أ ¤.º,L لا مi>.ﺱ. ف«. _..L.Iا اiه .º Classes Direct Show Dot Net
,.olîIا ,.c ةر«ﺹ طls.IV ا lç±,cو لا _Ic Picture box lsﺡV lçIl.رإ ×.L.ﺱ. ..ﺡ
لا _Iإ Nulticast Group لا ماi>..l. memory Stream لاو Sendto method lo «هو
ls.l. el... ..

لا »±. ف«. lçoاi>..ا ×.L.ﺱ. _.ﺡو Direct Show Classes loآو عو,.oIا _Iإ
.I. :
79


ماi>..l. lç.ci.ﺱ. ف«. lçao _ola.. _.ﺡو :
: # C
using DShowNET;
using DShowNET.Device;

VB.NET:
imports DShowNET
imports DShowNET.Device

,.olîIا ,.c لl.رVا rol.,. _î. ن«î..و ا .Il.Iا _î.Iا .º loآ :



لا مi>.ﺱ. ف«. DeviceSelector Class _.a.. ¤.اi. i.c لl>دVا زlç> رl..>V
.I. loآو rol.,.Iا :

: # C
DeviceSelector selector = new DeviceSelector( capDevices );
selector.ShowDialog( this );
80
dev = selector.SelectedDevice;

VB.NET:
Dim selector As DeviceSelector = New DeviceSelector(capDevices)
selector.ShowDialog(Ne)
dev = selector.SelectedDevice

ا طl.IV و ,.olîIا ,.c ةر«±I ا ءl..|. م«s. ف«. method .I. loآ ةi.i> :
: # C
void OnCaptureDone()
{
try {
Trace.WriteLine( "!!DLG: OnCaptureDone" );
toolBarBtnGrab.Enabled = true;
int hr;
if( sampGrabber == null )return;
hr = sampGrabber.SetCallback( null, 0 );
int w = video!nfoHeader.BmiHeader.Width;
int h = video!nfoHeader.BmiHeader.Height;
if( ((w 8 0x03) != 0) ]] (w < 32) ]] (w > +096) ]] (h < 32) ]] (h > +096) )
return;
int stride = w * 3;
GCHandle handle = GCHandle.Alloc( savedArray, GCHandleType.Pinned );
int scan0 = (int) handle.AddrOfPinnedObject();
scan0 += (h - 1) * stride;
Bitmap b = new Bitmap( w, h, -stride, PixelFormat.Format2+bppRgb, (!ntPtr)
scan0 );
handle.Free();
savedArray = null;
!mage old = pictureBox.!mage;
pictureBox.!mage = b;
if( old != null ) old.Dispose();
toolBarBtnSave.Enabled = true;}
catch( Exception){}
}

VB.NET:
Private Sub OnCaptureDone()
Try
Trace.WriteLine("!!DLG: OnCaptureDone("
toolBarBtnGrab.Enabled = True
Dim hr As !nteger
!f sampGrabber !s Nothing Then
Return
End !f
hr = sampGrabber.SetCallback)Nothing ، 0 (
Dim w As !nteger = video!nfoHeader.BmiHeader.Width
Dim h As !nteger = video!nfoHeader.BmiHeader.Height
!f)) w And8 H03) <> 0 (OrElse) w < 32 (OrElse) w > +096 (OrElse) h < 32 (
OrElse) h > +096 (Then
Return
81
End !f
Dim stride As !nteger = w * 3
Dim handle As GCHandle = GCHandle.Alloc(savedArray,
GCHandleType.Pinned(
Dim scan0 As !nteger = C!nt)handle.AddrOfPinnedObject(()
scan0 += (h - 1) * stride
Dim b As Bitmap = New Bitmap(w, h, -stride, PixelFormat.Format2+bppRgb ،
New !ntPtr(scan0((
handle.Free()
savedArray = Nothing
Dim old As !mage = pictureBox.!mage
pictureBox.!mage = b
!f Not old !s Nothing Then
old.Dispose()
End !f
toolBarBtnSave.Enabled = True
Catch e1 As Exception
End Try
End Sub

_oc ». Timer ةر«±Iا طls.Iا ¤.Ioc را,o..V ﻩ.º .Il.Iا د«îIا ¤ºl±إو :
: # C
int hr;
int size = video!nfoHeader.BmiHeader.!mageSize;
savedArray = new byte| size + 6+000 |;

VB.NET:
Dim hr As !nteger
Dim size As !nteger = video!nfoHeader.BmiHeader.!mageSize
savedArray = New Byte(size + 6+000) {}

مi>.ﺱ. ف«. ,>Vا ف,LIا _Iإ ةر«±Iا لl.رVو method .º ﻩa±.و ةر«±Iا لl.رإ
Timer .I. loآو :
: # C
try
{
NemoryStream ms = new NemoryStream();
pictureBox.!mage.Save(ms,System.Drawing.!maging.!mageFormat.Jpeg);
byte|| arr!mage = ms.GetBuffer();
ms.Close();
Socket server = new Socket(AddressFamily.!nterNetwork,SocketType.Dgram,
ProtocolType.Udp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse(textBox1.Text), 5020);
server.SendTo(arr!mage, iep);
server.Close();}
catch (Exception){}





82
VB.NET:
Try
Dim ms As NemoryStream = New NemoryStream
pictureBox.!mage.Save(ms, System.Drawing.!maging.!mageFormat.Jpeg)
Dim arr!mage As Byte() = ms.GetBuffer
ms.Close
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Parse(textBox1.Text),
5020)
server.SendTo(arr!mage, iep)
server.Close
Catch generatedExceptionvariable0 As Exception
End Try

,.olîIا ,.c ةر«±Iا لl.رإ ,±lﺡoIا ×.L.ﺱ. l.هو ا ﻩ.VL _Iإ ¤.ؤر _o _îo.. ف«. loآ
,.olîIا ,.c ﻩ.VL ا _.,.. ..ﺡ ¤.îI. V ¤î..I ﻩoاi>..ا l.ه ض,.s. ف«.و
لا _Iإ تl.l..Iا Access Point لا ب«I.l. Unicast لا V«.. ف«.و Access Point ×.ز«.
لا _Iإ _.o±.oIا ءl±cVا ×.o> _Iإ تl.l..Iا Nulticast Group »çI lçI.,.و
لا ماi>..l. Broadcast .Il.Iا _î.Iا .º loآو :






ةi.l.Vا ¤c«o>o _..c«o>o _Iإ »±.. ,±lﺡoIا ن|º _.lﺱIا _î.Iا .º LﺡV. loآو
.هو 225.100.1.1 lç.Ic ﻩ.VL ةر«ﺹ _.s.ﺱ.. ..ﺡ ، بVLIا ¤c«o>oو
83
22+.100.0.1 lç.Iإ ةر«±Iا _.,. ف«. ..Iاو .. لl.رVا ¤.Ioc ن|º l±.ا LﺡV. loآو
لا _.. Access Point1 لاو Access Point2 لا ماi>..l. ».. Unicast ...
ﻩ.VL ¤.ؤر ذl..Vاو »هذl..أ ¤.ؤر بVLIا ×.L.ﺱ. _.ﺡو ، rol.,. ءl..إ _o i.V
لا ﺱs. مi>.ﺱ.. ..ﺡ لl.s..Vا method ةر«±Iا لl.s..V ls.l. lç.ﺡ,. ..Iا
_oa. »º ءi.IIو New Form .Il.Iا _î.Iا .º loآ i.i> :



لا مi>.ﺱ. ف«. Namespaces لا _o ةر«±Iا لl.s..V ¤.Il.Iا Nulticast Group :

: # C
using System.Net.Sockets ;
using System.Net;
using System.!O;
using System.Threading;

VB.NET:
imports System.Net.Sockets
imports System.Net
imports System.!O
imports System.Threading

¤.l.î. »º ». method .I. loآ لl.s..Vا :

void !mage_Receiver()
{
UdpClient sock = new UdpClient(5020);
sock.JoinNulticastGroup(!PAddress.Parse(textBox1.Text));
!PEndPoint iep = new !PEndPoint(!PAddress.Any, 0);
84

byte|| data = sock.Receive(ref iep);
NemoryStream ms = new NemoryStream(data);
pictureBox1.!mage = !mage.FromStream(ms);
sock.Close();
}
VB.NET:
Sub !mage_Receiver()
Dim sock As UdpClient = New UdpClient(5020)
sock.JoinNulticastGroup(!PAddress.Parse(textBox1.Text))
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim data As Byte() = sock.Receive(iep)
Dim ms As NemoryStream = New NemoryStream(data)
pictureBox1.!mage = !mage.FromStream(ms)
sock.Close()
End Sub

لا ماi>..ا _o i.V lç.ci.ﺱ. _.ﺡو Threading ¤.Ioa. _.a..Iا مlL. ,.l.. V _.ﺡ
لl.s..Vا ، _oa. »º :Ii. م«s. _.ﺡو Timer لا ماi>..V .Il.Iا د«îIا ﻩ.º ×±و
Threading :
: # C
Thread myth;
myth= new Thread (new System.Threading .ThreadStart(!mage_Receiver));
myth.Start ();

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

,.olîIا ,.c ¤Ls.IoIا ةر«±Iا _.,>. _o _îo.. _.ﺡو ا ¤..ه _Ic JPEG !mage File »º
ءl..|. saveFileDialog .I. loآ ﻩ.ci..او :
: # C
try
{
saveFileDialog1.Filter = "JPEG !mage (*.jpg)]*.jpg" ;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{

string mypic_path = saveFileDialog1.FileName;
pictureBox1.!mage.Save(mypic_path);
}
}
catch (Exception){}

VB.NET:
Try
saveFileDialog1.Filter = "JPEG !mage (*.jpg)]*.jpg"
!f saveFileDialog1.ShowDialog = DialogResult.OK Then
Dim mypic_path As String = saveFileDialog1.FileName
pictureBox1.!mage.Save(mypic_path)
85
End !f
Catch generatedExceptionvariable0 As Exception
End Try

لا «هو لوVا عو,.oIا _o ءlç..Vا ». iº l.هو video Conference System ، _.ﺡو
لا rol.,. ماi>..l. ة,±lﺡoIا ض,c ,±lﺡoIا ×.L.ﺱ. Power Point _oa. م«s. ف«.
..îoIا rL. تا,o.åo عو,.o ...

2 - Half Duplex Multicast Desktop Conferencing System ] Full :

,.oIا اiه _o فiçIا لا rol.,. ماi>..l. ة,±lﺡoIا ض,c _o ذl..Vا _.îo. «ه عو
Power Point ¤.ILIا ة,ç>أ _Iإ ذl..Vا زlç> _o ..îoIا rL. ةر«ﺹ _.,.. ..ﺡ ،
ءl..إ ى«. ء.. .º _.lﺱIا rol.,.Iا _c لl.رVا ¤.Ioc .I.>. Vو Classes م«s.I
لا _Iإ lçIl.رإ ». _oو ..îoIا rL. ةر«ﺹ طls.Il. Nulticast Group ». _oو
ماi>..l. بVLIا _Ic lç±,cو lçIl.s..ا Data Show Projector ...

rol.,.Iا _oc LL>o l.هو :




eزlç> _Ic ة,±lﺡoIا ح,.. م«s. ذl..Vا ن|º .Il.Iا _î.Iا _o LﺡV. loآو
¤.دlﺡأ .ه ¤.IoaIا eiه ن|º l±.أ LﺡV. loآو بVLIا _Iإ ةر«±Iا _.,.و .±>.Iا
el>.Vا .هl>.l. lçIa> _îo. loآو _ Full ]] Half Duplex ¤c«o>o ءl..إ _o i.V _آV
lçI.,.و ﻩ.,±lﺡo ذl..Vا ض,a. ..ﺡ ذl..Vا _Iإ .IlLIا _o لl.رVا ¤.IoaI ةi.i>
ةر«±Iا _.,. ذإ ذl..Vا _Ic eزlç> ض,c بVLIا iﺡأ ×.L.ﺱ.و بVLIا ¤c«o>o _Iإ
ذl..Vا ¤c«o>o _Iإ ...

.,. ءl..Vو _oa. »º ..îoIا rL. ةر«ﺹ لl.رإ rol New Form _î.Iا .º loآ i.i>
.Il.Iا :
86


_oa. م«s. ف«. ¤.اi.Iا .º Three Classes .I. loآو ..îoIا rL. ةر«ﺹ طls.IV :

Vوأ PlatForm!nvokeGD!32.cs لا ماi>..l. ..îoIا rL. ةر«ﺹ طls.IV GD!+ لاو
AP! :

: # C
using System;
using System.Runtime.!nteropServices;
namespace SampleGrabberNET
{

//This class shall keep the GD!32 AP!s being used in our program.
public class Platform!nvokeGD!32
{

#region Class variables
public const int SRCCOPY = 133693/6;
#endregion

#region Class Functions
|Dll!mport("gdi32.dll",EntryPoint="DeleteDC")|
public static extern !ntPtr DeleteDC(!ntPtr hDc);

|Dll!mport("gdi32.dll",EntryPoint="DeleteObject")|
public static extern !ntPtr DeleteObject(!ntPtr hDc);

|Dll!mport("gdi32.dll",EntryPoint="BitBlt")|
public static extern bool BitBlt(!ntPtr hdcDest,int xDest,int
yDest,int wDest,int hDest,!ntPtr hdcSource,int xSrc,int ySrc,int RasterOp);

|Dll!mport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")|
public static extern !ntPtr CreateCompatibleBitmap(!ntPtr hdc,
int nWidth, int nHeight);

|Dll!mport ("gdi32.dll",EntryPoint="CreateCompatibleDC")|
87
public static extern !ntPtr CreateCompatibleDC(!ntPtr hdc);

|Dll!mport ("gdi32.dll",EntryPoint="SelectObject")|
public static extern !ntPtr SelectObject(!ntPtr hdc,!ntPtr bmp);
#endregion

#region Public Constructor
public Platform!nvokeGD!32()
{
}
#endregion
}}

: NET . VB
!mports System
!mports System.Runtime.!nteropServices
Namespace SampleGrabberNET

' This class shall keep the GD!32 AP!s being used in our program .
Public Class Platform!nvokeGD!32

# Region " Class variables "
Public Const SRCCOPY As !nteger = 133693/6
# End Region

# Region " Class Functions "
_ > Dll!mport("gdi32.dll",EntryPoint:="DeleteDC (" <
Public Shared Function DeleteDC ) Byval hDc As !ntPtr ( As !ntPtr
End Function

_ > Dll!mport("gdi32.dll",EntryPoint:="DeleteObject (" <
Public Shared Function DeleteObject ) Byval hDc As !ntPtr ( As !ntPtr
End Function

_ > Dll!mport("gdi32.dll",EntryPoint:="BitBlt (" <
Public Shared Function BitBlt ) Byval hdcDest As !ntPtr ، Byval xDest As
!nteger ، Byval yDest As !nteger ، Byval wDest As !nteger ، Byval hDest As
!nteger ، Byval hdcSource As !ntPtr ، Byval xSrc As !nteger ، Byval ySrc As
!nteger ، Byval RasterOp As !nteger ( As Boolean
End Function

_ > Dll!mport ("gdi32.dll",EntryPoint:="CreateCompatibleBitmap (" <
Public Shared Function CreateCompatibleBitmap ) Byval hdc As !ntPtr ، Byval
nWidth As !nteger ، Byval nHeight As !nteger ( As !ntPtr
End Function

_ > Dll!mport ("gdi32.dll",EntryPoint:="CreateCompatibleDC (" <
Public Shared Function CreateCompatibleDC ) Byval hdc As !ntPtr ( As !ntPtr
End Function
_ > Dll!mport ("gdi32.dll",EntryPoint:="SelectObject (" <
88
Public Shared Function SelectObject ) Byval hdc As !ntPtr ، Byval bmp As
!ntPtr ( As !ntPtr
End Function
# End Region

# Region " Public Constructor "
Public Sub New()
End Sub
# End Region
End Class
End Namespace

l..l. Platform!nvokeUSER32.cs لا ×o lçoi>.ﺱ. ف«. ذإ Class طls.IV _.lﺱIا
لا ماi>..l. ..îoIا rL. ةر«ﺹ user32 AP! :
: # C
using System;
using System.Runtime.!nteropServices;
namespace SampleGrabberNET
{
// This class shall keep the User32 AP!s being used in our program.
public class Platform!nvokeUSER32
{

#region Class variables
public const int SN_CXSCREEN=0;
public const int SN_CYSCREEN=1;
#endregion

#region Class Functions
|Dll!mport("user32.dll", EntryPoint="GetDesktopWindow")|
public static extern !ntPtr GetDesktopWindow();

|Dll!mport("user32.dll",EntryPoint="GetDC")|
public static extern !ntPtr GetDC(!ntPtr ptr);

|Dll!mport("user32.dll",EntryPoint="GetSystemNetrics")|
public static extern int GetSystemNetrics(int abc);

|Dll!mport("user32.dll",EntryPoint="GetWindowDC")|
public static extern !ntPtr GetWindowDC(!nt32 ptr);

|Dll!mport("user32.dll",EntryPoint="ReleaseDC")|
public static extern !ntPtr ReleaseDC(!ntPtr hWnd,!ntPtr hDc);

#endregion

#region Public Constructor
public Platform!nvokeUSER32()
{
}
#endregion
89
}
//This structure shall be used to keep the size of the screen.
public struct S!ZE
{
public int cx;
public int cy;
}
}
VB.NET:
!mports System
!mports System.Runtime.!nteropServices
Namespace SampleGrabberNET
' This class shall keep the User32 AP!s being used in our program .
Public Class Platform!nvokeUSER32

Region " Class variables "
Public Const SN_CXSCREEN As !nteger = 0
Public Const SN_CYSCREEN As !nteger = 1
# End Region

# Region " Class Functions "
_ > Dll!mport("user32.dll", EntryPoint:="GetDesktopWindow (" <
Public Shared Function GetDesktopWindow () As !ntPtr
End Function

_ > Dll!mport("user32.dll",EntryPoint:="GetDC (" <
Public Shared Function GetDC ) Byval ptr As !ntPtr ( As !ntPtr
End Function

_ > Dll!mport("user32.dll",EntryPoint:="GetSystemNetrics (" <
Public Shared Function GetSystemNetrics ) Byval abc As !nteger ( As !nteger
End Function

_ > Dll!mport("user32.dll",EntryPoint:="GetWindowDC (" <
Public Shared Function GetWindowDC ) Byval ptr As !nt32 ( As !ntPtr
End Function

_ > Dll!mport("user32.dll",EntryPoint:="ReleaseDC (" <
Public Shared Function ReleaseDC ) Byval hWnd As !ntPtr ، Byval hDc As !ntPtr (
As !ntPtr
End Function

# End Region

# Region " Public Constructor "
Public Sub New()
End Sub
# End Region
End Class
' This structure shall be used to keep the size of the screen .
Public Structure S!ZE
90
Public cx As !nteger
Public cy As !nteger
End Structure
End Namespace

l.Il. : CaptureScreen.cs ..ﺡ rol.,.Iا .º ,.l.o _î.. lçoi>.ﺱ. ف«. ..Iاو
لا ×o _ola.. Class PlatForm!nvokeGD!32 لاو Platform!nvokeUSER32 Class :


: # C
using System;
using System.Drawing;

namespace SampleGrabberNET
{
//This class shall keep all the functionality for capturing the desktop.
public class CaptureScreen
{
#region Public Class Functions
public static Bitmap GetDesktop!mage()
{
//!n size variable we shall keep the size of the screen.
S!ZE size;

//variable to keep the handle to bitmap.
!ntPtr hBitmap;
//Here we get the handle to the desktop device context.
!ntPtr hDC =
Platform!nvokeUSER32.GetDC(Platform!nvokeUSER32.GetDesktopWindow());
//Here we make a compatible device context in memory for screen device
context.
!ntPtr hNemDC = Platform!nvokeGD!32.CreateCompatibleDC(hDC);
//We pass SN_CXSCREEN constant to GetSystemNetrics to get the X
coordinates of screen.
size.cx=Platform!nvokeUSER32.GetSystemNetrics(Platform!nvokeUSER32.SN
_CXSCREEN);
//We pass SN_CYSCREEN constant to GetSystemNetrics to get the Y
coordinates of screen.
size.cy=Platform!nvokeUSER32.GetSystemNetrics(Platform!nvokeUSER32.SN
_CYSCREEN);
//We create a compatible bitmap of screen size using screen device context.
hBitmap = Platform!nvokeGD!32.CreateCompatibleBitmap(hDC, size.cx,
size.cy);
//As hBitmap is !ntPtr we can not check it against null. For this purspose
!ntPtr.Zero is used.
if (hBitmap!=!ntPtr.Zero)
{
//Here we select the compatible bitmap in memory device context and keeps
the reference to Old bitmap.
!ntPtr hOld = (!ntPtr) Platform!nvokeGD!32.SelectObject(hNemDC, hBitmap);
//We copy the Bitmap to the memory device context.
91
Platform!nvokeGD!32.BitBlt(hNemDC, 0, 0,size.cx,size.cy, hDC, 0, 0,
Platform!nvokeGD!32.SRCCOPY);
//We select the old bitmap back to the memory device context.
Platform!nvokeGD!32.SelectObject(hNemDC, hOld);
//We delete the memory device context.
Platform!nvokeGD!32.DeleteDC(hNemDC);
//We release the screen device context.
Platform!nvokeUSER32.ReleaseDC(Platform!nvokeUSER32.GetDesktopWindo
w(), hDC);//!mage is created by !mage bitmap handle and stored in local
variable.
Bitmap bmp = System.Drawing.!mage.FromHbitmap(hBitmap);
//Release the memory to avoid memory leaks.
Platform!nvokeGD!32.DeleteObject(hBitmap);
//This statement runs the garbage collector manually.
GC.Collect();//Return the bitmap
return bmp;
}//!f hBitmap is null return null.
return null;
}
#endregion
}
}

VB.NET:
!mports System
!mports System.Drawing

Namespace SampleGrabberNET
'This class shall keep all the functionality for capturing the desktop.
Public Class CaptureScreen
#Region" Public Class Functions"
Public Shared Function GetDesktop!mage ()As Bitmap
'!n size variable we shall keep the size of the screen.
Dim size As S!ZE

'variable to keep the handle to bitmap.
Dim hBitmap As !ntPtr
'Here we get the handle to the desktop device context.
Dim hDC As !ntPtr =
Platform!nvokeUSER32.GetDC(Platform!nvokeUSER32.GetDesktopWindow(()
'Here we make a compatible device context in memory for screen device
context.
Dim hNemDC As !ntPtr = Platform!nvokeGD!32.CreateCompatibleDC(hDC(
'We pass SN_CXSCREEN constant to GetSystemNetrics to get the X
coordinates of screen.
size.cx=Platform!nvokeUSER32.GetSystemNetrics(Platform!nvokeUSER32.SN
CXSCREEN( _
'We pass SN_CYSCREEN constant to GetSystemNetrics to get the Y
coordinates of screen.
size.cy=Platform!nvokeUSER32.GetSystemNetrics(Platform!nvokeUSER32.SN
CYSCREEN( _
92
'We create a compatible bitmap of screen size using screen device context.
hBitmap = Platform!nvokeGD!32.CreateCompatibleBitmap(hDC, size.cx,
size.cy(
'As hBitmap is !ntPtr we can not check it against null. For this purspose
!ntPtr.Zero is used.
!f Not hBitmap.Equals(!ntPtr.Zero (Then
'Here we select the compatible bitmap in memory device context and keeps
the reference to Old bitmap.
Dim hOld As !ntPtr = CType)Platform!nvokeGD!32.SelectObject(hNemDC,
hBitmap), !ntPtr(
'We copy the Bitmap to the memory device context.
Platform!nvokeGD!32.BitBlt(hNemDC, 0, 0,size.cx,size.cy, hDC, 0, 0,
Platform!nvokeGD!32.SRCCOPY(
'We select the old bitmap back to the memory device context.
Platform!nvokeGD!32.SelectObject(hNemDC, hOld(
'We delete the memory device context.
Platform!nvokeGD!32.DeleteDC(hNemDC(
' We release the screen device context.
Platform!nvokeUSER32.ReleaseDC(Platform!nvokeUSER32.GetDesktopWindo
w(), hDC ( ' !mage is created by !mage bitmap handle and stored in local
variable.
Dim bmp As Bitmap = System.Drawing.!mage.FromHbitmap(hBitmap(
'Release the memory to avoid memory leaks.
Platform!nvokeGD!32.DeleteObject(hBitmap(
'This statement runs the garbage collector manually.
GC.Collect () ' Return the bitmap
Return bmp
End !f ' !f hBitmap is null return null.
Return Nothing
End Function
#End Region
End Class
End Namespace

لا ..î. ف«. ةر«±Iا »>ﺡ .º »îﺡ.Iا ×.L.ﺱ. _.ﺡو method ¤.Il.Iا :
: # C
public Bitmap ResizeBitmap( Bitmap b, int nWidth, int nHeight )
{
Bitmap result = new Bitmap( nWidth, nHeight ); using( Graphics g =
Graphics.From!mage( (!mage) result ) ) g.Draw!mage( b, 0, 0, nWidth,
nHeight );
return result;
}

VB.NET:
Public Function ResizeBitmap(Byval b As Bitmap, Byval nWidth As !nteger,
Byval nHeight As !nteger) As Bitmap
Dim result As Bitmap = New Bitmap(nWidth, nHeight)
' Using
Dim g As Graphics = Graphics.From!mage(CType(result, !mage))
Try
93
g.Draw!mage(b, 0, 0, nWidth, nHeight)
Finally
CType(g, !Disposable).Dispose()
End Try
Return result
End Function

لا مi>.ﺱ. ف«. Namespaces لا ×o _ola.I rol.,.Iا .º ¤.Il.Iا Nulticasting :
: # C
using System.Net;
using System.Net.Sockets;
using System.!O;

VB.NET:
imports System.Net
imports System.Net.Sockets
imports System.!O


_oa. م«s. ». Timer ..îoIا rL. ةر«ﺹ طls.IV لا _Iإ lçIl.رإ و Nulticast Group
دiﺡoIا :
: # C
Bitmap bt = new Bitmap(CaptureScreen.GetDesktop!mage());
picScreen.!mage = ResizeBitmap(bt, 352, 200 );
NemoryStream ms = new NemoryStream();
picScreen.!mage.Save(ms,System.Drawing.!maging.!mageFormat.Jpeg);
byte|| arr!mage = ms.GetBuffer();
ms.Close();
Socket server = new Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse(textBox1.Text), 5020);
server.SendTo(arr!mage,iep);
server.Close();

VB.NET:
Dim bt As Bitmap = New Bitmap(CaptureScreen.GetDesktop!mage)
picScreen.!mage = ResizeBitmap(bt, 352, 200)
Dim ms As NemoryStream = New NemoryStream
picScreen.!mage.Save(ms, System.Drawing.!maging.!mageFormat.Jpeg)
Dim arr!mage As Byte() = ms.GetBuffer
ms.Close
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Parse(textBox1.Text),
5020)
server.SendTo(arr!mage, iep)
server.Close




94
3 - Conferencing System Text plex Multicast Du Half ] Full :

لا ماi>..l. ذl..Vا _Iإ ثiﺡ.Iا ¤.ILIا ×.L.ﺱ. _.ﺡو Text Chat Nulticast
Conference System ءl..|. م«s. ف«. New Form .Il.Iا _î.Iا .º loآو i.i> :




لا ¤ºl±|. »º ». Namespaces ¤.Il.Iا :
: # C
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

VB.NET:
imports System.Net
imports System.Net.Sockets
imports System.Text
imports System.Threading

لا مi>.ﺱ. ف«. method i.c ¤Il.,Iا _.,.. ..ﺡ لl.رVا ¤.Ioc ءا,>V ¤.Il.Iا
لا _Ic La±Iا Enter لا .º ¤Il.,Iا ¤.l.آ ia. Textbox ﺹ±>oIا :

: # C
private void msg_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{if(e.KeyChar == '\r'){
try{
Socket server = new Socket(AddressFamily.!nterNetwork,SocketType.Dgram,
ProtocolType.Udp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse(txt_host.Text), 5020);
95
byte|| data = Encoding.ASC!!.GetBytes(msg.Text);
server.SendTo(data, iep);
server.Close();
msg.Clear();
msg.Focus();
}
catch(Exception){}}}

VB.NET:
Private Sub msg_KeyPress(Byval sender As Object, Byval e As
System.Windows.Forms.KeyPressEventArgs)
!f e.KeyChar = Nicrosoft.visualBasic.Chr(13) Then
Try
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As !PEndPoint = New
!PEndPoint(!PAddress.Parse(txt_host.Text), 5020)
Dim data As Byte() = Encoding.ASC!!.GetBytes(msg.Text)
server.SendTo(data, iep)
server.Close()
msg.Clear()
msg.Focus()
Catch generatedExceptionvariable0 As Exception
End Try
End !f
End Sub

¤I.s.ﺱoIا ¤Il.,Iا ض,a.. ..ﺡ لl.s..Vا ¤.IoaI ¤.Il.Iا د«..oIا مi>.ﺱ. ف«.و
.º list Box ﺹ±>o :
: # C
public void server()
{
try
{
UdpClient sock = new UdpClient(5020);
sock.JoinNulticastGroup(!PAddress.Parse(txt_host.Text), 50);
!PEndPoint iep = new !PEndPoint(!PAddress.Any, 0);

byte|| data = sock.Receive(ref iep);
string stringData = Encoding.ASC!!.GetString(data, 0, data.Length);
listBox1.!tems.Add(iep.Address.ToString() +" :_ "+stringData );
sock.Close();
listBox1.Focus();
msg.Focus();
myth.Abort();
}catch(Exception){}}





96
VB.NET:
Public Sub server()
Try
Dim sock As UdpClient = New UdpClient(5020)
sock.JoinNulticastGroup(!PAddress.Parse(txt_host.Text), 50)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Any, 0)
Dim data As Byte() = sock.Receive(iep)
Dim stringData As String = Encoding.ASC!!.GetString(data, 0,
data.Length)
listBox1.!tems.Add(iep.Address.ToString + " :_ " + stringData)
sock.Close()
listBox1.Focus()
msg.Focus()
myth.Abort()
Catch generatedExceptionvariable0 As Exception
End Try
End Sub

لا ماi>..ا _o i.V lç.lci..Vو Threading ، _oa. »º Timer لا ﻩ.º .ci..او
method لا ماi>..l. ¤s.lﺱIا Thread .I. loآو :

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

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

لا _a.. ف«. Timer ماi>..l. لl±.Vا رز _Ic La±Iا i.c timer1.Enabled = true
ز .ºو ر ءlç.إ لl±.Vا .Il.Iا د«îIا ¤ºl±|. »º :

: # C
timer1.Enabled = false;
txt_host.ReadOnly = false;
msg.Enabled=false;
try
{
Socket server = new Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp);
!PEndPoint iep = new !PEndPoint(!PAddress.Parse(txt_host.Text), 5020);
byte|| data = Encoding.ASC!!.GetBytes("has Left the Room");

server.SendTo(data, iep);
server.Close();
msg.Clear();
msg.Focus();
}
catch(Exception){}
97
VB.NET:
timer1.Enabled = False
txt_host.ReadOnly = False
msg.Enabled = False
Try
Dim server As Socket = New Socket(AddressFamily.!nterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim iep As !PEndPoint = New !PEndPoint(!PAddress.Parse(txt_host.Text),
5020)
Dim data As Byte() = Encoding.ASC!!.GetBytes("has Left the Room")
server.SendTo(data, iep)
server.Close
msg.Clear
msg.Focus
Catch generatedExceptionvariable0 As Exception
End Try




































98
Dot Net Networks 8 TCP/!P Programming
Chapter 5
Application Layer Programming

Application Layer Programming

A. DNS Programming
B. SNTP Programming
C. POP3 Programming
D. HTTP Programming
E. Web Services 8 XNL Programming
F. FTP Programming
















99
o.>,ìا _o>,ìا ¤ا oa:

5.1 : DNS Programming :

¤oi> ,..a. DNS ..Vا .º مi>.ﺱ. ..Iا تloi>Iا »هأ _o ةiﺡاو تlî..Iاو ..,
مlc _î.. ، ¤s.Lو ,±.>.و DNS لا ¤o>,. ¤.Ioa. مl.sIl. Domain Name _Iإ
Domain !P تادو,o _o اi> ة,..آ ¤c«o>o لV> _o :Iذ »..و ﺱîaIا _Iإو _o DNS
) ¤..ao ة,.º _آ lç. ¤ﺹl>Iا تl.l..Iا ةiclº ..iﺡ.. م«s. ..Iاو ( ، ¤.IoaIا eiه أi..
لا مl.s. A Client لا .IL. Domain لl. صl>Iا Client B لl>د|. :Iذو Domain Name
ﻩ. صl>Iا - لا مl.º ls.ﺱo ». ..ﺡ Client B ¤ﺹl>Iا تl.l..Iا ةiclº .º ﻩﺱs. ..,a..
ب DNS Server - _آ ي«.ﺡ. loآ Client لا _.وl.c _Ic ي«.ﺡ. تl.l.. ةiclº _Ic
Domains لl. _oﺱ.و local DNS I>اi. .ﺡ.Il. م«s. ..ﺡ نا«.c _Ic lç Domain
لV> _o Domain Name لا _o _.oوiIا نا«.c .IL. م«s. ei>. »I اذ|º DNS Server
لا م«s. eدl>.إ ia.و DNS Server لا _Iإ نا«.aIا لl.ر|. Client _.,>.. eروi. م«s.و
.º نا«.aIا Local DNS ﻩ. صl>Iا ، .Il.Iا _î.Iا _Iإ ,L.ا :





ا l..îo. ... توiIا .º ×o _ola.I DNS ماi>..l. Name Space System.Net ..Iاو
لا ×.o> _Ic ي«.ﺡ. Classes DNS لا _آ _Ic ي«.ﺡ. ..Iاو Nethods ب ¤ﺹl>Iا
DNS _oا,.o _.oﺱº _Iإ سد«..oIا eiه »ﺱs.و Synchronous Nethods ,.c و
_oا,.o Asynchronous Nethods .I. loآ .هو :

¤.oا,.oIا سد«..oIا Vوأ chronous Nethods Syn .هو :
GetHostName ¤o.º د«..oIا eiه ×>,.و ..«çIا ».ا .I>I مi>.ﺱ. ..Iاو String
لا _Ic ي«.ﺡ. Computer Name _îo.و تا,..oارl. يأ د«..oIا eiه i>l. Vو
.I. loآ lçoاi>..ا :
: # C
string hostname = Dns.GetHostName();

VB.NET:
Private hostname As String = Dns.GetHostName







100
د«..oIا GetHostByName د«..oIا و GetHostByAddress .I. loآ lç.o _آ مi>.ﺱ.و :

: # C
!PHostEntry host_ip = Dns.GetHostByName(Computer_Name); // نا«.aIا .I>I
».Vا ماi>..l.
!PHostEntry host_name = Dns.GetHostByAddress(!P_Address); // ».Vا .I>I
نا«.aIا ماi>..l.

VB.NET:
Private host_ip As !PHostEntry = Dns.GetHostByName(Computer_Name)
Private host_name As !PHostEntry = Dns.GetHostByAddress(!P_Address)

د«..oIا Resolve .هو Overloaded Nethod ×>,. ..ﺡ Host Name lçI .I.رأ اذإ
!P Address ×>,.و Host Address lçI .I.رأ اذإ Host Name لا .º !PHostEntry
¤s.lﺱIا سد«..oIا ماi>..ا _c lçoاi>..ا .I.>. Vو .
lçoاi>..ا ¤s.,L _... لl.oIا اiهو :

: # C
using System;
using System.Net;

class FNO_DNS
{
public static void Nain()
{
!PHostEntry !PHost = Dns.Resolve("www.yahoo.com"); // i.,. يiIا _.oوiIا
ﻩ. صl>Iا .. يVا ¤º,ao
Console.WriteLine(!PHost.HostName); // _olîIl. _.oوiIا ».ا .I>

!PAddress|| addr = !PHost.AddressList; // ¤º«s±o .º _.وl.aIا ¤o.lº ×±و

for(int i= 0; i < addr.Length ; i++) // ¤cl.L ¤º«s±oIا ,ﺹl.c
{Console.WriteLine(addr|i|);}}}
VB.NET:
!mports System
!mports System.Net

Class FNO_DNS

Public Shared Sub Nain()
Dim !PHost As !PHostEntry = Dns.Resolve("www.yahoo.com")
Console.WriteLine(!PHost.HostName)
Dim addr As !PAddress() = !PHost.AddressList
Dim i As !nteger = 0
While i < addr.Length
Console.WriteLine(addr(i))
System.Nath.Nin(System.Threading.!nterlocked.!ncrement(i), i - 1)
End While
End Sub
End Class
0
101
¤.oا,.oIا ,.c سد«..oIا l..l. Asynchronous Nethods :
¤oIî. ةدlc أi..و Begin وأ End lç.Ic ¤I.oVا _oو :
BeginGetHostByName و BeginResolve و EndGetHostByName و EndResolve
ط,... V ﻩ.ا ن«î. .I.>. lç.îI ¤.oا,.oIا سد«..oIا .º لlﺡIا «ه loآ lçIoc ¤a..L
.. ¤..l.Iا ة«L>Iا _Iإ لls..V. roﺱ. V _oا,.oIا _.ﺡ .º rol.,.Iا _oc لloآV lهi.s
ﺱ.loر«s.,.Iا ¸s>. ¤..ﺱIا eiه ..ﺱ. iºو lçIoc ءlç..ا ¤Ilﺡ .º Vإ rol.,.Iا .º
loآ مi>.ﺱ.و ¤.oا,.o ,.aIا ¤s.,LIا ماi>..l. r±.. :IiI rol.,.Iا .º مlc _î..
.I. : Begin___
public static !AsyncResult BeginResolve(string hostname,
AsyncCallback requestCallback, object stateObject)

.º ».. ..«çIا ×±و ».. ..ﺡ .Iا l ر و ,..o و لوVا .Iا l ر و ,..o لا ﻩ.º ف,a. ..l.Iا
delegate Vإ تV>io ,.,o.. :I roﺱ.و delegate ، مi>.ﺱ.و End____ .I. loآ :

public static !PHostEntry EndResolve(!asyncResult ar)

×.o> .I>. م«s. L.ﺱ. و _ol. لl.o l.هو لا !P’s ..ﺡ ¤î..Iا _Ic ةد«>«oIا
لا .I> _Ic _oa. host names _o ProcessStart!nfo ¤.ﺹl>Iا لV> _o
StandardOutput _Iإ ﻩI.«ﺡ. ».. ..ﺡ host name د«..oIا لV> _o
GetNachineNamesFromProcessOutput .º lç..,>. ». Collicaion _.«ﺡ. ».. ».
د«..oIا لV> _o _.وl.c _Iإ ءlo.Vا Dns.Resolve .. لا ماi>..ا ».. la.L
StreamReader ةءا,sI لا collection لl. صl>Iا ProcessStart!nfo لl.oIا «ه اiهو :

: # C
using System;
using System.!O;
using System.Diagnostics;
using System.Net;
using System.Collections.Specialized;

namespace Network!Ps
{
public class Names

{
public StringCollection GetNames()

{
ProcessStart!nfo _start!nfo = new ProcessStart!nfo("net","view");
_start!nfo.CreateNoWindow = true;
_start!nfo.UseShellExecute = false;
_start!nfo.RedirectStandardOutput = true;
Process _process = Process.Start(_start!nfo);
StreamReader _reader = _process.StandardOutput;
StringCollection _machineNames =
GetNachineNamesFromProcessOutput(_reader.ReadToEnd());
StringCollection _machine!Ps = new StringCollection();
foreach(string machine in _machineNames)
{
_machine!Ps.Add(!PAddresses(machine));
}
102
return _machine!Ps;
}
private static string !PAddresses(string server)
{
try
{
System.Text.ASC!!Encoding ASC!! = new System.Text.ASC!!Encoding();
// Get server related information.
!PHostEntry heserver = Dns.Resolve(server);
//assumin the machine has only one !P address
return heserver.AddressList|0|.ToString();
}
catch
{
return "Address Retrieval error for " + server;
}
}
//string manipulations
private StringCollection
GetNachineNamesFromProcessOutput(string processOutput)
{
string _allNachines = processOutput.Substring( processOutput.!ndexOf("\\"));
StringCollection _machines= new StringCollection();
while(_allNachines.!ndexOf("\\") != -1 )
{
_machines.Add(_allNachines.Substring(_allNachines.!ndexOf("\\"),
_allNachines.!ndexOf(" ",_allNachines.!ndexOf("\\")) -
_allNachines.!ndexOf("\\")).Replace("\\",String.Empty));
_allNachines = _allNachines.Substring(_allNachines.!ndexOf("
",_allNachines.!ndexOf("\\") + 1));
}
return _machines;
}
}

public class Runner
{
static void Nain()
{
Names _names = new Names();
StringCollection names = _names.GetNames();
foreach(string name in names)
Console.WriteLine(name);
Console.ReadLine();
}
}





103
: NET . VB
!mports System
!mports System.!O
!mports System.Diagnostics
!mports System.Net
!mports System.Collections.Specialized


Public Class Names

Public Function GetNames() As StringCollection

Dim _start!nfo As ProcessStart!nfo = New ProcessStart!nfo("net",
"view")
_start!nfo.CreateNoWindow = True
_start!nfo.UseShellExecute = False
_start!nfo.RedirectStandardOutput = True
Dim _process As Process = Process.Start(_start!nfo)
Dim _reader As StreamReader = _process.StandardOutput
Dim _machineNames As StringCollection =
GetNachineNamesFromProcessOutput(_reader.ReadToEnd())
Dim _machine!Ps As StringCollection = New StringCollection
For Each machine As String !n _machineNames
_machine!Ps.Add(!PAddresses(machine))
Next machine
Return _machine!Ps
End Function
Private Shared Function !PAddresses(Byval server As String) As String
Try
Dim ASC!! As System.Text.ASC!!Encoding = New
System.Text.ASC!!Encoding
' Get server related information.
Dim heserver As !PHostEntry = Dns.Resolve(server)
'assumin the machine has only one !P address
Return heserver.AddressList(0).ToString()
Catch
Return "Address Retrieval error for " 8 server
End Try
End Function
'string manipulations
Private Function GetNachineNamesFromProcessOutput(Byval
processOutput As String) As StringCollection
Dim _allNachines As String =
processOutput.Substring(processOutput.!ndexOf("\"))
Dim _machines As StringCollection = New StringCollection
Do While _allNachines.!ndexOf("\") <> -1
_machines.Add(_allNachines.Substring(_allNachines.!ndexOf("\"),
_allNachines.!ndexOf(" ", _allNachines.!ndexOf("\")) -
_allNachines.!ndexOf("\")).Replace("\", String.Empty))
_allNachines = _allNachines.Substring(_allNachines.!ndexOf(" ",
_allNachines.!ndexOf("\") + 1))
104
Loop
Return _machines
End Function
End Class

Public Class Runner
Shared Sub Nain()
Dim _names As Names = New Names
Dim names As StringCollection = _names.GetNames()
For Each name As String !n names
Console.WriteLine(name)
Next name
Console.ReadLine()
End Sub
End Class




































105
5.2 : Programming 3 POP & SMTP
l..iﺡ. .º ء,>Iا ل«آ«.,. ¤>o,. _c _.lﺱIا DNS ¤o>,. ¤.Ioc _c ل«.ﺱoIاو
Domain _Iإ قlL. ».ا _o !P .. .º ¤.IoaIا eiç. مl.sIا ¤.s.آ l...و ﺱîaIl.و
برl. ، اiه .º ء,>Iا لا ¤s.LI ى,>Vا تV«آ«.و,.Iا ¸a. ¤>o,. _c ثiﺡ.. ف«.
Application Layer لا l.ه loهو SNTP رإ _c ل«.ﺱoIاو i.,.Iا ,.c _.l.,Iا لl.
لا و ..و,.îIVا POP3 _oc لV> _o ن«.,Iا _Iإ ¤Il.,Iا _.ﺹ«. ¤.Ioc _c ل«.ﺱoIاو
Download لا _o lçI Nail Server .ºو ء,>Iا لا _c ثiﺡ.. ف«. _ﺡVIا HTTP
Programming لا rs±. .º ..l.أ _î.. مi>.ﺱ. يiIاو Web ، ﻩ.ا »IaIا ×o
..آ تV«آ«.و,. i>«. ¤>lﺡIا i.c lçﺡ,. _Ic ..l ف«. ة, ..

لوVا ء,>ìا : Simple Mail Transfer Protocol Programming – SMTP
لا نأ فو,aoIا _o Nail Server i.,.Iا لl.s..ا و لl.رإ تl.Ioc ¤.,>.. م«s.
.Il.Iا _î.Iا .º loآ .هو ءا,>أ ¤.V. _Iإ ..,..Vا ,.c ..و,.îIVا :

NTA – Nessage Transfer Agent لl.رVا _c ل«.ﺱoIاو Outgoing _.ﺹ«.Iاو
!ncoming _.l.,II
NDA - Nessage Delivery Agent لا تl.Ioc _c ل«.ﺱoIا و filtering _o iآl.Iاو
¤Il.,Iا ل«ﺹو
NUA - Nessage User Agent .º ¤Il.,Iا _.,>. و ةءا,º ¤.Ioc _c ل«.ﺱoIاو
Database _.s.ﺱoIا ىiI Client »..و ل«آ«.و,. ماi>..l. ¤.IoaIا eiه POP - Post
Office Protocol .Il.Iا _î.Iا _Iإ ,L.ا :


لا ل«آ«.,. مi>.ﺱ. و SNTP Simple Nail Transfer Protocol لا .º ..l.أ _î..
NTA لl.رإ تl.Ioc يأ Outgoing _.ﺹ«.و !ncoming _.l.,Iا .

106
بlﺱﺡ :Io. :.أ _o iآl.Iا Vوأ .>. _..L.I SNTP لا _o !nternet Provider صl>Iا
لا ¤.,>. ×.L.ﺱ. :. Account لا rol.,. لV> _o :. صl>Iا Outlook Express
لا ×o د«>«oIا Windows بlﺱﺡ :Io. V ..آ اذإ SNTP _o rol.,.Iا ¤.,>. ×.L.ﺱ.
ءl..إ لV> virtual SNTP Server لا _.,L _c !!S _o lç...... :Iذو : Control
Panel >> Add/Remove Programs لا _o _آ _.as. _o iآl. !!S لاو SNTP .º loآ
.Il.Iا _î.Iا :


لا داicإ ». Server لا _o !!S .Il.Iا _î.Iا .º loآ :



لا ل«آ«.و,. ماi>..ا ... توiIا »ci. SNTP لV> _o Name Space
System.Web.Nail سVîIا _Ic ي«.ﺡ. و SmtpNail V> _o ..Iاو مi>.ﺱ. lçI
د«..oIا Send لا ,.c ¤Il.,Iا لl.رV مi>.ﺱ. ..Iاو Port 25 لا «هو Port ﺹ±>oIا
ل«آ«.و,.I SNTP د«..oIا ,..a. و Send " overloaded Nethod " ةic i>l. ..ﺡ
تا,..oارl. ¤a.رأ _Iإ iﺡاو ,..oا,. ×o lçoاi>..ا :.lîo|. ذإ لlî.أ ، .±ا,.ºا _î..و
تا,.oا,.Iا مi>.ﺱ. ¤.Il.Iا :
SmtpNail.Send(string from, string to, string subject, string body)
107
و ﻩ.Iإ _.,oIا نا«.c ﻩ.º ×±«. ..l.Iاو _.,oIا نا«.c ﻩ.º ×±«. لوVا ,..oا,.Iا
¤Il.,Iا ﺹ.I ×.ا,Iاو ¤Il.,Iا نا«.aI .Il.Iا ,..oا,.Iا .

»º ..و,.îIVا i.,.Iا لl.ر|. م«s. rol.,. _oaIو ءl..|. New Form _î.Iا .º loآ
.Il.Iا :

¤ºl±|. »º ». Name Space System.Web.Nail ، ) :.iI ,çL. »I اذإ .Nail جارد|. »º
Name Space System.Web لا _Iإ References ( .Il.Iا د«îIا ¤.l.î. »º ». :

¤ºl±إ _ﺱ.. V Name Space rol.,.Iا ¤.اi. .º اiه
: # C
using System.Web.Nail;

VB.NET:
imports System.Web.Nail;

لl.رVا رز .º اiه د«îIا ¤.l.آ ».
: # C
try
{
string from = textBox1.Text;
string to = textBox2.Text;
string subject = textBox3.Text;
string body = textBox+.Text;
SmtpNail.SmtpServer = textBox5.Text;
SmtpNail.Send(from, to, subject, body);
}
catch (Exception ex) {NessageBox.Show(ex.Nessage);}

VB.NET:
Try
Dim from As String = textBox1.Text
Dim to As String = textBox2.Text
Dim subject As String = textBox3.Text
Dim body As String = textBox+.Text
SmtpNail.SmtpServer = textBox5.Text
108
SmtpNail.Send(from, to, subject, body)
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try
اﺩ> ¤olه ¤b>Vo :
i.> _î.. _oa. د«îIا اiه ، لا _.as. _o iآl.Iا .>. _آV SNTP لا _o !!S ,آذ loآ
×±«. »ºو _.lﺱIا .º !P لا ب صl>Iا SNTP ) a. ». يiIاو .º ls.ﺱo ﻩs., SNTP
virtual Server ( لl. Textbox SNTP Server ، لا _o l±.أ iآl.Iا .>. SNTP Server
¤..îoIا ماi>..ا »ci. :.iI CDO2 - Nicrosoft Collaboration Data Objects
version 2 _Ic _±ﺡ. ف«. Vإو Exception _Iإ ل«ﺹ«Iا ×.L.ﺱ. V ﻩ.l. ك,.>.
CDO2 Object ، ا .º ×o lçoi>..ا ».. ةدlaI Windows XP و Windows 2000 _oa.و
لا ..... i.c .±ا,.ºا _î.. SNTP virtual Server ×o وأ Nicrosoft Exchange
Server2003 مi>.ﺱ. ..آ اذإ loأ Exchange version 5 وأ 5.5 _Ic _±ﺡ. ف«ﺱº
لا Exception ,آiIا _.lﺱIا .

مis. ,.آVا ء,>Iا : anced Programming SNTP Adv
,.c _.l.ر لl.رV L.ﺱ. لl.o _.lﺱIا لl.oIا ,..a. SNTP ماi>..l. CDO2 ، .ºو
لا rol.,. _.o roا,. ءl..إ i.c ةدlaIا Outlook لا ماi>..ا ».. HTNL Format
i.,.Iا لl.s..ا و لl.رV تارl.> ةic :.La. la.Lو تlsﺡIo لl.رإ ¤..lîoإ Vإ ¤ºl±Vl.
ه ..و,.îIVا لا ماi>..l. _ HTTP لا وأ POP3 ... rol.,. ءl..|. م«s. ف«. l.هو
لا ماi>..l. ..و,.îIVا i.,.Iا لl.s..او لl.ر|. م«s. L.ﺱ. SNTP و POP3 ¤.ﺱ..
لا ماi>..V POP3 بlﺱﺡ :.iI ,º«.. نأ .>.º POP3 لا _o !SP نأ وأ :. صl>Iا
...... م«s. Nicrosoft Exchange Server2003 ç> _Ic مi>.ﺱ. ..ﺡ. eداicإو كزl
لا POP3 د«>«I جl.ﺡ. ف«. lهi.c ذإ Domain Controller و زlç>Iا _Ic ...o
Windows 2003 Server لا ..... _Iإ ¤ºl±Vl. Active Directory ﻩ.Ic .
لV> _o :Iذو صا«>Iا eiه ماi>..V زl.oo »cد ... توiIا .oiº Name Space
System.Web.Nail îIا ماi>..l.و سV NailNessage لا »ciI HTNL Format و
سVîIا NailAttachment لا ¤>o,.I _آVو ¤Il.,Iا ×o تlsﺡIo لl.رإ ¤..lîoإ »ciI
POP3 ماi>..ا م,I. Name Space System.Net.Sockets و System.Net و
System.!O _oc ».. ..ﺡ Session لا ×o صl> Server د«>و ﺹﺡs. ¤.Ioa. مl.sII
.i> _.l.ر .º lç...a.. م«s. lهد«>و ¤Ilﺡ .ºو ةi List Box وأ Treelist ¤>lﺡIا .ﺱﺡ
لا م«s. lهاiﺡإ _Ic La±Iا i.cو Client _oa. Download لا _o ¤Il.,I Nail Server
_oaIو Advanced SNTP eNail Sender i>l. »º Object سVîIا _o NailNessage
.I. loآ :
: # C
using System.Web.Nail;

try
{
NailNessage mm = new NailNessage();

_.lﺱIا .º loآو .Il.Iا د«îIا ¤ºl±إ ».
mm.From = textBox1.Text;
mm.To = textBox2.Text;
// mm.Cc = ¤>lﺡIا .ﺱﺡ eiه ﺹ>. _o ,.آV لl.رVا _o :.îo. ﻩ.ا LﺡV
// mm.Bcc =
mm.Subject = textBox3.Text;
mm.Headers.Add("Reply-To", "fadi822000@yahoo.com"); // ×±«I يأ تlºl±إ
¤Il.,Iا ×o lهi.,.
mm.Headers.Add("Comments", "This is a test HTNL message");
mm.Priority = NailPriority.High; // ¤Il.,Iا ¤.oهأ تارl.> ×±و :.îo.
109
mm.BodyFormat = NailFormat.Html; // مi>.ﺱoIا تloر«sIا ع«.
mm.Body = "<html><body><h1>" + textBox+.Text + "</h1></html>";
SmtpNail.Send(mm);
}
catch (Exception ex) {NessageBox.Show(ex.Nessage);}

VB.NET:
imports System.Web.Nail;

Try
Dim mm As NailNessage = New NailNessage
mm.From = textBox1.Text
mm.To = textBox2.Text
mm.Subject = textBox3.Text
mm.Headers.Add("Reply-To", "fadi822000@yahoo.com")
mm.Headers.Add("Comments", "This is a test HTNL message")
mm.Priority = NailPriority.High
mm.BodyFormat = NailFormat.Html
mm.Body = "<html><body><h1>" + textBox+.Text + "</h1></html>"
SmtpNail.Send(mm)
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try

لا د«آ مi>.ﺱ. ¤Il.,Iا »ﺱ> نأ LﺡV HTNL »>ﺡ وأ ن«I يأ ×±و _o :.îo. اiهو
لا ماi>..l. ﻩIoc _îo. ء.. يأ وأ HTNL ) لا »ﺱº ×>ار HTNL ف,a.I ىi..oIl.
¤a.ا,Iا ¤...,îﺱIا ¤aIIا eiه _Ic ( ، .>. تlsﺡIo لl.رإ _Ic ردlº rol.,.Iا _a>Iو
سVîIا ماi>..ا NailAttachment د«îIl. .I. loآو ﻩ.º .IoIا ».ا جاردإو :
: # C
NailAttachment myattach =
new NailAttachment("Your_Attached_File_path.extension",
NailEncoding.Base6+);

لا rol.,. _oc _o l..ç..ا iº l.هو SNTP _olآ _î.. ، لا ¤.Ioc la.L Design
ro,.oIا ة,.>و ءlآذو قوذ .ﺱﺡ _Ic io.a. lه,.cو .

VB.NET:
Dim myattach As NailAttachment = New
NailAttachment("Your_Attached_File_path.extension", NailEncoding.Base6+)












110
l.:l: : Programming 3 Post Office Protocol Version - 3 POP

لا ل«آ«.,. ¤s.Lو ن|º ls.l. l..iﺡ. loآ POP3 لا ء,> .º _oa. يiIاو NUA - Nail
User Agent _Ic Port 110 لا ل«آ«.,. _o± TCP _c ل«.ﺱoIا ﻩ.«آ .º _oî.
ن«.,Iا _Iإ ¤Il.,Iا _.ﺹ«. ¤.Ioc Client لV> _o _oc Download لا _o lçI Nail
Server لا .º _.l.,Iا Lsﺡ. ..ﺡ Nail Folder لا زlç> .º l.l.أ د«>«oIاو Client
.Il.Iا _î.Iا _Iإ ,L.أ :



_I _.اi.Iا _oو POP3 ل«آ«.,. !nteractive Nail Access Protocol – !NAP ﻩIV> _oº
ءl..إ مi>.ﺱoIا ×.L.ﺱ. Nail Folder و ﻩ. ¤ﺹl> لا .º _آV Nail server ﺱ.Iو
لا تا,.o _o eiه ,..a.و ن«.,Iا زlç> .º !NAP ةءا,º نأ ذإ .º«Iا ﺱs.. ﻩ.l...و
لا لV> _o ة,.l.o ».. ¤Il.,Iا Server _o ,.آأ _o lç.ءا,º ×.L.ﺱ. ..ﺡ Client
_.l.,Iا مدl> ,.io »îﺡ. .ه ﻩ.º ¤Iî.oIا _آVو Nail Server Administrator »>ﺡ.
لا Nail Folder .Il.Iا _î.Iا _Iإ ,L.أ ةدوiﺡo lç.a. ةدlaIا .º ن«î. ذإ :

لا نأ LﺡV Nail Folder _o± ×s. Nail Server _sﺡ.Iا ia. ﻩ.ءا,º »..و
Authentication _oî. ﻩ.Iî.o ن|º l.Iº loآ _آV رو,oIا ¤oIآو مi>.ﺱoIا ».ا _o
لا ¤a. ¤.دوiﺡo .º Nail Folder lآ,.I r±.. اiI لا ماi>..ا ة,..îIا ت POP3 ﻩ.«آ
لا «ه ¤aﺱIا .º »îﺡ.. يiIlº ¤aﺱIا دوiﺡo ,.c Client ل _>د Vو Nail Server
Administrator lç. .

لا دlo.cا l.ر,º l..أ lo.و POP3 ,oVا م,I. ذإ ﻩ.>o,.. أi.. ف«. _.l.,Iا ةءا,º ¤.IoaI
ماi>..ا Name Space System.Net.Sockets و System.Net و System.!O ».. ..ﺡ
_oc Session لا ×o صl> Server لا ماi>..l. Socket د«>و ﺹﺡs. ¤.Ioa. مl.sII
111
.º lç..وl.c ¤..a.. م«s. lهد«>و ¤Ilﺡ .ºو ةi.i> _.l.ر List Box وأ Treelist صl>
لا م«s. lهاiﺡإ _Ic La±Iا i.cو ¤>lﺡIا .ﺱﺡ Client _oa. Download _o ¤Il.,I
لا Nail Server لا _Iإ Nail Folder .º lç±,c ». Textbox .


ءl..|. »º _..L.Iو New Form .Il.Iا _î.Iا .º ,çL. loآ i.i> :



¤ºl±|. »º ». Name Spaces ¤.Il.Iا :
: # C
using System.Net;
using System.Net.Sockets;
using System.!O;

VB.NET:
imports System.Net
imports System.Net.Sockets
imports System.!O

لا ×o _ola.Iا ».. ﻩ.ا LﺡV Socket لاو Stream ءl..V Session لا ×o Server
لا ل«آ«.,. ماi>..l. TCP لا _o ¤Il.,Iا ةءا,ºو POP3 Server .

rol.,.Iا ¤.اi. .º ¤.Il.Iا ..رla.Iا ¤ºl±|. »º ». ) .ﺱ..,Iا سVîIا ..,a. ia. يأ –
لا ¤sL.o .º Global Declaration : (
: # C
public TcpClient Server;// فiç. :Iذو .. .. ..Iا ل«آ«.,. _o .î>.وا قls..ا
¤ﺱI>Iا ءl..إ
public NetworkStream NetStrm;// مi>.ﺱoIا تlo«Iao لl.رV ﻩoi>.ﺱ. ف«.
public StreamReader RdStrm; // ب«.Iا _o ةدرا«Iا تlo«IaoIا ةءا,sI 3 Server
public string Data; // .º lçoاi>..V _.l.,Iا دic ¤º,ao
public byte|| szData; // ب«.Iا _o ةدرا«Iا تl.l..Iا _.,>.I 3 Server
public string CRLF = "\r\n";// rol.,.Iا .º lçoاi>..V i.i> ,L. _oaI ..

112
VB.NET:
Public Server As TcpClient
Public NetStrm As NetworkStream
Public RdStrm As StreamReader
Public Data As String
Public szData As Byte()
Public CRLF As String = "" 8 Nicrosoft.visualBasic.Chr(13) 8 "" 8
Nicrosoft.visualBasic.Chr(10) 8 ""

لا .º Connect Button .Il.Iا د«îIا ¤ºl±|. »º :
: # C

// create server POP3 with port 110
// ب«.Iا ×o _... ءl..VServerلا ,.c Port «هو ﺹ±>oIا 110
Server = new TcpClient(POPServ.Text,110);
try
{
// initialization
NetStrm = Server.GetStream();
RdStrm= new StreamReader(Server.GetStream());
Status.!tems.Add(RdStrm.ReadLine());

// Login Process
// ب«.Iا _Iإ lه,.,o.و رو,oIا ¤oIآو مi>.ﺱoIا ».ا لl>دإServer
Data = "USER "+ User.Text+CRLF;
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.!tems.Add(RdStrm.ReadLine());
Data = "PASS "+ Passw.Text+CRLF;
// صl>Iا درا«Iا قوi.ﺹ ةءا,º ».. رو,oIا ¤oIآو مi>.ﺱoIا ».ا _o iآl.Iا ia.
مi>.ﺱoIl.
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.!tems.Add(RdStrm.ReadLine());

Send STAT command to get information ie: number of mail and size
.º ةد«>«oIا _.l.,Iا دic ¤º,aoI POP3 Server اi>..l. م ,oVا STAT
Data = "STAT"+CRLF;
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.!tems.Add(RdStrm.ReadLine());

لا _Iإ .Il.Iا د«îIا ¤ºl±|. »º Disconnect Button :
// Send OU!T command to close session from POP server
Data = "OU!T"+CRLF;
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.!tems.Add(RdStrm.ReadLine());
//close connection
NetStrm.Close();
RdStrm.Close();
113
VB.NET:
Server = New TcpClient(POPServ.Text, 110)
NetStrm = Server.GetStream
RdStrm = New StreamReader(Server.GetStream)
Status.!tems.Add(RdStrm.ReadLine)
Data = "USER " + User.Text + CRLF
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray)
NetStrm.Write(szData, 0, szData.Length)
Status.!tems.Add(RdStrm.ReadLine)
Data = "PASS " + Passw.Text + CRLF
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray)
NetStrm.Write(szData, 0, szData.Length)
Status.!tems.Add(RdStrm.ReadLine)
Data = "STAT" + CRLF
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray)
NetStrm.Write(szData, 0, szData.Length)
Status.!tems.Add(RdStrm.ReadLine)
Data = "OU!T" + CRLF
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray)
NetStrm.Write(szData, 0, szData.Length)
Status.!tems.Add(RdStrm.ReadLine)
NetStrm.Close
RdStrm.Close

درا«Iا قوi.ﺹ _o _.l.,Iا ةءا,sIو ) ةءا,º »... .±ا,.ºا _î.. ة,.>Vا ¤Il.,Iا ( »º
لا _Iإ .Il.Iا د«îIا ¤ºl±|. Read Last Come Email Button :
: # C
string szTemp;
Nessage.Clear();
try
{

// retrieve mail with number mail parameter
Data = "RETR 1"+CRLF; // lç.ءا,º دا,oIا ¤Il.,Iا »ºر i.iﺡ.I

szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
szTemp = RdStrm.ReadLine(); // lç.cl.L ».. _.ﺡ .ºåo _î.. ¤Il.,Iا _.,>.
if(szTemp|0|!='-')
{
while(szTemp!=".")
{
Nessage.Text += szTemp+CRLF;
szTemp = RdStrm.ReadLine();
}
}
else
{Status.!tems.Add(szTemp);}
}
catch(!nvalidOperationException err){Status.!tems.Add("Error:
"+err.ToString());}
114
VB.NET:
Dim szTemp As String
Nessage.Clear
Try
Data = "RETR 1" + CRLF
szData = System.Text.Encoding.ASC!!.GetBytes(Data.ToCharArray)
NetStrm.Write(szData, 0, szData.Length)
szTemp = RdStrm.ReadLine
!f Not (szTemp(0) = "-"C) Then
While Not (szTemp = ".")
Nessage.Text += szTemp + CRLF
szTemp = RdStrm.ReadLine
End While
Else
Status.!tems.Add(szTemp)
End !f
Catch err As !nvalidOperationException
Status.!tems.Add("Error: " + err.ToString)
End Try

ب«.Iا _oc ¤.s.آ ح,.. .oº iº l.هو 3 L.ﺱ. لl.o اiهو .. توiIا .º ﻩ.>o,.و
لl. ﻩ... _olآ عو,.o _oaI ﻩ.o ءi.Iا ×.L.ﺱ. Outlook ..ﺡ .º«.و,î.o. صl>Iا
لا .Io ماi>..ا ×.L.ﺱ. DLL Vi. ةدرا«Iا _.l.,Iا ض,aI رر«I.ﺱآإ ..,..Vl. صl>Iا
_î. _Ic lç±,c _o HTNL Code _oc ×.L.ﺱ. loآ Tree List _.l.,Iا ×±«I
lç.ءا,sI _.lﺱIا د«îIا .º ﻩa±و ».. .IﺱIﺱ. »ºر ¤Il.ر _îI ن«î. ..ﺡ ةدرا«Iا
»º,Iا .oi>..ا ..ﺡ 1 s. يiIاو .±ا,.ºا _î.. م« ةدرا«Iا ة,.>Vا ¤Il.,Iا ةءا,s. ،

























115
5.3 : Hyper Text Transfer Protocol Programming – HTTP

لا ¤s.Lو ﺹ>I.. HTTP .IL _.ﺹ«.I مi>.ﺱoIا ل«آ«.,.Iا ﻩ.ا _Ic مlc _î..
مi>.ﺱoIا User Request ..«Iا _Iإ Server لا مl.º ». web server _Ic د,Il. لا
Request ب _oﺱ. يiIاو Server Response لا لlî.أ ×.o> _s. ×.L.ﺱ. i.آl..و
) Nultimedia ( e,.cو «.i.º و ت«ﺹ و ةر«ﺹو ﺹ.Iا _o .. لا _o Web Server لا _Iإ
Client Application ماi>..l. Byte Stream object .
لا ل«آ«.,. _oa. HTTP لا _Ic Application Layer ..a. اiهو _î.. ﻩoاi>..ا
.º لlﺡIا «ه loآ مi>.ﺱoIا ¤ç>او _o ,.l.o DNS,SNTP,POP3,FTP _Iإ ,L.ا
.Il.Iا _î.Iا :


Vوأ : Downloading From Web Server
لا ×o _ola.Iا ×.L.ﺱ. Web Server ... توiIا .º سVîIا ماi>..l. WebClient
.º د«>«oIا Name Space System.Net .o> l.I مis. ذإ .IL _.ﺹ«.I تl..lîoVا ×
ﻩ.Ic د,Iا و ن«.,Iا User Request 8 Server Response لا »ci.و WebClient Class
¤.V. Nethods لا _o تl.l..Iا _.oﺡ.I Web Server .هو :

1 - DownloadData لا _o تl.l..Iا .I> lç.s.Lوو Web Server .º lç..,>.و Byte
Array _î. _Ic ض,a.و HTNL Code .و لl.oآ .I. loآ مi>.ﺱ :
: # C
using System;
using System.Net;
using System.Text;
class DownloadData_Nethod
{
public static void Nain ()
{
WebClient wc = new WebClient();
byte|| response =
wc.DownloadData("http://www.google.com");
Console.WriteLine(Encoding.ASC!!.GetString(response));
}
}



116
VB.NET:
!mports System
!mports System.Net
!mports System.Text

Class DownloadData_Nethod

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
Dim response As Byte() = wc.DownloadData("http://www.google.com")
Console.WriteLine(Encoding.ASC!!.GetString(response))
End Sub
End Class

2 - DownloadFile لا _o lo .Io _s. lç.s.Lوو Web Server .º ة,.l.o lç..,>.و
Local Computer .IoIا ׺«o ,.,o. ا«. :.Ic lo ذإ اi> ماi>..Vا _ç. «هو
نlîoIاو لl.oآ .I. loآ مi>.ﺱ.و ﻩ.º .IoIا _.,>. i.,. يiIا :
: # C
using System;
using System.Net;

class DownloadFile_Nethod
{
public static void Nain ()
{
WebClient wc = new WebClient();
string filename = "C:\\ra.zip";

Console.WriteLine("Download in Progress Please Waite...");

wc.DownloadFile("http://www.personalmicrocosms.com/zip/ra.zip", filename);

Console.WriteLine("file downloaded");
}
}

VB.NET:
!mports System
!mports System.Net
!mports System.Text

Class DownloadData_Nethod

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
Dim response As Byte() = wc.DownloadData("http://www.google.com")
Console.WriteLine(Encoding.ASC!!.GetString(response))
End Sub
End Class

117
3 - OpenRead ءl..إ lç.s.Lوو Read Only Stream ن«.,Iا _.. لاو Server .I>I
_o تl.l.. URL .º ﻩ..,>.و دiﺡo Stream Object لا ,.,o. ia. URL i.,. يiIا ׺«oII
د«..oIا ماi>..l.و ﻩ±,c ReadLine لا .º ¤.,>oIا تl.l..Iا ض,c ×.L.ﺱ. Stream
Object _î. _Ic HTNL Code .
¤LﺡVo : د«..oIا مi>.ﺱ. Peek لا ¤.lç. ¤º,aoI Stream Object .
: # C
using System;
using System.!O;
using System.Net;

class OpenRead_Nethod
{
public static void Nain ()
{
WebClient wc = new WebClient();
string response;

Stream strm = wc.OpenRead("http://www.google.com");
StreamReader sr = new StreamReader(strm);

while(sr.Peek() > -1)
{
response = sr.ReadLine();
Console.WriteLine(response);
}
sr.Close();
}
}

VB.NET:
!mports System
!mports System.!O
!mports System.Net

Class OpenRead_Nethod

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
Dim response As String
Dim strm As Stream = wc.OpenRead("http://www.google.com")
Dim sr As StreamReader = New StreamReader(strm)
While sr.Peek > -1
response = sr.ReadLine
Console.WriteLine(response)
End While
sr.Close()
End Sub
End Class

لا ي«.ﺡ.و WebClient Class لا _o ¤c«o>o _Ic Properties مi>.ﺱ. ..Iاو
لا _c تlo«Iao .I>I Web Host _.o property ResponseHeaders مi>.ﺱ. يiIاو
118
لا _c ¤olه تlo«Iao .I>I web host c _.o لا دi Headers لا ع«.و cash control
لا ».او Server لا ع«. و Encoding ¤olçIا تlo«IaoIا _o lه,.cو مi>.ﺱoIا ،
لl.oآ .I. loآ مi>.ﺱ.و :
: # C
using System;
using System.Net;

class ResponseHeaders_property
{
public static void Nain ()
{
WebClient wc = new WebClient();
byte|| response =
wc.DownloadData("http://www.google.com");
WebHeaderCollection whc = wc.ResponseHeaders;
Console.WriteLine("header count = {0}", whc.Count);
for (int i = 0; i < whc.Count; i++)
{
Console.WriteLine(whc.GetKey(i) + " = " + whc.Get(i));
}
}
}

VB.NET:
!mports System
!mports System.Net

Class ResponseHeaders_property

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
Dim response As Byte() = wc.DownloadData("http://www.google.com")
Dim whc As WebHeaderCollection = wc.ResponseHeaders
Console.WriteLine("header count = {0}", whc.Count)
Dim i As !nteger = 0
While i < whc.Count
Console.WriteLine(whc.GetKey(i) + " = " + whc.Get(i))
System.Nath.Nin(System.Threading.!nterlocked.!ncrement(i), i - 1)
End While
End Sub
End Class

: Output //
//header count = 6
//Cache-Control = private
//Content-Type = text/html
//Set-Cookie = PREF=!D=6ae22f++980c5d/8…
///JRA; expires=Sun, 1/-Jan-2038 19:1+:
//Server = GWS/2.1
//Transfer-Encoding = chunked
//Date = Wed, 23 Nov 2005 10:10:58 GNT
119
l.:l: : Uploading to Web Server
لا »ci. WebClient ¤a.رأ Nethods لا _Iإ تl.l..Iا _.oﺡ.I Web Server .هو :
1 - OpenWrite لl.رV مi>.ﺱ.و Stream Data لا _Iإ Web Server و ia. :Iذ
لا نا«.c ,.,o. URL لا _Ic ﻩ..l.آ i.,. يiIا ﺹ.Iاو .IoII Web Page نأ .>. la.L
لl.oآ .I. loآ مi>.ﺱ.و :IiI تl.ﺡV±Iا :Io. :
: # C
using System;
using System.!O;
using System.Net;
class OpenWrite _ method
{
public static void Nain ()
{
WebClient wc = new WebClient();
string data = "<h1>Welcome to Ny Page</h1>";
Stream strm = wc.OpenWrite("C:\\mypage.html");
StreamWriter sw = new StreamWriter(strm);
sw.WriteLine(data);
sw.Close();
strm.Close();
}
}

VB.NET:
!mports System
!mports System.!O
!mports System.Net

Class OpenWrite_method

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
Dim data As String = "<h1>Welcome to Ny Page</h1>"
Dim strm As Stream = wc.OpenWrite("C:\mypage.html")
Dim sw As StreamWriter = New StreamWriter(strm)
sw.WriteLine(data)
sw.Close()
strm.Close()
End Sub
End Class













120
2 – UploadData ع«.Iا _o ¤º«s±o تl.«.ﺡo _s.I مi>.ﺱ.و Byte لا _Iإ Web
Server ع«. يأ ׺ر lçIV> _o ×.L.ﺱ. :.ا ..a. اiهو ر«±Iا ﺹ.Iا _.o تl.l..Iا _o
لا _Iإ e,.cو «.i.sIا web server _Iإ lçI.«ﺡ. ia. Byte Array .I. loآ مi>.ﺱ.و
لl.oآ :

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

class UploadData _ Nethod
{
public static void Nain ()
{
WebClient wc = new WebClient();
string data = "This is The Text Before Converted it to Byte";
byte|| dataarray = Encoding.ASC!!.GetBytes(data);
wc.UploadData("C:\\mydata.txt", dataarray);
}
}

VB.NET:
!mports System
!mports System.Net
!mports System.Text

Class UploadData_Nethod

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
Dim data As String = "This is The Text Before Converted it to Byte"
Dim dataarray As Byte() = Encoding.ASC!!.GetBytes(data)
wc.UploadData("C:\mydata.txt", dataarray)
End Sub
End Class

3 - UploadFile لا _o .Io ׺,I د«..oIا eiه مi>.ﺱ.و Local Computer لا _Iإ
Web Host لl.oآ .I. loآ مi>.ﺱ.و اi> ماi>..Vا ¤Lﺱ. .هو :
: # C
using System;
using System.Net;

class UploadFile_Nethod
{
public static void Nain ()
{
WebClient wc = new WebClient();
wc.UploadFile("http://www.yoursite.com", "C:\\myfile.html");
}
}

121
VB.NET:
!mports System
!mports System.Net

Class UploadFile_Nethod

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
wc.UploadFile("http://www.yoursite.com", "C:\myfile.html")
End Sub
End Class

+ - UploadValues ׺,I مi>.ﺱ.و Collection تl.l..Iا _o لاو values lç. ¤ﺹl>Iا
..«Iا _Iإ Server لا _.«ﺡ. ia. :Iذو Collection _Iإ Byte Array ..,a.Iو
Collection سVîIا مi>.ﺱ. NamevalueCollection .º د«>«oIا Name Space
System.Collections.Specialized د«..oIا مi>.ﺱ. ﻩs.,a. ia.و add لا ¤ºl±V Collection
i.i> .. لl.oآ .I. loآ مi>.ﺱ.و :
: # C

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

class Uploadvalues_Nethod
{

public static void Nain ()
{
WebClient wc = new WebClient();
NamevalueCollection nvc = new NamevalueCollection();
nvc.Add("firstname", "Fadi");
nvc.Add("lastname", "Abdel-qader");
byte|| response =
wc.Uploadvalues("http://localhost/mypage.aspx", nvc);
Console.WriteLine(Encoding.ASC!!.GetString(response));
}
}

VB.NET:
!mports System
!mports System.Collections.Specialized
!mports System.Net
!mports System.Text

Class Uploadvalues_Nethod

Public Shared Sub Nain()
Dim wc As WebClient = New WebClient
Dim nvc As NamevalueCollection = New NamevalueCollection
nvc.Add("firstname", "Fadi")
122
nvc.Add("lastname", "Abdel-qader")
Dim response As Byte() =
wc.Uploadvalues("http://localhost/mypage.aspx", nvc)
Console.WriteLine(Encoding.ASC!!.GetString(response))
End Sub
End Class

l.ìl: : لا L8 loﺩa: ,.آVا g.æاuoìا HTTP Programming :

تls..L. ¤>o,. .º ءا,>Vا »هأ _o ء,>Iا اiه ,..a. Web Client Applications يiIاو
لا _o _آ ماi>..ا _c ﻩ.º ثiﺡ.. ف«. HttpWebRequest Class لا و
HttpWebResponse Class :

1 - ماﺩ>.aا HttpWebRequest Class :
لا _o ¤c«o>o _Ic سVîIا اiه ي«.ﺡ. Properties ..l.أ _î.. مi>.ﺱ. ..Iاو
لا تls..L. .º Web Client Applications _.o ءl..V :
1 - لا ¤.ﺹl> ماi>..ا Web Proxy : .c lç.º ر,o. ..Iاو لا نا« Proxy Server »ºرو
لا Port لا ×o _ola.Iا ×.L.ﺱ. _.ﺡ HTTP Web Requests .I> _o Proxy Server وأ
Firewall لا ..,a. »..و Proxy Server Prosperity لl.oآ .I. loآ :
: # C
using System;
using System.Net;

class ProxyServer_Property
{

public static void Nain ()
{
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(
"http://www.google.com");

WebProxy proxysrv = new
WebProxy("http://proxy1.server.net:8080");
hwr.Proxy = proxysrv;
}
}

VB.NET:
!mports System
!mports System.Net

Class ProxyServer_Property

Public Shared Sub Nain()
Dim hwr As HttpWebRequest =
CType(WebRequest.Create("http://www.google.com"), HttpWebRequest)
Dim proxysrv As WebProxy = New
WebProxy("http://proxy1.server.net:8080")
hwr.Proxy = proxysrv
End Sub
End Class
123
لا ¤.اi.Iا .º ف,a. HttpWebRequest Object ف,a. ». WebProxy Object _o
سVîIا webProxy لا نا«.c ﻩI i.ﺱ.و Proxy Server لا »ºرو Port ×.L.ﺱ. :Iذ ia.و
¤.ﺹl>Iا ماi>..l. .î>.وا يأ _Iإ eدl..إ Proxy ×.o> .º ةدlc ةد«>«o ن«î. ..Iا
HttpWebRequest Objects ..

2 - لا ماi>..ا HttpWebrequest لl.رV ..«Iا _Iإ تl.l.. Server لا ماi>..l.
Streams لl.oآ .I. loآ مi>.ﺱ.و :
: # C
HttpWebrequest hwr =
(HttpWebRequest)WebRequest.Create("http://localhost");
Stream strm = hwr.GetRequestStream();
StreamWriter sw = new StreamWriter(strm);
sw.WriteLine(data);

VB.NET:
Dim hwr As HttpWebrequest = CType(WebRequest.Create("http://localhost"),
HttpWebRequest)
Dim strm As Stream = hwr.GetRequestStream
Dim sw As StreamWriter = New StreamWriter(strm)
sw.WriteLine(data)

لا ..,a. ia. HttpWebRequest Object ..,a.. م«s. Stream Object لا ﻩI i.ﺱ.و
Request Stream د«..oIا لV> _o GetRequestStream .

2 - ماﺩ>.aا HttpWebResponse Class :

لا مi>.ﺱ. HttpWebResponse Object ..«Iا _o تl.l.. عl>رV Server لا _Iإ
Client د«..oIا مi>.ﺱ. ..ﺡ GetResponse د«..oIا و BeginGetResponse eiçI
نأ ى«. سد«..oIا eiه ¤s.Lو .º ق,º i>«. Vو ¤.IoaIا BeginGetResponse
,..a. asynchronous Method .


لا ي«.ﺡ. HttpWebResponse Object لا _o دic _Ic Properties .هو :

1 - CharacterSet : لا ع«. i.iﺡ.I مi>.ﺱ.و Character Set
2 - ContentEncoding : لا ¤.IoaI مi>.ﺱ.و encoding
3 - ContentLength : I مi>.ﺱ.و د,Iا »>ﺡ ¤º,ao
+ - ContentType : لا ع«. i.iﺡ.I Respone
5 - Cookies : لا ×o _ola.I Cookies .Io ءl..إ Vوأ .>. lçoاi>.ﺱIو Cookie غرlº
لl.oآ .I. loآ ﻩs.,a.و :

: # C
HttpWebRequest hwr =
(HttpWebRequest)WebRequest.Create(http://www.amazon.com);
hwr.CookieContainer = new CookieContainer();
لا _.º :Iذو HTTP Request .I. loآ ﻩ.Iإ ei.ﺱ. ». :
HttpWebResponse hwrsp = (HttpWebResponse)hwr.GetResponse();
hwrsp.Cookies = hwr.CookieContainer.GetCookies(hwr.RequestUri);



124
VB.NET:
Dim hwr As HttpWebRequest =
CType(WebRequest.Create("http://www.amazon.com"), HttpWebRequest)
hwr.CookieContainer = New CookieContainer

Dim hwrsp As HttpWebResponse = CType(hwr.GetResponse,
HttpWebResponse)
hwrsp.Cookies = hwr.CookieContainer.GetCookies(hwr.RequestUri)


6 - Headers : لا ¤º,aoI HTTP Headers
/ - LastModified : _.ia. ,>أ r.رl.و .ºو ﻩ.º ×>,.
8 - Method : لا .º مi>.ﺱ. ..Iاو د«..oIا ¤º,aoI HTTP Response
9 – ProtocolVersion : ¤º,aoI لا HTTP version
10 – ResponseUri : لا URL ب صl>Iا Server
11 – Server : لا ».ا ¤º,aoI Server
12 – StatusCode : لا ع«. ¤º,aoI Coding مi>.ﺱoا
13 – StatusDescription : عl>رV Text لا ¤Ilﺡ _Ic ي«.ﺡ. HTTP

































125
.4 5 : mming Web Services Progra

.º l..iﺡ. ء,>Iا لا ¤>o,. _c _.lﺱIا HTTP لا _.. _cls.Iا ¤.s.آ ﻩ.º l...و web
server لاو client اiه ,..a.و ء,>Iا ls.l. ﻩ.c l..iﺡ. loI _oîo ، ¤s.Lو ﺹ>I..
لا ماi>..ا web services لا _o ةدls..Vا ¤..lîo|. Nethods لl. ةد«>«oIا web
server .,Iا rol.,. _>اد لا ل«آ«.,. ماi>..l.و ن« SOAP ل رl±.>ا «هو Simple
Object Access Protocol لا _s. ».. Result لا _o web Services server لا _Iإ
Client لا _Iإ lçI.«ﺡ. ia. XNL - extensible Narkup Language ,.c _s.. ..ﺡ
لا ل«آ«.,. HTTP .çﺱ. «ه ﻩoاi>..ا _o فiçIاو ن«.,Iا زlç> _Iإ لا ل«ﺹو _ Data
لا _o web server لا _Iإ Client لا لV> _o firewalls ×.o> نأ ذإ ¤sI.>oIا تl...Iاو
لا ل«آ«.,. »ci. تlî..Iا تl... HTTP لا _Ic _oa. يiIاو Port 80 . ¤aI .I.>. Vو
لا XNL لا _c HTNL لا .º icا«sIا ﺱs. مi>.ﺱ. ذإ HTNL _o ¤c«o>o .هو
لا Elements لاو Attributes لا _.o > / <> < lç.Ic لl.oآو ,.آا ¤.و,o. ,.o.. _آV :

<myStuff>
<myName>FADI Abdel-qader</myName>
<myTelephone>+962796...</myTelephone>
<myEmail>fadiS22000@yahoo.com</myEmail>
<myAge>23</myAge>
<mySex>M</mySex>
</myStuff>

»..و ماi>..l. ... توiIا .º lç.lci..ا Name Spaces System.xml ».. ..ﺡ
د«..oIا ماi>..l. lç.ءا,º Load لا .º د«>«oIا XmlDocument Class .I. loآ :

: # C
using System.Xml;
// Then you can Read any XNL File as Below:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"C:\myinfo.xml");
XmlNodeList name = xDoc.GetElementsByTagName("myName");
XmlNodeList telephone = xDoc.GetElementsByTagName("myTelephone");
XmlNodeList email = xDoc.GetElementsByTagName("myEmail");
XmlNodeList age = xDoc.GetElementsByTagName("myAge");
XmlNodeList sex = xDoc.GetElementsByTagName("mySex");

NessageBox.Show(
"Name: " + name|0|.!nnerText +"\n"+
"Telephone: " + telephone|0|.!nnerText +"\n"+
"Email: "+ email|0|.!nnerText +"\n"+
"Age: "+ age|0|.!nnerText +"\n"+
"sex: "+ sex|0|.!nnerText +"\n"









126
VB.NET:
Dim xDoc As XmlDocument = New XmlDocument
xDoc.Load("C:\myinfo.xml")
Dim name As XmlNodeList = xDoc.GetElementsByTagName("myName")
Dim telephone As XmlNodeList =
xDoc.GetElementsByTagName("myTelephone")
Dim email As XmlNodeList = xDoc.GetElementsByTagName("myEmail")
Dim age As XmlNodeList = xDoc.GetElementsByTagName("myAge")
Dim sex As XmlNodeList = xDoc.GetElementsByTagName("mySex")
Nsgbox("Name: " + name(0).!nnerText + "" 8 Nicrosoft.visualBasic.Chr(10)
8 "" + "Telephone: " + telephone(0).!nnerText + "" 8
Nicrosoft.visualBasic.Chr(10) 8 "" + "Email: " + email(0).!nnerText + "" 8
Nicrosoft.visualBasic.Chr(10) 8 "" + "Age: " + age(0).!nnerText + "" 8
Nicrosoft.visualBasic.Chr(10) 8 "" + "sex: " + sex(0).!nnerText + "" 8
Nicrosoft.visualBasic.Chr(10) 8 "")

لا ماi>..ا ¤.Ioc ,o. web services .هو _ﺡا,o ¤.V.. :
1 - The web service server : ,.c تl.l..Iا لl.s..او لl.رإ ﻩIV> _o ».. يiIاو
لا ل«آ«.,. SOAP لا ماi>..l. !!S لاو ASP.NET .
2 - The proxy object : _I roﺱ. يiIاو Client .Iا لl.s..ا و لl.ر|. _Iإو _o تl.l.
لا web Services Server لا .º ﻩs.,a. ».. ..ﺡ HttpWebRequest لV> _o
سVîIا WebProxy .º ﻩ.... lo «هو ء,>Iا _.lﺱIا .
3 - The client application : لl. lçL.ر ».. ..Iاو ن«.,. ¤ﺹl>Iا ¤ç>ا«Iا «هو Web
Services Server
.Il.Iا _î.Iا .º loآ :













127
ءl..Vو web services server عو,.o _oa. م«s. Web Services ASP.NET i.i>
.ci.ﺱ.و Name Spaces System.Web.Services سVîIا ..ر«.. م«s. ».
WebService لl.oآ .I. loآو عو,.oII .ﺱ..,Iا سVîII :
: # C
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

|WebService(Namespace = "http://my_url.com/")|
|WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)|
public class Service : System.Web.Services.WebService
{
public Service () {}
|WebNethod|
public int Add(int a, int b)
{
return a + b;
}

}

VB.NET:
!mports System
!mports System.Web
!mports System.Web.Services
!mports System.Web.Services.Protocols

<WebService(Namespace="http://my_url.com/")> _
<WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)> _
Public Class Service
!nherits System.Web.Services.WebService

Public Sub New()
End Sub

<WebNethod()> _
Public Function Add(Byval a As !nteger, Byval b As !nteger) As !nteger
Return a + b
End Function
End Class










128
_..o.º لl.s..ا ».. ..ﺡ A و B ×o _IوVا ¤o.sIا ×o> r.l. عl>ر|. م«s. :Iذ ia.و
لا _Iإ ¤..l.Iا ¤o.sIا Client _î. _Ic XNL لا ل«آ«.,. ماi>..l. SOAP ,çL. loآو
.Il.Iا _î.Iا .º :



لا rol.,. ءl..Vو Client _Iإ _.lﺱIا سVîIا _.«ﺡ. Vوأ .>. Dll File رإ و لl. ﻩºlº
Client Resources .I. loآ ﻩoاi>..ا »..و :
: # C
using System;
class Client_side
{
public static void Nain(string|| argv)
{
Ny_main_class mm = new Ny_main_class();
int x = Convert.To!nt16(argv|0|);
int y = Convert.To!nt16(argv|1|);
int sum = mm.Add(x, y);
Console.WriteLine(sum);
}
}
}









129

VB.NET:
Class Client_side

Public Shared Sub Nain(Byval argv As String())
Dim mm As Ny_main_class = New Ny_main_class
Dim x As !nteger = Convert.To!nt16(argv(0))
Dim y As !nteger = Convert.To!nt16(argv(1))
Dim sum As !nteger = mm.Add(x, y)
Console.WriteLine(sum)
End Sub
End Class


لا .º تl..l.Vا l... اiîهو Web services ¤>ﺱ.Iا .º _.ﺹls.Iا ¤ºlآ i>. ف«.و
_o ¤.ºر«Iا بl.îIا ...



































130
5.5 File Transfer Protocol Programming – FTP :

لا تV«آ«.,. _o ,>أ ل«آ«.,. ح,.. l.ه أi.. ف«. Application Layer لا ل«آ«.,. «هو
FTP _.,.. ¤.Ioc .º ..l.أ _î.. مi>.ﺱ. يiIاو downloading ׺ر و
uploading و _o تlsIoIا لا _Iإ FTP Server تlî..Iا تl.>o,. .Icا .º ةدlaIlآو
د«>و _Ic io.a. ..Iا و Client/Server لا م«s. ..ﺡ Server لا _Ic ..±.. Port
_I ﺹ±>oIا FTP لا «هو Port 21 لا ماi>..l. TCP Connection Oriented
Protocol لا _s.. ..ﺡ Server لا _o .IL دور«I رlL..Vا ×±«. Client .|. ءl.
Session _sﺡ.Iا تl.Ioc ءا,>إ ia.و ﻩao Authentication ».. تl.ﺡV±Iا _o iآl.Iاو
لا »ºر i.iﺡ. ».. ..ﺡ ¤ﺱI>Il. ءi.Iا _Ic ¤sºا«oIا Port لl.s..ا ».. ف«. يiIاو
لا ,.c ن«.,Iا زlç> _Iإ لl.رVا »..و ﻩIV> _o تl.l..Iا Port 20 لا .º Server r±..و
ا .º loآ ¤.IoaIا eiه .Il.Iا _î.I :






¤b>Vo : لا ¤oi> _.as.I FTP ك كزlç> _oa. ..ﺡ. :.iI FTP Server Vوأ .>.
لا نأ _o iآl.Iا FTP Services لا ×o :.iI ¤...o !!S .Il.Iا _î.Iا .º ,çL. loآ و :

131


ب lçI.as. _o iآl.Iا ». _oو Services _o Control Panel ». Administrative Tools
. » Services .Il.Iا _î.Iا .º ,çL. loآو :



لا .º ﻩ.o iآl.Iا ». !!S .Il.Iا _î.Iا .º loآ ,çL. ..ﺡ. :

132



Vوأ : FTP Commands
_I ماi>..Vا و لl±.Vا ¤.Ioc ﻩ... FTP لا ¤.Ioc Telnet »ci. ..ﺡ ,..آ iﺡ _Iإ
لا ل«آ«.,. FTP oc lçIV> _o ».. ..Iاو ,oاوVا _o ¤c«o>o ×o .Ll>.Iا ¤.I
لا Server لا ×o وأ Remote Host .Il.Iا _î.Iا .º loآ ¤.IoaIا eiه r±.و :




لا »هV ح,. l.هو Commands FTP :

USER <username> 8 PASS
<password>
¤ﺱI>Iا ءl..V _sﺡ.Iا ¤.IoaI ¤.«ILo
RECv وأ RETR <filename> لا _o .Io _.,..I مi>.ﺱ.و Server ia.
ﺡ. .IoIا ».ا i.i
SEND وأ STOR <filename> لا _Iإ .IoIا ׺,I مi>.ﺱ.و Server ia.
.IoIا ».ا i.iﺡ.
TYPE <type indicator> ».. ..Iا تl.l..Iا ¤..ه وأ ¤a..L i.iﺡ.I
.I. loآو lçIs. :
-a ASC!! –e EBCD!C – ! for Binary
Data - L<Byte Size> »... يiIاو
133
ﻩIs.
PASv .I ءا«. ¤ﺱI>Iا ع«. i.iﺡ Passive وأ
Active لا ¤Ilﺡ .º ﻩ.ا ذإ Passive »..
׺ر وأ دورو ¤Ilﺡ .º Lsº لl±.Vا _.as.
لا _Iإ و _o .Io يأ Server .
Status وأ STAT و لl±.Vا ¤Ilﺡ ﺹﺡsI uploading 8
Downloading
Delete , cd , mkdir , rename … Iا .º ﻩ.Ic فرla.o «ه loآ .هو _ola.
لا مlL. .º تاiI>oIا و تlsIoIا ×o
DOS
Close وأ OU!T لا ×o ¤ﺱI>Iا ءlç.V Remote Host

l.:l: : لا go _olﻉ.ìا FTP ماﺩ>.al: u.: توﺩìا L8 COM Components :
لا ماi>..ا ... توiIا »ci. FTP ,.c !TC – !nternet Transfer Control ء,> «هو
لا _o CON Components Controls ءl..|. »º ءi.IIو New Windows Application
.Il.Iا _î.Iا .º loآ :



¤ºl±|. »º ». Name Spaces ¤.Il.Iا :
: # C
using System.!O;
using System.Reflection;


VB.NET:
imports System.!O
imports System.Reflection


لا _Iإ .Il.Iا د«îIا ¤ºl±إ ». Upload Button :
: # C
private void button1_Click(object sender, System.EventArgs e)
{
File!nfo thisFile = new File!nfo(tbFile.Text);
Type !TC;
object|| parameter= new object|2|;
object !TCObject;
!TC = Type.GetTypeFromProg!D("!netCtls.!net");
!TCObject = Activator.Create!nstance(!TC);
parameter|0| = (string)tbServer.Text;
134
parameter|1| = (string)"PUT " + thisFile.FullName + " /" +
thisFile.Name;
!TC.!nvokeNember("execute", BindingFlags.!nvokeNethod,null, !TCObject,
parameter);}

VB.NET:
Private Sub button1_Click(Byval sender As Object, Byval e As
System.EventArgs)
Dim thisFile As File!nfo = New File!nfo(tbFile.Text)
Dim !TC As Type
Dim parameter(2) As Object
Dim !TCObject As Object
!TC = Type.GetTypeFromProg!D("!netCtls.!net")
!TCObject = Activator.Create!nstance(!TC)
parameter(0) = CType(tbServer.Text, String)
parameter(1) = CType("PUT ", String) + thisFile.FullName + " /" +
thisFile.Name
!TC.!nvokeNember("execute", BindingFlags.!nvokeNethod, Nothing,
!TCObject, parameter)
End Sub

لا ..,a. ¤.اi.Iا .º ». !TC لا لV> _o Type Class _o± د«>«oIاو Name Space
System.Reflection l.º,c ». Array ع«.Iا _o Object ,.,o. .º lçoاi>..V :Iذو
لا و .IoIا ».ا FTP Server د«..oIا _Iإ !nvokeNember لا _o± ةد«>«oIاو !TC
Control Object ...
.IoIا i>. ف«. iI>oIا .º ﻩaºر »... يiIا :
C:\!netpub\ftproot





























135
l.ìl: : لا go _olﻉ.ìا FTP ماﺩ>.al: u.: توﺩìا L8 لا Web Class :
لا ¤>o,. _îo. FTP ماi>..l. web Class _o± ةد«>«oIاو Name Spaces
System.Net _ola.Iا .º loآ ﻩao _ola.Iا ¤.Ioc ﻩ...و لا ×o WebRequest لا و
webResponse Classes و لا ¤>o,. .º lçao l.Iola. ..Iا HTTP l..îo. ..ﺡ
لا ×o _ola.I lç.o ةدls..Vا FTP Protocol .I. loآ .هو :

- WebClient »cد ». ذإ dot net Framework 2 سVîIا ماi>..ا WebClient يiIاو
لا ×o _ola.Iا »ci. FTP .. يiIاو _o ﻩ.lci..ا » Name Spaces System.Net »..و
.I. loآ ﻩs.,a. :
: # C
using System;
using System.Net;

namespace Web_Client
{
class Program
{
public static void Nain(string|| args)
{
string filename = "ftp://ms.com/files/dotnetfx.exe";
WebClient client = new WebClient();
client.DownloadFile(filename, "dotnetfx.exe");

}

}
}

VB.NET:
!mports System
!mports System.Net
Namespace Web_Client

Class Program

Public Shared Sub Nain(Byval args As String())
Dim filename As String = "ftp://ms.com/files/dotnetfx.exe"
Dim client As WebClient = New WebClient
client.DownloadFile(filename, "dotnetfx.exe")
End Sub
End Class
End Namespace









136
- FtpRequestCreator مi>.ﺱ.و لا ×o _oaIا أi.و _.>ﺱ.I FTP .I. loآ ف,a.و :

: # C
using System;
using System.Net;

namespace FTP
{
public class FtpRequestCreator : !WebRequestCreate
{
public FtpRequestCreator()
{
}

public System.Net.WebRequest Create(System.Uri uri)
{
return new FtpWebRequest(uri);
}
}
}

VB.NET:
!mports System
!mports System.Net
Namespace FTP

Public Class FtpRequestCreator
!mplements !WebRequestCreate

Public Sub New()
End Sub

Public Function Create(Byval uri As System.Uri) As
System.Net.WebRequest
Return New FtpWebRequest(uri)
End Function
End Class
End Namespace

- FtpWebRequest _oaI مi>.ﺱ.و download or upload a file on an FTP server
.I. loآ lçs.,a. »..و :

: # C
using System;
using System.Net;

namespace FTP
{
public class FtpWebRequest : WebRequest
{
private string username = "Fadi";
137
internal string password = "fff";
private Uri uri;
private bool binaryNode = true;
private string method = "GET";

internal FtpWebRequest(Uri uri)
{
this.uri = uri;
}

public string Username
{
get { return username; }
set { username = value; }
}

public string Password
{
set { password = value; }
}

public bool BinaryNode
{
get { return binaryNode; }
set { binaryNode = value; }
}

public override System.Uri RequestUri
{
get { return uri; }
}

public override string Nethod
{
get { return method; }
set { method = value; }
}

public override System.Net.WebResponse GetResponse()
{
FtpWebResponse response = new FtpWebResponse(this);
return response;
}
}
}






138
VB.NET:
!mports System
!mports System.Net
Namespace FTP

Public Class FtpWebRequest
!nherits WebRequest
Private username As String = "Fadi"
Friend password As String = "fff"
Private uri As Uri
Private binaryNode As Boolean = True
Private method As String = "GET"

Friend Sub New(Byval uri As Uri)
Ne.uri = uri
End Sub

Public Property Username() As String
Get
Return username
End Get
Set(Byval value As String)
username = value
End Set
End Property

Public WriteOnly Property Password() As String
Set(Byval value As String)
password = value
End Set
End Property

Public Property BinaryNode() As Boolean
Get
Return binaryNode
End Get
Set(Byval value As Boolean)
binaryNode = value
End Set
End Property

Public Overloads Overrides ReadOnly Property RequestUri() As
System.Uri
Get
Return uri
End Get
End Property

Public Overloads Overrides Property Nethod() As String
Get
Return method
139
End Get
Set(Byval value As String)
method = value
End Set
End Property

Public Overloads Overrides Function GetResponse() As
System.Net.WebResponse
Dim response As FtpWebResponse = New FtpWebResponse(Ne)
Return response
End Function
End Class
End Namespace

- FtpWebResponse لا _.º _o د,Iا ¤.IoaI مi>.ﺱ.و Server .I. loآ lçs.,a. »..و :

: # C
using System;
using System.!O;
using System.Net;
using System.Net.Sockets;

namespace FTP
{
public class FtpWebResponse : WebResponse
{
private FtpWebRequest request;
private FtpClient client;

internal FtpWebResponse(FtpWebRequest request)
{
this.request = request;
}
}
}













140

VB.NET:
!mports System
!mports System.!O
!mports System.Net
!mports System.Net.Sockets
Namespace FTP

Public Class FtpWebResponse
!nherits WebResponse
Private request As FtpWebRequest
Private client As FtpClient

Friend Sub New(Byval request As FtpWebRequest)
Ne.request = request
End Sub
End Class
End Namespace

- FtpWebStream لا ..,a.I مi>.ﺱ.و Stream _s.Iا ¤.IoaI مi>.ﺱ. ف«. يiIاو
.I. loآ ..i.o _î.. ف,a.و :

: # C
using System;
using System.!O;
using System.Net.Sockets;

namespace FTP
{
internal class FtpWebStream : Stream
{
private FtpWebResponse response;
private NetworkStream dataStream;

public FtpWebStream(NetworkStream dataStream, FtpWebResponse
response)
{
this.dataStream = dataStream;
this.response = response;
}
}
}










141
VB.NET:
!mports System
!mports System.!O
!mports System.Net.Sockets
Namespace FTP

Friend Class FtpWebStream
!nherits Stream
Private response As FtpWebResponse
Private dataStream As NetworkStream

Public Sub New(Byval dataStream As NetworkStream, Byval response
As FtpWebResponse)
Ne.dataStream = dataStream
Ne.response = response
End Sub
End Class
End Namespace


ار lﻉ: : زlq> _o mlo g8,ì La..b: لl.o Client زlq> _ìإ Server ماﺩ>.al:
لا Stream لاو Socket :

_.>ol.,. ءl..|. م«s. ف«. ء,>Iا اiه .º Client / Server لا ×o _ola..و Stream
Library _Iإ .IoIا _.«ﺡ.. م«s. ف«. Byte Array لا ,.c ﻩIl.رإ و Stream
لا ماi>..l. Socket و TCP Connection ، لا وأ لl.رVl. صl>Iا ء,>Iا ¤>o,.Iو
Client i.i> عو,.o ءl..|. »º .Il.Iا _î.Iا .º loآ :



مi>.ﺱ. ف«. Name Spaces ¤.Il.Iا :
: # C
using System.!O;
using System.Net;
using System.Net.Sockets;
using System.Text;

VB.NET:
imports System.!O
imports System.Net
imports System.Net.Sockets
imports System.Text

142
لا .º Send Button .Il.Iا د«îIا ¤.l.î. »º :
: # C
try
{
Stream fileStream = File.OpenRead(textBox1.Text);
// Alocate memory space for the file
byte|| fileBuffer = new byte|fileStream.Length|;
fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
// Open a TCP Connection and send the data
TcpClient clientSocket = new TcpClient(textBox2.Text,8880);
NetworkStream networkStream = clientSocket.GetStream();
networkStream.Write(fileBuffer,0,fileBuffer.GetLength(0));
networkStream.Close();
}
catch (Exception ex){NessageBox.Show(ex.Nessage);}

VB.NET:
Try
Dim fileStream As Stream = File.OpenRead(textBox1.Text)
Dim fileBuffer(fileStream.Length) As Byte
fileStream.Read(fileBuffer, 0, CType(fileStream.Length, !nteger))
Dim clientSocket As TcpClient = New TcpClient(textBox2.Text, 8880)
Dim networkStream As NetworkStream = clientSocket.GetStream
networkStream.Write(fileBuffer, 0, fileBuffer.GetLength(0))
networkStream.Close
Catch ex As Exception
Nsgbox(ex.Nessage)
End Try

ب ﻩ..,>.و ﻩIl.رإ د«. يiIا .IoIا ةءا,s. ¤.اi.Iا .º l.oº Stream Object _.ﺡو
لا ,.c ﻩIl.رإ ×.L.ﺱ. Socket ع«.Iا _o ¤º«s±o _Iإ ﻩI.«ﺡ. _o i.V Byte l.oºو
ب ﻩ..oﺱ.. fileBuffer د«..oIا ماi>..l. ﻩ...a. ». Read _o± ةد«>«oIاو
fileStream :Iذ ia.و لا ×o لl±.ا ءl..|. l.oº Server لا ماi>..l. TCP Connection
لا تl.«.ﺡo لl.رإ ». ..ﺡ fileBuffer لا _Iإ Server ماi>..l. NetworkStream
Class ...

ء,> ¤>o,.Iو Server عو,.o ءl..|. »º ﻩ..,>.و .IoIا لl.s..ا _c ل«.ﺱoIا «هو
.Il.Iا _î.Iا .º ,çL. loآ i.i> :



143
مi>.ﺱ. ف«. Name Spaces ¤.Il.Iا :
: # C
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.!O;

VB.NET:
imports System.Threading
imports System.Net
imports System.Net.Sockets
imports System.Text
imports System.!O

». لا ¤ºl±إ Nethod ¤.Il.Iا lço.ا _î.Iو handlerThread .I. loآو :
: # C
public void handlerThread()
{
Socket handlerSocket = (Socket)alSockets|alSockets.Count-1|;
NetworkStream networkStream = new
NetworkStream(handlerSocket);
int thisRead=0;
int blockSize=102+;
Byte|| dataByte = new Byte|blockSize|;
lock(this)
{
// Only one process can access
// the same file at any given time
Stream fileStream = File.OpenWrite(@"c:\upload");

while(true)
{
thisRead=networkStream.Read(dataByte,0,blockSize);
fileStream.Write(dataByte,0,thisRead);
if (thisRead==0) break;
fileStream.Close();
}
lbConnections.!tems.Add("File Written");
handlerSocket = null;
}










144
VB.NET:
Public Sub handlerThread()
Dim handlerSocket As Socket = CType(alSockets(alSockets.Count - 1),
Socket)
Dim networkStream As NetworkStream = New
NetworkStream(handlerSocket)
Dim thisRead As !nteger = 0
Dim blockSize As !nteger = 102+
Dim dataByte(blockSize) As Byte
SyncLock Ne
Dim fileStream As Stream = File.OpenWrite("c:\upload")
While True
thisRead = networkStream.Read(dataByte, 0, blockSize)
fileStream.Write(dataByte, 0, thisRead)
!f thisRead = 0 Then
' break
End !f
fileStream.Close()
End While
lbConnections.!tems.Add("File Written")
handlerSocket = Nothing
End SyncLock
End Sub

r.sI :Iذو ةi.i> ى,>أ د«..o ¤.l.î. »º ». TCP Connection لا _Ic Port 8880
lç.Ic ..±.Iاو .±ا,.ºا «هو lço.ا _î.Iو listenerThread .I. loآو :

: # C
public void listenerThread()
{
TcpListener tcpListener = new TcpListener(8 8 80);
tcpListener.Start();
while(true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
lbConnections.!tems.Add(handlerSocket.RemoteEndPoint.ToString() +
" connected.");
lock (this)
{
alSockets.Add(handlerSocket);
}
ThreadStart thdstHandler = new
ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdstHandler);
thdHandler.Start();
}
}
}

145
VB.NET:
Public Sub listenerThread()
Dim tcpListener As TcpListener = New TcpListener(8880)
tcpListener.Start()
While True
Dim handlerSocket As Socket = tcpListener.AcceptSocket
!f handlerSocket.Connected Then
lbConnections.!tems.Add(handlerSocket.RemoteEndPoint.ToString +
" connected.")
SyncLock Ne
alSockets.Add(handlerSocket)
End SyncLock
Dim thdstHandler As ThreadStart = New ThreadStart(handlerThread)
Dim thdHandler As Thread = New Thread(thdstHandler)
thdHandler.Start()
End !f
End While
End Sub

rol.,.Iا _.a.. أi. ثiﺡ _Iإ .Il.Iا د«îIا ¤ºl±|. »º ». Form Load :


: # C
private void Form1_Load(object sender, System.EventArgs e)
{
!PHostEntry !PHost = Dns.GetHostByName(Dns.GetHostName());

lbConnections.Text = "Ny !P address is " +
!PHost.AddressList|0|.ToString();

alSockets = new ArrayList();

Thread thdListener = new Thread(new ThreadStart(listenerThread));
thdListener.Start();}

















146

VB.NET:
Private Sub Form1_Load(Byval sender As Object, Byval e As
System.EventArgs)
Dim !PHost As !PHostEntry = Dns.GetHostByName(Dns.GetHostName)
lbConnections.Text = "Ny !P address is " + !PHost.AddressList(0).ToString
alSockets = New ArrayList
Dim thdListener As Thread = New Thread(New
ThreadStart(listenerThread))
thdListener.Start()
End Sub



لا ماi>..l. Thread لا i.s.. ». listenerThread Nethod ..,a.. lç.º l.oº ..Iاو
لا tcpListener لا _Ic ﻩI.as.و Port 8880 l. .IL يأ ل«.º »... ..ﺡ لا _o ..
Client لا اiه _Ic Port د«..oIا ءlci..ا :Iذ ia.و handlerThread lç.º »... ..Iاو
لا لl.s..ا Stream Data .º lç..,>.و Byte Array نlîoIا .º lç..,>.و lç.ءا,º ».
لا ماi>..l.و دiﺡoIا fileStream.Write لا ﻩI l.ر,o ..ﺡ Stream _Ic ي«.ﺡ. يiIاو
.IoIا ».ا thisRead لاو dataByte Array ...

































147
Dot Net Networks 8 TCP/!P Programming
Chapter 6
Network Security Programming


Network Security Programming

Dot Net Security Namespaces Overview
1. Cryptography
2. Permission






















148
o.>,ìا _o>,ìا ¤ا oa:

.1 6 : Network Security Programming :

ل«>oIا ,.c ل«>iIا _o تl.l..Iا ¤.loﺡ. _oVا _o ة,îsIا ﺹ>I.. unauthorized
Access
lçoهأو ..Il.أ ةic ماi>..l. :

- Data Encryption 8 Decryption ,.s..Iا :ºو ,.s..Iا
- Authentications ¤.«ه _o _sﺡ.Iا ¤Il.,Iا _.,o ﺹ>.Iا
- Set Policies 8 Permissions تl.ﺡV±Iا و تl.l.ﺱIا i.s..و i.iﺡ.

×.o> ... توiIا .º .ocد ls.l. lهl.,آذ ..Iا ¤.loﺡIا ..Il.أ لا ماi>..l.
Security Namespaces ¤.c,sIا تl..îoIا _o ¤o>± ¤c«o>o _Ic ي«.ﺡ. ..Iاو
.Il.Iا _î.Iا .º loآ .هو




Vوأ : Overview Namespace ryptography C :

NET . Cryptography in : :ºو ,.s.. تl.Ioc ﺹ>. lo _î. ».ç. ..Iا ¤..îoIا .هو
_o تl.l..Iا ,.s.. Clear Text _Iإ Cipher Text ﺱîaIl.و ..l.أ _î.. مi>.ﺱ.و
مV..Vا i.c lه,.s.. :ºو لl.رVا ¤.Ioc _.º تl.l..Iا ,.s..I ، ».ﺱs. ×.L.ﺱ.و
ق,L .ه ¤.ﺱ..ر مlﺱºأ ¤.V. _Iإ lç.º ,.s..Iا :

A - Symmetric algorithms : _.lo.oIا ب«I.Vا ﻩ.اذ ي,ﺱIا حl.soIا مi>.ﺱ. ﻩ.ºو
,.s..Iا :ºو ,.s..Iا ¤.Ioc ءا,>V ¤a.,. ¤s.,L .هو ,.s..Iا :ºو ,.s..Iا ¤.IoaI
,.s..Iا ... توiIا .ocدو ¤I.lo.oIا ,.aIا ¤s.,Lآ ¤.ol .ﺱ.I lç.آ V _.lo.oIا
لا _o ¤c«o>o. Classes Algorithms .هو :

- لا ماi>..l. ,.s..Iا »ci. يiIا سVîIا DES-Data Encryption Standard :
DESCryptoServiceProvider
- »ci. يiIا سVîIا RC2 Algorithms : RC2CryptoServiceProvider
- »ci. يiIا سVîIا Algorithms Rijndael Nanaged : RijndaelNanaged

,.c lçIl.رإو ¤Il.,Iا ,.s.. .ه _.lo.oIا ب«I.Vl. ,.s..Iا .º ةدl.aoIا ¤s.,LIا
iºو اi> ¤.Ilc ن«î. iº ..Iا lL>Iا ¤.ﺱ. ن|º ¤s.,LIا eiه ماi>..l. _آV ¤î..Iا
تl.Ioc lç.Ic ى,>.و ق,ﺱ. iº وأ ¤Il.,Iا isº _Iإ يدå. loo تl.l..Iا eiه ¸a. iss.
ا :º ¤IوlﺡoI لا _o e,>ﺡ. iº ..Iا _.lçIا »>ﺡIا _c :.هl. ة,s..I Network
Bandwidth .. لا ى«.ﺱo _Ic ».. ,.s..Iا ¤.Ioc _a>. ¤Iî.oIا eiه _ﺡ ».و
Stream لا ¤.IoaIا eiçI مi>.ﺱ.و ﻩﺱs. CryptoStream Class ».. ..ﺡ
,.s..Iا حl.so ,.s..I _.ﺡl.so ماi>..ا Encryption Kay s..Iا :sI حl.soو ,. IV
!nstallation victor Decryption ¤.Ioc .º _.ﺡl.soIا ﺱs. ماi>..ا ط,...و
لا ×o _.lﺱIا سVîIا مi>.ﺱ.و ,.s..Iا :ºو ,.s..Iا MemoryStream وأ
FileStream لا ﻩI ر,o. ..ﺡ Stream Data ءا«. ,.s..Iا ع«.و DES وأ TripleDES وأ
149
RC2 ، لا مi>.ﺱ. ف«. لl.oآو TripleDES >.ذإ _o _.ﺡl.soIا Vآ ن«î.. نأ . 16
Bits ...

Symmetric Stream Encryption Example:

: # C
byte|| Key = {0x01, 0x02, 0x03, 0x0+, 0x05, 0x06, 0x0/, 0x08, 0x09,0x10,
0x11, 0x12, 0x13, 0x1+, 0x15, 0x16};
byte|| !v = {0x01, 0x02, 0x03, 0x0+, 0x05, 0x06, 0x0/, 0x08, 0x09,0x10,
0x11, 0x12, 0x13, 0x1+, 0x15, 0x16};

string phrase = msg.Text;
NemoryStream ms = new NemoryStream();

TripleDESCryptoServiceProvider tdes = new
TripleDESCryptoServiceProvider();
CryptoStream csw = new CryptoStream(ms,tdes.CreateEncryptor(Key, !v),
CryptoStreamNode.Write);

csw.Write(Encoding.ASC!!.GetBytes(phrase), 0, phrase.Length);
csw.FlushFinalBlock();

byte|| cryptdata = ms.GetBuffer();

textBox1.Text=Encoding.ASC!!.GetString(cryptdata, 0, (int)ms.Length);

VB.NET:
Dim Key As Byte() = {8H1, 8H2, 8H3, 8H+, 8H5, 8H6, 8H/, 8H8, 8H9,
8H10, 8H11, 8H12, 8H13, 8H1+, 8H15, 8H16}
Dim !v As Byte() = {8H1, 8H2, 8H3, 8H+, 8H5, 8H6, 8H/, 8H8, 8H9, 8H10,
8H11, 8H12, 8H13, 8H1+, 8H15, 8H16}

Dim phrase As String = msg.Text
Dim ms As NemoryStream = New NemoryStream()

Dim tdes As TripleDESCryptoServiceProvider = New
TripleDESCryptoServiceProvider()
Dim csw As CryptoStream = New CryptoStream(ms,
tdes.CreateEncryptor(Key, !v), CryptoStreamNode.Write)

csw.Write(Encoding.ASC!!.GetBytes(phrase), 0, phrase.Length)
csw.FlushFinalBlock()

Dim cryptdata As Byte() = ms.GetBuffer()

textBox1.Text=Encoding.ASC!!.GetString(cryptdata, 0, C!nt(ms.Length))





150
Symmetric Stream Decryption Example:

: # C
byte|| Keyy = {0x01, 0x02, 0x03, 0x0+, 0x05, 0x06, 0x0/, 0x08, 0x09,0x10,
0x11, 0x12, 0x13, 0x1+, 0x15, 0x16};
byte|| !vv = {0x01, 0x02, 0x03, 0x0+, 0x05, 0x06, 0x0/, 0x08, 0x09,0x10,
0x11, 0x12, 0x13, 0x1+, 0x15, 0x16};
ms.Position = 0;
byte|| data = new byte|102+|;
CryptoStream csr = new CryptoStream(ms,tdes.CreateDecryptor(Keyy,
!vv),CryptoStreamNode.Read);
int recv = csr.Read(data, 0, data.Length);
string newphrase = Encoding.ASC!!.GetString(data, 0, recv);
textBox1.Text=newphrase;

VB.NET:
Dim Keyy As Byte() = {8H1, 8H2, 8H3, 8H+, 8H5, 8H6, 8H/, 8H8, 8H9,
8H10, 8H11, 8H12, 8H13, 8H1+, 8H15, 8H16}
Dim !vv As Byte() = {8H1, 8H2, 8H3, 8H+, 8H5, 8H6, 8H/, 8H8, 8H9,
8H10, 8H11, 8H12, 8H13, 8H1+, 8H15, 8H16}
ms.Position = 0
Dim data As Byte() = New Byte(1023) {}
Dim csr As CryptoStream = New CryptoStream(ms,
tdes.CreateDecryptor(Keyy, !vv), CryptoStreamNode.Read)
Dim recv As !nteger = csr.Read(data, 0, data.Length)
Dim newphrase As String = Encoding.ASC!!.GetString(data, 0, recv)
textBox1.Text=newphrase

ماi>..l. ¤I.,oIا تl.l..Iا ,.s... ¤.اi.Iا .º م«s. تlî..Iا تl.>o,. .º _o يأ
_Iإ ة,s.oIا تl.l..Iا ل«ﺡ. ». ,.s..I ¤s.lﺱIا ..Il.Vا Stream لا ,.c lçIl.رV
Socket لا ماi>..l. Network Stream ، ¤Il.,Iا لl.s..l. _.s.ﺱoIا ف,LIا م«s. ».
لا ماi>..l. Network Stream لا ,.c Socket ، .ه loآ ن«î. ,.s..Iا :º ¤.Ioc
. ». ¤oi>.ﺱoIا ¤.oزرا«>Iا لا _Iإ ¤Il.,Iا _oﺡ memory stream .º ن,>.و Byte
Array .Il.Iا _î.Iا .º loآو ى,>أ ة,o ¤Il.ر _Iإ ل«ﺡ. نأ _îo. lهi.c :




151
B - Asymmetric algorithms : ب«I.Vا _o l.loأ ,.آأ «هو _.lo.o ,.aIا ب«I.Vا
مlc حl.so ماi>..l. تl.l..Iا ,s.. ذإ _.lo.oIا Public Kay ا :sIو مi>.ﺱ. ,.s..I
صl> حl.so Private Kay مi>.ﺱ.و _.ﺡl.soIا _.. ¤ºVc كl.ه ن«î.و 128 Bits
_.lo.o ,.aIا ,.s..Iا ... توiIا .ocدو تl.l..II ,.s..Iا ..Il.أ _±ºأ «هو ,.s..I
صl>Iا حl.soIا ,.s.. »ci. يiIاو Private Kay ماi>..l. Tow Algorithms Classes
.هو :
1- DSACryptoServiceProvider for Digital Signature Algorithm

¤.oº,Iا ×.ºا«.Iا : lç.o فiçIاو ¤Il.,Iا _.,o ﺹ>.Iا ¤.«ه _o _sﺡ.Iا م«s. لl.oآو
لا ماi>..l. ¤Il.,I ﺹ>Io i.I«.. _.,oIا Hash Function ,.s... م«s. :Iذ ia.و
مi>.ﺱ.. يiIاو صl>Iا حl.soIا _.«î.I ei.I«. ». يiIا ¤Il.,Iا ﺹ>Io .oºر ×.º«.آ
¤Il.,Iا ×o مlaIا حl.soIا _.,. ». _.,oII ، :s. م«s.º »I.ﺱoIl. _Ia.. lo. loأ
¤.oزرا«>Iا ﺱs. ماi>..l. :Iذ ».. نأ .>.و مlaIا حl.soIا ماi>..l. ﺹ>IoIا ,.s..
ﺹ>IoIا ,.s.. .º _.,oIا lça..ا ..Iا ، lهiIو ..Iا ¤Il.,Iا ﺹ>Io نlآ اذ|º
و ..Iا lçﺱs. .ه »I.ﺱoIا _.,o ﺹ>.Iا نأ _o _sﺡ.. lهi.c _.,oIا lهiI
ﻩﺱs. «ه ¤Il.,Iا .
ف«. ¤.اi.Iا .º _... instance لا _o DSACryptoServiceProvider حl.soIا i.I«.I
صl>Iاو مlaIا لا ن«î. ». Hash sign value .º ﻩ.,>.و Byte Array iI«. ﻩ±ﺡsIو
hash sign value .lﺱIl. ﻩ.رls.و i.i> _ lç.l.. اذ|º ﻩﺱs. «ه ﺹ>.Iا نأ ر,s. lهi.c
.I. loآو ¤I.,oIا ¤Il.,Iا .ﺡlﺹ :
: # C
using System;
using System.Security.Cryptography;

class DSACSPSample
{

static void Nain()
{
try
{
//Create a new instance of DSACryptoServiceProvider to generate
//a new key pair.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

//The hash value to sign.
byte|| Hashvalue =
{59,+,2+8,102,//,9/,1+2,201,210,12,22+,93,25,+1,100,19/,213,13+,130,135}
;
//The value to hold the signed value.
byte|| SignedHashvalue = DSASignHash(Hashvalue,
DSA.ExportParameters(true), "SHA1");

//verify the hash and display the results.
if(DSAverifyHash(Hashvalue, SignedHashvalue, DSA.ExportParameters(false),
"SHA1"))
{Console.WriteLine("The hash value was verified.");}
else
{Console.WriteLine("The hash value was not verified.");}}
catch(ArgumentNullException e)
{Console.WriteLine(e.Nessage);}
152
}
public static byte|| DSASignHash(byte|| HashToSign, DSAParameters
DSAKey!nfo, string HashAlg)
{
try
{
//Create a new instance of DSACryptoServiceProvider.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

//!mport the key information.
DSA.!mportParameters(DSAKey!nfo);

//Create an DSASignatureFormatter object and pass it the
//DSACryptoServiceProvider to transfer the private key.
DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSA);

//Set the hash algorithm to the passed value.
DSAFormatter.SetHashAlgorithm(HashAlg);

//Create a signature for Hashvalue and return it.
return DSAFormatter.CreateSignature(HashToSign);
}
catch(CryptographicException e)
{Console.WriteLine(e.Nessage);return null;}
}

VB.NET:
!mports System
!mports System.Security.Cryptography

Friend Class DSACSPSample

Shared Sub Nain()
Try
'Create a new instance of DSACryptoServiceProvider to generate
'a new key pair.
Dim DSA As DSACryptoServiceProvider = New
DSACryptoServiceProvider()

'The hash value to sign.
Dim Hashvalue As Byte() = {59, +, 2+8, 102, //, 9/, 1+2, 201, 210,
12, 22+, 93, 25, +1, 100, 19/, 213, 13+, 130, 135}
'The value to hold the signed value.
Dim SignedHashvalue As Byte() = DSASignHash(Hashvalue,
DSA.ExportParameters(True), "SHA1")

'verify the hash and display the results.
!f DSAverifyHash(Hashvalue, SignedHashvalue,
DSA.ExportParameters(False), "SHA1") Then
Console.WriteLine("The hash value was verified.")
Else
153
Console.WriteLine("The hash value was not verified.")
End !f
Catch e As ArgumentNullException
Console.WriteLine(e.Nessage)
End Try
End Sub
Public Shared Function DSASignHash(Byval HashToSign As Byte(), Byval
DSAKey!nfo As DSAParameters, Byval HashAlg As String) As Byte()
Try
'Create a new instance of DSACryptoServiceProvider.
Dim DSA As DSACryptoServiceProvider = New
DSACryptoServiceProvider()

'!mport the key information.
DSA.!mportParameters(DSAKey!nfo)

'Create an DSASignatureFormatter object and pass it the
'DSACryptoServiceProvider to transfer the private key.
Dim DSAFormatter As DSASignatureFormatter = New
DSASignatureFormatter(DSA)

'Set the hash algorithm to the passed value.
DSAFormatter.SetHashAlgorithm(HashAlg)

'Create a signature for Hashvalue and return it.
Return DSAFormatter.CreateSignature(HashToSign)
Catch e As CryptographicException
Console.WriteLine(e.Nessage)
Return Nothing
End Try
End Function


: # C
public static bool DSAverifyHash(byte|| Hashvalue, byte|| SignedHashvalue,
DSAParameters DSAKey!nfo, string HashAlg)
{
try
{
//Create a new instance of DSACryptoServiceProvider.
DSACryptoServiceProvider DSA = new DSACryptoServiceProvider();

//!mport the key information.
DSA.!mportParameters(DSAKey!nfo);

//Create an DSASignatureDeformatter object and pass it the
//DSACryptoServiceProvider to transfer the private key.
DSASignatureDeformatter DSADeformatter = new
DSASignatureDeformatter(DSA);

//Set the hash algorithm to the passed value.
154
DSADeformatter.SetHashAlgorithm(HashAlg);

//verify signature and return the result.
return DSADeformatter.verifySignature(Hashvalue, SignedHashvalue);
}
catch(CryptographicException e){Console.WriteLine(e.Nessage);return
false;}}}

VB.NET:
Public Shared Function DSAverifyHash)Byval Hashvalue As Byte () ، Byval
SignedHashvalue As Byte () ، Byval DSAKey!nfo As DSAParameters ،Byval
HashAlg As String (As Boolean
Try
'Create a new instance of DSACryptoServiceProvider.
Dim DSA As DSACryptoServiceProvider = New DSACryptoServiceProvider()

'!mport the key information .
DSA.!mportParameters(DSAKey!nfo(

'Create an DSASignatureDeformatter object and pass it the
'DSACryptoServiceProvider to transfer the private key.
Dim DSADeformatter As DSASignatureDeformatter = New
DSASignatureDeformatter(DSA(

'Set the hash algorithm to the passed value.
DSADeformatter.SetHashAlgorithm(HashAlg(

'verify signature and return the result .
Return DSADeformatter.verifySignature(Hashvalue, SignedHashvalue(
Catch e As CryptographicException
Console.WriteLine(e.Nessage(
Return False
End Try
End Function

















155
2- RSACryptoServiceProvider
,>إ .º مi>.ﺱ.و «هو _.lo.o ,.aIا ,.s..Iا :ºو ,.s..Iا ءا non inherited Class .º
ف«. ¤.اi.Iا _... instance i.i> لا _o RSACryptoServiceProvider i.I«.I :Iذو
:s. »I.ﺱoIا م«s. ». _oو ¤Il.,Iا ×o مlaIا حl.soIا _º,.و صl>Iاو مlaIا حl.soIا
.º loآ »..و صl>Iا حl.soIا ماi>..l. ¤Il.,Iا .Il.Iا _î.Iا :



لا ماi>..l. ,.s..Iا :ºو ,.s..Iا ¤s.,LI .ﺡ.±«. لl.o l.هو RSA Algorithm :
: # C
using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{

static void Nain()
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();

//Create byte arrays to hold original, encrypted, and decrypted data.
byte|| dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte|| encryptedData;
byte|| decryptedData;

//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
156
encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(false),
false);

//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true),
false);

//Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}",
ByteConverter.GetString(decryptedData));
}
catch(ArgumentNullException) {Console.WriteLine("Encryption failed.");}
}

VB.NET:
!mports System
!mports System.Security.Cryptography
!mports System.Text

Friend Class RSACSPSample

Shared Sub Nain()
Try
'Create a UnicodeEncoder to convert between byte array and string.
Dim ByteConverter As UnicodeEncoding = New UnicodeEncoding()

'Create byte arrays to hold original, encrypted, and decrypted data.
Dim dataToEncrypt As Byte() = ByteConverter.GetBytes("Data to
Encrypt")
Dim encryptedData As Byte()
Dim decryptedData As Byte()

'Create a new instance of RSACryptoServiceProvider to generate
'public and private key data.
Dim RSA As RSACryptoServiceProvider = New
RSACryptoServiceProvider()

'Pass the data to ENCRYPT, the public key information
'(using RSACryptoServiceProvider.ExportParameters(false),
'and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(dataToEncrypt,
RSA.ExportParameters(False), False)

'Pass the data to DECRYPT, the private key information
'(using RSACryptoServiceProvider.ExportParameters(true),
'and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData,
RSA.ExportParameters(True), False)

157
'Display the decrypted plaintext to the console.
Console.WriteLine("Decrypted plaintext: {0}",
ByteConverter.GetString(decryptedData))
Catch e1 As ArgumentNullException
Console.WriteLine("Encryption failed.")
End Try
End Sub

,.aa.: مua.a L.ìا دu..oìا ¿a.: ¤ìla,ìا :
: # C
static public byte|| RSAEncrypt(byte|| DataToEncrypt, RSAParameters
RSAKey!nfo, bool DoOAEPPadding)
{
try{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//!mport the RSA Key information. This only needs
//to include the public key information.
RSA.!mportParameters(RSAKey!nfo);

//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Nicrosoft Windows XP or
//later.
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch(CryptographicException e){Console.WriteLine(e.Nessage);return null;}
}

VB.NET:
Shared Public Function RSAEncrypt)Byval DataToEncrypt As Byte () ، Byval
RSAKey!nfo As RSAParameters ،Byval DoOAEPPadding As Boolean (As Byte()
Try
'Create a new instance of RSACryptoServiceProvider.
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider()
'!mport the RSA Key information. This only needs
'to include the public key information.
RSA.!mportParameters(RSAKey!nfo(
'Encrypt the passed byte array and specify OAEP padding .
'OAEP padding is only available on Nicrosoft Windows XP or
'later .
Return RSA.Encrypt(DataToEncrypt, DoOAEPPadding(
'Catch and display a CryptographicException
'to the console.
Catch e As CryptographicException
Console.WriteLine(e.Nessage(
Return Nothing
End Try
End Function
158

: مua.a L.ìا دu..oìا ¿a.: Oa ,.aa: ¤ìla,ìا :

: # C
static public byte|| RSADecrypt(byte|| DataToDecrypt, RSAParameters
RSAKey!nfo,bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//!mport the RSA Key information. This needs
//to include the private key information.
RSA.!mportParameters(RSAKey!nfo);

//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Nicrosoft Windows XP or
//later.
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch(CryptographicException e){Console.WriteLine(e.ToString());return null;}
}}

VB.NET:
Shared Public Function RSADecrypt)Byval DataToDecrypt As Byte () ، Byval
RSAKey!nfo As RSAParameters ،Byval DoOAEPPadding As Boolean (As Byte()
Try
'Create a new instance of RSACryptoServiceProvider.
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider()
'!mport the RSA Key information. This needs
'to include the private key information.
RSA.!mportParameters(RSAKey!nfo(

'Decrypt the passed byte array and specify OAEP padding .
'OAEP padding is only available on Nicrosoft Windows XP or
'later .
Return RSA.Decrypt(DataToDecrypt, DoOAEPPadding(
'Catch and display a CryptographicException
'to the console.
Catch e As CryptographicException
Console.WriteLine(e.ToString(()
Return Nothing
End Try
End Function





159
C - Hashing algorithms : «هو ذإ تl.l..Iا ,.s..I ¤.>o,.Iا ..Il.Vا ى«ºأ
ﻩ.º مi>.ﺱ. algorithm 512 bits _±ºأ iﺡآ _o Vi. 128 bits ماi>..l. Nessage
Digest Algorithms NAC _Iإ lçcl>رإو ¤Il.,Iا ,.s.. :º ×.L.ﺱ. _I l.هو lç.Ilﺡ
لا i.I«.I ..l.أ _î.. مi>.ﺱ.و ¤s.lﺱIا Passwords ¤.oº,Iا ×.ºا«.Iا i.I«. .ºو
Digital Signature رو,oIا ¤oIآ _.,>.I مi>.ﺱ. تVlﺡIا .Icا .ºو Password لا .º
Database _oا _î.. .
لا مi>.ﺱ.و SHA1Nanaged لا و SHA256Nanaged لاو SHA38+Nanaged لاو
SHA512Nanaged I ..,a. Hash Object لا مi>.ﺱ. ﻩ.oو ComputeHash Nethod
i.I«.I لا hash code .º ﻩ..,>.و byte Array .I. loآو لl.oآ :
: # C
SHA1Nanaged shaN1 = new SHA1Nanaged ();
byte|| my_kay1= ASC!!Encoding.ASC!!.GetBytes("convert this text to hash
code");
byte|| hashed_kay1 = shaN1.ComputeHash(my_kay1);
NessageBox.Show(ASC!!Encoding.ASC!!.GetString(hashed_kay1));

SHA256Nanaged shaN2 = new SHA256Nanaged();
byte|| my_kay2= ASC!!Encoding.ASC!!.GetBytes("convert this text to hash
code");
byte|| hashed_kay2 = shaN2.ComputeHash(my_kay2);
NessageBox.Show(ASC!!Encoding.ASC!!.GetString(hashed_kay2));

SHA38+Nanaged shaN3 = new SHA38+Nanaged ();
byte|| my_kay3= ASC!!Encoding.ASC!!.GetBytes("convert this text to hash
code");
byte|| hashed_kay3 = shaN3.ComputeHash(my_kay3);
NessageBox.Show(ASC!!Encoding.ASC!!.GetString(hashed_kay3));

SHA512Nanaged shaN+ = new SHA512Nanaged ();
byte|| my_kay+= ASC!!Encoding.ASC!!.GetBytes("convert this text to hash
code");
byte|| hashed_kay+ = shaN+.ComputeHash(my_kay+);
NessageBox.Show(ASC!!Encoding.ASC!!.GetString(hashed_kay+));


VB.NET:
Dim shaN1 As SHA1Nanaged = New SHA1Nanaged
Dim my_kay1 As Byte() = ASC!!Encoding.ASC!!.GetBytes("convert this text to
hash code")
Dim hashed_kay1 As Byte() = shaN1.ComputeHash(my_kay1)
Nsgbox(ASC!!Encoding.ASC!!.GetString(hashed_kay1))
Dim shaN2 As SHA256Nanaged = New SHA256Nanaged
Dim my_kay2 As Byte() = ASC!!Encoding.ASC!!.GetBytes("convert this text to
hash code")
Dim hashed_kay2 As Byte() = shaN2.ComputeHash(my_kay2)
Nsgbox(ASC!!Encoding.ASC!!.GetString(hashed_kay2))
Dim shaN3 As SHA38+Nanaged = New SHA38+Nanaged
Dim my_kay3 As Byte() = ASC!!Encoding.ASC!!.GetBytes("convert this text to
hash code")
Dim hashed_kay3 As Byte() = shaN3.ComputeHash(my_kay3)
Nsgbox(ASC!!Encoding.ASC!!.GetString(hashed_kay3))
160
Dim shaN+ As SHA512Nanaged = New SHA512Nanaged
Dim my_kay+ As Byte() = ASC!!Encoding.ASC!!.GetBytes("convert this text to
hash code")
Dim hashed_kay+ As Byte() = shaN+.ComputeHash(my_kay+)
Nsgbox(ASC!!Encoding.ASC!!.GetString(hashed_kay+))

l.:l: : Overview Namespace Permission :

لا »ci.و Permission Namespace لا .هو تl.ﺡV±Iا _o عا«.أ ¤.V. ... توiIا .º
Socket permissions لاو !dentity Permissions لاو Role- based permissions ...

Socket Permission : و لا ماi>..ا تl.ﺡVﺹ i.iﺡ. _o :.îo. Socket .º
ماi>..l. تlî..Iا تl.>o,. SocketPermission و SocketPermissionAttribute _o±
لا System.Net لاو System.Security.Permissions Namespaces ×.L.ﺱ. لl.oآو
×.o Client Host Address لا ×o لl±.Vا _o _.ao Listener Application ، :Iذ »..و
..,a.. Attribute SocketPermission لاو ¤.IoaIا ع«. lç.º دiﺡ. Access Kind نا«.c و
لا Host لا ﻩ.Ic _.L.. يiIا Permission لا »ºرو Port لا ع«.و Transport ءا«.
ﻩ>«o ,.c وأ ﻩ>«o TCP وأ UDP .

لl.oIا اiه .º i.,. لا لl±.ا ×.o 12/.0.0.1 Address loopback لl. Socket ×.o> ,.c
لا Ports لا ع«. _c ,L.Iا ¸a.و Socket مi>.ﺱoIا .

|SocketPermission(SecurityAction.Deny, Access="Connect",
Host="12/.0.0.1",Port="All", Transport="All")|


لا l..îo. object SecurityAction .I. loآو lهi.,. ..Iا ¤.IoaIا ع«. i.iﺡ. _o :



Assert : حloﺱIا ..a.و Client Host ةدiﺡo ¤.Ioc ءا,>إ _o _.ao
Demand : ..a.و لا ×.o> _Ic تl.ﺡV±Iا _..L. Classes لا ¤sL.o .º ×s. ..Iا
Stack لا _Icأ Defined Abstract
Deny : لا ×.o ..a.و Client Host ¤..ao ¤.Ioc ءا,>إ _o .
InheritanceDemand : لا _Ic تl.ﺡV±Iا _.L. lç.ºو Class لا ث,.. يiIا
Class .IlﺡIا .
PermitOnly : لا ×.o> ×.o. ﻩ.ºو Access لا اic Client User دiﺡoIا .
...





161
لا .ºو Access property loه _.رl.> i>l.و حloﺱIا وأ ×.oIا ¤.Ioc ع«. دiﺡ. :

Accept ل حloﺱIا وأ ×.oI Client Socket _oc _o Binding لا ×o !P Address لا و
Port دiﺡoIا .
Connect ل حloﺱIا وأ ×.oI Client Socket _oc _o connect لا ×o Remote Host
دiﺡoIا .

لا .º Host لاو Port لا نا«.c دiﺡ. Host لا ﻩ.Ic _.L.. يiIا Permission »ºر و
لا Port lç. _±.. ..Iا ) لا .º Port property ¤oIآ ,.,o. ×.L.ﺱ. all _Ic ¤IViI
لا ×.o> _Ic ¤.ﺡV±Iا _..L. Ports (

لا دiﺡ. ا,.>أو Transport property . ..Iاو لا ع«. lç.º ف,a. Socket مi>.ﺱoIا
¤.Il.Iا تارl.>Iا i>l.و :
All لا ع«. i.iﺡ. نوi. Socket لا eiه _.L. ذإ Permession لا ×.o> _Ic Socket
Types .
Connectionless لا ..lآ اذإ Socket مi>.ﺱ. Datagram Protocols لl.oآو
ل«آ«.و,. UDP .
ConnectionOriented لا ..lآ اذإ Socket مi>.ﺱ. Oriented Protocols لl.oآو
ل«آ«.و,. TCP .
TCP ×.L.ﺱ. ذإ ة,.l.o ei.iﺡ. .
UDP ة,.l.o ei.iﺡ. ×.L.ﺱ. ذإ .


لا _.ﺹla: ¤8lآ ح,a _lc L:Ì: فua Permission Namespace لا ¤.a:و
Security Namespaces بl.Sìا _o ¤.8رuìا ¤>a.ìا L8 .













162





¿-~ا:--او ت'-'~Vا ó- :----ا -=- ف:ﺱ
ª--ر:-ا ª=~--ا ¸- ة-ی-=-ا ، ¸- ا--أ دد:-- V
'+--= ...














Copyrighted to: Mr. FADI Abdel-qader, Jordan
Fadi822000@yahoo.com
http:]]spaces.msn.com]members]csharp2005



The End of ebook

ª-ﻥو:---Vا ª=~--ا ªی'+ﻥ

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->