networking and socket
TRANSCRIPT
![Page 1: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/1.jpg)
NetworkingIn the world of computers, networking is
the practice of linking two or more computing devices together for the purpose of sharing data. Networks are built with a mix of computer hardware and computer
software.
![Page 2: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/2.jpg)
• A network consists of two or more computers that are linked in order to share resources (such as printers and CDs), exchange files, or allow electronic communications.
• The computers on a network may be linked through cables, telephone lines, radio waves, satellites, or infrared light beams.
• common types of networks :– Local Area Network(LAN)–Wide Area Network(WAN)–Metropolitan area Network(MAN)
![Page 3: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/3.jpg)
Advantages
• User access control , Information storing and sharing, Connections ,Services, Internet, sharing resources, Flexible Access.
Disadvantages
• Expensive to Install, Requires Administrative Time, Servers Fail, Cables May Break, Security
![Page 4: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/4.jpg)
• A computer network consists of machines interconnected by communication channels.
• We call these machines hosts or routers . • Hosts are computers that run applications such as your Web browser, the application programs running on hosts are really the users of the network.
• Routers are machines whose job is to relay or forward information from one communication channel to another.
![Page 5: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/5.jpg)
• By information we here mean a sequences of bytes that are constructed and interpreted by programs.
• In the context of computer networks these byte sequences are generally called packets .
• A protocol is an agreement about the packets exchanged .
• TCP is designed to detect and recover from the losses, duplications, and other errors that may occur in the host-to-host channel provided by IP.
• TCP provides a reliable byte-stream channel.
![Page 6: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/6.jpg)
TCP/IP network
![Page 7: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/7.jpg)
Socket• A socket is one end-point of a two-way
communication link between two programs running on the network.
• A server application normally listens to a specific port waiting for connection requests from a client.
• When a connection request arrives, the client and the server establish a dedicated connection over which they can communicate.
• During the connection process, the client is assigned a local port number, and binds a socket to it. The client talks to the server by writing to the socket and gets information from the server by reading from it.
![Page 8: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/8.jpg)
logical relationships among applications
![Page 9: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/9.jpg)
Socket Addresses• IPv4 uses 32-bit binary addresses to identify communicating hosts.
• .NET encapsulates the IP addresses abstraction in the IPAddress class which can take a long integer IP argument in its constructor, or process a string with the dotted-quad representation of an IP address using its Parse()method.
• The Dns class also provides a mechanism to look up, or resolve names to IP addresses (e.g., server.example.com ).
![Page 10: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/10.jpg)
• a single server to resolve to multiple IP addresses or name aliases, the results are returned in a container class IPHostEntry, which contains an array of one or more string.
• The Dns class has several methods for resolving IP addresses. The GetHostName() method takes no arguments and returns a string containing the local host name.
![Page 11: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/11.jpg)
• The GetHostByName() and Resolve()methods are basically identical, they take a string argument containing the host name to be looked up and returns the IP address and host name.
• Information for the supplied input in the form of an IPHostEntry class instance. The Get-HostByAddress() method takes a string argument containing the dotted-quad string representation of an IP address and also returns host information in an IPHostEntry instance.
![Page 12: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/12.jpg)
using System.Net;using System.Net.Sockets;namespace socket1{ class IPAddressExample { static void PrintHostInfo(String host) { try { IPHostEntry hostInfo;
// Attempt to resolve DNS for given host or address
hostInfo = Dns.Resolve(host);
![Page 13: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/13.jpg)
// Display the primary host name
Console.WriteLine(" \tCanonical Name : " + hostInfo.HostName); // Display list of IP addresses for this host Console.Write(" \tIP Addresses: "); foreach (IPAddress ipaddr in hostInfo.AddressList) { Console.Write(ipaddr.ToString()+""); } Console.WriteLine(" \n"); }catch (Exception) { Console.WriteLine(" \tUnable to resolve host:"+ host+ "\n"); } }
![Page 14: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/14.jpg)
static void Main(string[] args) {
// Get and print local host info try { Console.WriteLine("Local Host:"); String localHostName = Dns.GetHostName(); Console.WriteLine(" \tHost Name: " + localHostName); PrintHostInfo(localHostName); } catch (Exception) { Console.WriteLine("Unable to resolve local host\n"); } } }
![Page 15: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/15.jpg)
![Page 16: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/16.jpg)
![Page 17: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/17.jpg)
TCP classes• The transmission control protocol (TCP) classes
offer simple methods for connecting and sending data between two endpoints. An endpoint is the combination of an IP address and a port number.
• Existing protocols have well defined port numbers, for example, HTTP uses port 80, while SMTP uses port 25.
• The Internet Assigned Number Authority, IANA, (http://www.iana.org/) assigns port numbers to these well-known services.
![Page 18: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/18.jpg)
• Socket: Low-level class that deals with managing connections. Classes such as WebRequest, TcpClient, and UdpClient use this class internally.
• NetworkStream: Derived from Stream . Represents a stream of data from/to the network.
• TcpClient: Enables you to create and use TCP connections.
![Page 19: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/19.jpg)
• TcpListener: Enables you to listen for incoming TCP connection requests.
• The TcpListener class listens for incoming TCP connections with the Start()method.
• When a connection request arrives you can use the AcceptSocket() method to return a socket for communication with the remote machine, or use the AcceptTcpClient()method to use a higher-level TcpClient object for communication.
![Page 20: Networking and socket](https://reader033.vdocuments.mx/reader033/viewer/2022052619/5560ca23d8b42a3c158b48e4/html5/thumbnails/20.jpg)
• UdpClient: Enables you to create connections for UDP clients. (UDP is an alternative protocol to TCP, but is much less widely used, mostly on local networks.)