Socket Programming
Laboratory - 03
Socket class
◦ provides a rich set of methods and properties for network communications. The Socket class allows you to perform both synchronous and
asynchronous data transfer using any of the communication protocols listed in
the ProtocolType enumeration.
AddressFamily
◦ Specifies the addressing scheme that an instance of the Socket class can use. (i.e. InterNetwork for IPv4 address, InterNetworkV6 for IPv6 address,
etc.)
Socket Class
SocketType Enumeration
◦ Specifies the type of socket an instance of the Socket class represents.
◦ SocketType will sometimes implicitly indicate
which ProtocolType will be used within an AddressFamily.
◦ For example, when theSocketType is Dgram, the ProtocolType is always UDP. When
the SocketType is Stream, the ProtocolType is always TCP. If you try to create a Socket with an incompatible combination, Socket will throw
a SocketException.
ProtocolType Enumeration
◦ Specifies the protocols that the Socket class supports. For example, IP, TCP, IPv6, etc.
Socket Class
IPEndPoint Class
◦ Represents a network endpoint as an IP address and a port number.
IPEndPoint Class
Echo server and echo client are the socket programming equivalence of Hello World program found in typical programming
example. The main objective of echo server is to reply back (echo) the same message sent by client.
Download and Run the program
Echo Server & Echo Client
Modify the echo server program that will reply back the same sent by client but in uppercase.
Exercise - 1
Big-endian to Little-endian
Big-endian – Most significant byte first
Little-endian – Least significant byte first
To convert an IP address to integer, break it into four octets. For example:
Byte Ordering
Big-endian
◦ IP uses big-endian in network, also known as network order
Little-endian
◦ Host system running on windows operating system uses little-endian
Therefore, we need to convert from big- endian to little-endian
Why need to Convert?
IPEndPoint Class
◦ Represents a network endpoint as an IP address and a port number.
IPAddress.HostToNetworkOrder Method
◦ Converts a value from host byte order to network byte order.
Pnt.Address
◦ Gets or sets the IP address of the endpoint.
Example (LongEndPoint.cs)
Write a program that will convert from little- endian to big-endian.
Exercise - 2
Example: HostIP.cs
Dns.GetHostName() Method
◦ Gets the host name of the local computer
Dns.GetHostEntry(String)
◦ Resolves a host name or IP address to an IPHostEntry instance.
Host IP address
A machine could have several IP addresses.
Write a program that will display your own IP address.
Exercise - 3
Get IP addresses from Domain Name.
IPLookup.cs
WelcomeServer.cs
WelcomeClient.cs
Getting client’s IP
addresses
Modify the WelcomeClient.cs program that client will look for (search) the server IP
address and get connected.