Java Socket Programming

Home  »    Java   »    Advance Java  »  Socket Programming


Contents


Basic Of Networking

In the Today's World the Customer demands are increasing day by day and they wants All the Resources at a Single Place which is not possible on a Single Machine so that This Purpose Can be established by using the Computer Network by Connecting Multiple Computers this is called as Network. The Task of Transferring the data from one place to another is called as Networking. The Term Communication is also used Some Times When we Communicated with Each other When we use Telephones then this is called as Communication When we are Sharing some Information then we can say that we are Communicating with Each other. For Communicating there is always Some Medium is to be used Like Wires, Cables etc Which Transfer the data from one Machine to another. So For Communicating there are following Components those Must be Used :-


  1. Message This is the actual information which is shared by all Persons , The Message May a Text, Pictures etc.
  2. Sender The Sender is a Device which Sends the Message from a user , The Sender May a Computer, or any device on a Computer.
  3. Receiver The Receiver is that Which Accepts the Message which is Send by Sender This May Also be a Computer or any Device which is Connected with a Computer.
  4. Medium The Medium is the Main Interface between a Sender and a Receiver . The Medium is that Which transfer the data from sender to the Receiver this may be a Cable or Wire and any other Transmission Media Which we will talk Later.
  5. Protocol A Protocol is the set of Rules which Decide how the data will be sends from one Place to another Generally a Protocol is the set of Rules which Describe how the data will be sent from one user to another And This is Responsible or other factors like Whether a Sender or a Receiver is Available to Send or Receive the data.


Various Services of Computer Networks

  1. Sharing of Files Network allows Multiple users to Exchange Files between them or The Files those are Stored on different Computers are Accessible to all the other users. Suppose if a user Wants Some information which is stored on a Different Computer So that if a user wants to Stored that information into his own Computer than rather Storing that File , File Could be Shared between Computers.
  2. Sharing Hardware Hardware Refers to the Physical Parts those are attached to the Computer. There are Many hardware Components Like Printers, Plotters those are Attached with this Computer. So that this will increase the cost for Purchasing Hardware. So that With the help of Network we can Share Hardware between the Computers. So that this will reduce the Total Cost of Purchasing.
  3. Application Services With the Advent of Network We can also install the various Applications those are placed on different computers. With the Help of Networking We can use any Application which is installed on different Computers.
  4. Remote Access Many Times we use Network so that one can login onto the Remote Computer which is located in the different city , for doing this wit the help of Network and Telnet Service we can do this.
  5. Backup Network provides also Service of Backup as we make Backup in other System ,with the help of Network We can Backup our data on to the other Computers.

There are any Components which plays their Vital Role for Accessing the Services of Computer Network.
  1. IP Address This is used for Identification of Computer which is Located in the Network . Every Computer which is a Part of Network must have a IP Address.
  2. Port The Port is the Actual Place from Where the Communication will begin and when we Specifying the IP Address then we are Actually Specifying the Number to a Port. Every Request of data transfer will be Performed with the help of Port.
  3. Protocols- As I Mention Earlier that Protocols are Some Rules and Regulations which defines how the data will be transferred from one Computer to another.

For Connecting the Computers with each other we have many ways and Client Server is best among all of them. In this Client Request for a Service from a Server and a Server is that Which Listen the Request the Service and then Gives the Results those are Requested by a Client. So We can say that which fulfills the Request of Client will be known as the Server So that there are Many Servers those are as Listed Below:-

  1. Computer Server a Computer which Fulfill the Request of another Computer.
  2. Print Server Which Handles Request of Many Clients.
  3. Disk Server Provides Some Space Shared by Many Clients.
  4. Web Server Generally on the Internet used to Access the Services of Internet.

Uniform Resource Locator (URL)

URL Stands for Uniform Resource Locator , this is used to Mention the Address of a Page Which is Located on the different Computer. As We Know that Every Web Site has a Specific Address so that if we wants to view any web Page and if we wants to Retrieve any information from the Net then we have to Mention the Address of that Page. The URL Contains :-

  1. Protocol Every URL Must Contains a Protocol Which Specify the Operation , the Protocol may either a HTTP or a FTP.
  2. Host Name URL also Contains the Name of Host or Address of Computer from which we wants to Take any File.
  3. Directory Name A URL may Contains a Name of Directory from which we wants to Retrieve the data.
  4. File Name A URL also Contains Name of File which we wants to Retrieve from the Server.
There is a simple Example of URL :-



In this http is the name of Protocol and Ptu is the name of Server and Result is the name of Directory and Msc is Name of the File. JAVA Provides us a URL Class Which Contains Methods those are used to Retrieve the Attributes from a Address or from a URL . In JAVA URL is treated as a Class and We known that a Class is that Which Contains various Methods. The various Methods those are provided by URL Class are as followings and for accessing any Method from a URL Class First We have to Create the Object of URL Class.


Various Methods of the URL class
MethodsDescription
getHost() Gives the Name of Host or Name of Computer.
getProtocol()Protocl Which we are using For Request or Response.
getPort() Port Number used for Sending or Receiving data.
getFile() Gives the Name of File which we are Accessing.


IP Address

   As We know that Every Computer must have an IP Address in the Network and IP address of a Computer is Represented in the form of Dotted Form as 192.168.110.100 and Always Remember that address of a Computer is Represented in the Form of 32 bits or in 4 bytes. And we can also call them as 4 Octets. But this is very Difficult for a user to Remember Address of Each Computer in the dotted Form so that IP addresses are Converted into the Name system Which is also Called as Domain Name System.
    JAVA Provides us a Class Which is also Called as InetAddress which gives the Name of Address of Computer . this Class contains two Methods those are getLocalHost() and getByName() . The First Method gives us the Address of Computer and Second Method Gives us the Host Name of Name of Computer.



Proxy Server

As we know that internet is a Collection of Computers those are connected with each other through Some Communication Media. So When as Client Request for a Service to a Server. Then Many Times this is not Possible to Contains all the Required data on to the Single Computer So that A Server Request to another Server When he doesn't have Requested data of a Client .

So that When a Server Request for a Service to another Server, then there is no Communication between the Client and third Server but A server displays the Contents on the Client Machine So we can call a Server as a Proxy Which Request to another Server for fulfilling Request of his Client. The most Important Example of Proxy Server is the Google When a Client submits his Query then he will Search or Send his Request to many Computers.

We can call them as a Proxy Server. The proxy may a type of Web Proxy and Winsock Proxy and Socks Proxy. The Web Proxy Server is that which Request for a Web Pages from a Server and WinSock Server us that which is used by TCP or UDP Protocols for Fulfilling their Requests. And Socks Proxy Server is that which is used for transferring the data from Client to a Server. There are May advantages of Proxy Servers :-

  1. Proxy Server improves the Performance in Accessing Internet and Reduce the Delay in Time.
  2. Enable only few Clients to Connect with each other.
  3. Protects the data from a Unauthorized Users.

All the computers those are running on the Internet , communicate with each other Always use TCP or UDP protocols, which are both 4-layer protocol. TCP stands for Transmission Control Protocol and UDP stands for Unreliable Datagram Protocol. And Many Times some other Protocols s are used Like HTTP, this Protocol Is used when we wants to Connect with Internet. And FTP Protocol is used for Transfer the File from one Computer to another. So as Like there are Many Protocols those are used for Connecting Computers with Each other. But among of them TCP and UDP are most important when we wants Reliability of data delivery and When we have a Short time for Sending the data from one Place to another with higher Security and Reliability then we uses these Protocols.

The four layers are: -

Application layer
(HTTP,FTP,TELNET...)
Transport Layer
(TCP/IP,UDP...)
Network Layer
(IP...)
Data Link Layer
(Device Driver...)

All the java network programs are developed at the application layer and all the classes are available in java.net package. We can create our programs by using either TCP/IP or UDP. So there is difference between TCP and UDP.

  1. TCP/IP IP is a Datagram protocol. It means all the packets of Data, are not guaranteed to be deliver at the right place. It is a connection-less protocol. So TCP is used to provide a service With 100 % guarantees of delivery. In this if two applications want to communicate, first they establish a connection and then data is send over this connection. So it guarantees that data is delivered at the right place HTTP, FTP and Telnet uses this protocol. This Protocol is used when we have a Large Number of Packets and we wants higher Reliability of data. And When there are Many Computers Connected to a Single Network.

  2. UDP It is protocol that sends independent packets of data from one place to another with no guarantee about the delivery it is not connection-based like TCP. It sends independent data packets called datagram's with no guarantee and each packet is independent of each other. This Protocol will Make datagram's instead of Packets and these datagram's will treated as independent datagram's. and This Protocol is used When there are only a few users connected with Network and when we are sure about the delivery of Packets and When we have a short Time for Sending the data from one to another.


Ping

Ping command is used to test the communication between two programs over a network. It gives as the information about a connection in which how many packets are recovered or not. It uses the service of UDP, which doesn't need the guarantee of reliable delivery.



Ports

Generally a computer has a single connection on the network. All the data is send and receive through this connection but a computer uses ports to know which computer will receive this data. Using the address of machine and port number performs data transmission. The machine address is 32-bit and port number is 16-bit that is used by TCP and UDP to deliver the data. We know that for reliable network they first create connection. For creating a connection they should use a socket which binds them with a port number. At a time only one socket can be bound. So sockets are software interface that connect an application to a network. In the client-server first a client will open a port of its own and then it request to server for making a connection with the server by using server port.


Various Protocols have their own Port Numbers
  1. TCP/IP The TCP/IP Protocol uses 1024 Port Number.
  2. File Transfer Protocol FTP use 21 Port Number.
  3. Telnet This Protocol uses 23 Port Number.
  4. Finger Used by Client for Transferring their Files and Port Number is 79.
  5. HTTP This Protocol is used for Connecting Client With Internet and Port Number of HTTP is 80.





Socket Programming


Sockets for TCP/IP Sockets of UDP



<< Previous Topic
Next Topic >>



1 comment: