Networking and socket


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Networking and socket

  1. 1.  Networking In 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.
  2. 2. • 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)
  3. 3. 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 
  4. 4. • 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. 
  5. 5. • 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.
  6. 6. TCP/IP network
  7. 7. 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.
  8. 8. logical relationships among applications
  9. 9. 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., ).
  10. 10. • 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.
  11. 11. • 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 GetHostByAddress() method takes a string argument containing the dotted-quad string representation of an IP address and also returns host information in an IPHostEntry instance.
  12. 12. 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);
  13. 13. // 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"); }
  14. 14. 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 hostn"); }} }
  15. 15. 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, ( assigns port numbers to these well-known services.
  16. 16. • 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.
  17. 17. • 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.
  18. 18. • 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.)