Vous êtes sur la page 1sur 12

28/07/55

Socket Helper Classes

..
Faculty of IST, MUT
Email: mmunlin@gmail.com

Objective
In this lecture you will learn:
TcpClient
TcpListener
UdpClient
Transmitting a complex object

28/07/55

Dr. M. Munlin

C# Socket programming

28/07/55

C# Socket Helper Classes

The .NET Framework supports the normal socket interface


for advanced network programmers.
The simplified socket helper classes help network
programmers create socket programs with simpler
statements and less coding.
There are three helper classes used for socket
programming:
1. TcpClient
2. TcpListener
3. UdpClient
The TcpClient and TcpListener classes are used for creating
TCP client and server programs.
The UdpClient class is used for creating UDP programs.
28/07/55

C# Socket programming

TcpClient Class
The TcpClient class is used to create client connectionoriented network model.
There are three ways to create a TcpClient object and
connect it to a remote host.
1. The default constructor format creates a socket on any
available local port.
The Connect() method connects to a specified remote host:
TcpClient newclient = new TcpClient();
newclient.Connect("www.isp.net", 8000);
The TcpClient class will automatically attempt to resolve the
hostname to the proper IP address.

28/07/55

Dr. M. Munlin

C# Socket programming

28/07/55

TcpClient Constructor
2. A specific EndPoint object: specify a specific local
EndPoint object to use when creating the socket:
IPAddress ia = Dns.GetHostByName(
Dns.GetHostName()).AddressList[0];
IPEndPoint iep = new IPEndPoint(ia, 10232);
TcpClient newclient2 = new TcpClient(iep);
newclient2.Connect("www.isp.net", 8000);
3. A specific remote host: specify the remote host address
and port to connect, removing the need to use the Connect()
method:
TcpClient newclient3 = new TcpClient("www.isp.net", 8000);

28/07/55

C# Socket programming

The TcpClient Methods and Properties

28/07/55

Dr. M. Munlin

C# Socket programming

28/07/55

Sending and Receiving Data

1.
2.
3.
4.
5.

Once, TcpClient object is created, call its Connect() method


to connect to a remote device (or server).
Once the device is connected, create a network stream
object using the GetStream() method.
After the NetworkStream object is created, use the Read()
and Write() methods to receive and send data.
TcpClient server = new TcpClient();
server.Connect("www.ispnet.net", 8000);
NetworkStream ns = server.GetStream();
int recv = ns.Read(data, 0, data.Length); // from server
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
28/07/55

C# Socket programming

A Simple TcpClient Program

28/07/55

Dr. M. Munlin

C# Socket programming

28/07/55

A Simple TcpClient Program

28/07/55

C# Socket programming

The TcpListener Class


TcpListener class provides a simplified way to create TCP
server applications.
The TcpListener class has three constructor formats.
1. TcpListener(int port) binds to a specific local port
number.
2. TcpListener(IPEndPoint ie) binds to a specific local
EndPoint object (IPAddress and port).
3. TcpListener(IPAddress addr, int port) binds to a
specific local IPAddress object and port number.
The TcpListener class constructor requires at least a port
number to listen for new connections.
If the server machine has multiple network cards, we can
use an IPEndPoint object to specify the IP address and port
of each card.

28/07/55

Dr. M. Munlin

C# Socket programming

10

28/07/55

The TcpListener Methods

28/07/55

C# Socket programming

11

Creating TcpListener

1.
2.
3.

The steps to create a TcpListener object and listen for incoming


connections goes like this:
TcpListener server = new
TcpListener(IPAddress.Parse("127.0.0.1"), 9050);
server.Start();
TcpClient newclient = server.AcceptTcpClient();
Start() binds the socket to the endpoint defined in the TcpListener
constructor and listen to accept new connections.
AcceptTcpClient() accepts incoming connection attempts and
assigning them to a TcpClient object.
After the TcpClient object is created, all communication with the
remote device is performed with the new TcpClient object.
The TcpListener object can thus be used to accept other
connections and pass them to other TcpClient objects.
Stop() close the TcpListener object: server.Stop();
28/07/55

Dr. M. Munlin

C# Socket programming

12

28/07/55

A Simple TcpListener Program

28/07/55

C# Socket programming

13

A Simple TcpListener Program

28/07/55

Dr. M. Munlin

C# Socket programming

14

28/07/55

Sending and Receiving Data


Create a network stream object using GetStream() and
use Read() and Write() to send and receive data.
NetworkStream ns = client.GetStream();
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
ns.Write(data, 0, data.Length);

28/07/55

C# Socket programming

15

Assignment#4

Modify the TcpClient and TcpListener program to


send and receive data using StreamReader and
StreamWriter and write all the communication to
a log file on a server.

28/07/55

Dr. M. Munlin

C# Socket programming

16

28/07/55

The UdpClient Class


The UdpClient helper class class makes a UDP programs
simpler for network programmers.
The UdpClient class has four formats of constructors:
1. UdpClient() creates a new UdpClient instance not
bound to any specific address or port.
2. UdpClient(int port) binds the new UdpClient object
to a specific UDP port number.
3. UdpClient(IPEndPoint iep) binds the new
UdpClient object to a specific local IP address and port
number.
4. UdpClient(string host, int port) binds the new
UdpClient object to any local IP address and port and
associates it with a specific remote IP address and port.

28/07/55

C# Socket programming

17

The UdpClient Methods

28/07/55

Dr. M. Munlin

C# Socket programming

18

28/07/55

Send()

1.

2.
3.

The Send() method has three formats that can send data to
a remote host:
Send(byte[ ] data, int sz) sends the byte array data of size
sz to the default remote host. To use this format, specify a
default remote UDP host using either UdpClient
constructor, or the Connect() method:
UdpClient host = new UdpClient("127.0.0.1", 9050);
Send(byte[ ] data, int sz, IPEndPoint iep) sends the byte
array data of size sz to the remote host specified by iep.
Send(byte[ ] data, int sz, string host, int port) sends the
byte array data of size sz to the host host at port port.

28/07/55

C# Socket programming

19

Receive()
The UdpClient uses the Receive() method to accept UDP
packets on the specified interface and port.
byte[ ] Receive(ref IPEndPoint iep) accepts UDP packets on
the IP address and UDP port specified by the UdpClient
constructor.
The data received from the socket is not placed in a byte
array within the method call. It is returned by the method.
We must specify an empty byte array for the received data.
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
data = client.Receive(ref sender);
Receive()uses an IPEndPoint to extract the IP address and
UDP port number of the remote host.

28/07/55

Dr. M. Munlin

C# Socket programming

20

10

28/07/55

28/07/55

C# Socket programming

21

The UdpServer Program


The UdpSrvrSample program creates a UdpClient object
from an IPEndPoint object, specifying any IP address on the
server and using UDP port 9050.
The program immediately waits for an incoming UDP
packet from any remote client, using the Receive() method:
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
data = newsock.Receive(ref sender);
The sender variable stores the IP information of the client,
which is then used for sending messages back to the client:
client.Send(data, data.Length, sender);
The UdpClient object does not know the IP information of
the destination host, we must specify in Send() method.
Note that, the data-byte array does not need to be reset to
its full length after every Receive() method.

28/07/55

Dr. M. Munlin

C# Socket programming

22

11

28/07/55

Dr. M. Munlin

28/07/55

C# Socket programming

23

28/07/55

C# Socket programming

24

12

Vous aimerez peut-être aussi