Socket Programming Laboratory 03 Socket Class Socket class
Socket Programming Laboratory - 03
Socket Class � 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 Protocol. Type enumeration. � Address. Family ◦ Specifies the addressing scheme that an instance of the Socket class can use. (i. e. Inter. Network for IPv 4 address, Inter. Network. V 6 for IPv 6 address, etc. )
Socket Class � Socket. Type Enumeration ◦ Specifies the type of socket an instance of the Socket class represents. ◦ Socket. Type will sometimes implicitly indicate which Protocol. Type will be used within an Address. Family. ◦ For example, when the. Socket. Type is Dgram, the Protocol. Type is always UDP. When the Socket. Type is Stream, the Protocol. Type is always TCP. If you try to create a Socket with an incompatible combination, Socket will throw a Socket. Exception. � Protocol. Type Enumeration ◦ Specifies the protocols that the Socket class supports. For example, IP, TCP, IPv 6, etc.
IPEnd. Point Class � IPEnd. Point Class ◦ Represents a network endpoint as an IP address and a port number.
Echo Server & Echo Client � 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
Exercise - 1 � Modify the echo server program that will reply back the same sent by client but in uppercase.
Byte Ordering � 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:
Why need to Convert? � 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 bigendian to little-endian
Example (Long. End. Point. cs) � IPEnd. Point Class ◦ Represents a network endpoint as an IP address and a port number. � IPAddress. Host. To. Network. Order Method ◦ Converts a value from host byte order to network byte order. � Pnt. Address ◦ Gets or sets the IP address of the endpoint.
Exercise - 2 � Write a program that will convert from littleendian to big-endian.
Host IP address � Example: Host. IP. cs � Dns. Get. Host. Name() Method ◦ Gets the host name of the local computer � Dns. Get. Host. Entry(String) ◦ Resolves a host name or IP address to an IPHost. Entry instance.
Exercise - 3 �A machine could have several IP addresses. Write a program that will display your own IP address.
IPLookup. cs � Get IP addresses from Domain Name.
Getting client’s IP addresses � Welcome. Server. cs � Welcome. Client. cs
Exercise - 4 � Modify the Welcome. Client. cs program that client will look for (search) the server IP address and get connected.