5/18/2012

Socket Programming in Java: Basics Theory

 easywayprogramming.com socket programming in java basics theory    

       Hi guys, socket programming in java founds difficult to many peoples. I tried to show it in simple way, so peoples can understand it. First we will see some basic theory.
   
       There are two communication protocols that we can use for socket programming.
                  1. TCP/IP Communication (Stream communication)
                  2. UDP/IP Communication (Datagram communication)

Datagram Communication:
       The datagram communication protocol, known as UDP (user datagram protocol), is a connectionless protocol, meaning that each time you send datagrams, you also need to send the local socket descriptor and the receiving socket's address. As you can tell, additional data must be sent each time a communication is made.

Stream Communication:
       The stream communication protocol known as TCP(transfer communication protocol), i a connection oriented protocol. In order to do communication over the TCP protocol, a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client). Once two sockets have been connected, they can be used to transmit data in both (or either one of the) directions.
        Now we focus on stream communication.

Socket:
       Socket is one end-point of two way communication channel between two programes that are running on network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent. It is like end-point of tunnel or pipe-line.The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.

 Server: A server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.

 Client: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client request to the server on the server's machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system.





If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client. On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.

Example: Socket programming in java: basic example

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Great Post .The way you have explained socket programming is simply good and understandable.I have found similar to this http://crbtech.in/Java-Training/socket-programming-java/ it will surely help for beginners.

    ReplyDelete
  3. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  4. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  5. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete