Académique Documents
Professionnel Documents
Culture Documents
..
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 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
28/07/55
Dr. M. Munlin
C# Socket programming
28/07/55
1.
2.
3.
4.
5.
C# Socket programming
28/07/55
Dr. M. Munlin
C# Socket programming
28/07/55
28/07/55
C# Socket programming
28/07/55
Dr. M. Munlin
C# Socket programming
10
28/07/55
28/07/55
C# Socket programming
11
Creating TcpListener
1.
2.
3.
Dr. M. Munlin
C# Socket programming
12
28/07/55
28/07/55
C# Socket programming
13
28/07/55
Dr. M. Munlin
C# Socket programming
14
28/07/55
28/07/55
C# Socket programming
15
Assignment#4
28/07/55
Dr. M. Munlin
C# Socket programming
16
28/07/55
28/07/55
C# Socket programming
17
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
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