Published on

IJERA (International journal of Engineering Research and Applications) is International online, ... peer reviewed journal. For more detail or submit your article, please visit

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, pp Network programming in Java using Socket Abhijit A. Sawant, Dr. B. B. Meshram Department of Computer Technology, Veermata Jijabai Technological InstituteAbstract This paper describes about Network Network programming makes use of socket forprogramming using java. The Network interprocess communication. Where socket act asprogramming is similar to socket programming the endpoint of the interprocess communication.or Client-Server programming. Where Socket Here sockets can also be termed as network socketprogramming is important to understand how or Internet socket since communication betweeninternet based interprocess communication work. computers is based on Internet protocol. So NetworkIn this we describe about different types of socket programming is also Socket Programming.used in interprocess communicate on. Network From last few years network programmingprogramming basically uses the Client Server has stopped being territorial unit of the specialistmodel. In Client-Server programming there are and became part of every developers work chest. Ittwo different programs or process, one which was started to being used in all application. So thereinitiates communication called Client process and is need to make it simpler, So Java was started toother who is waiting for communication to start being used for network programming because of thecalled Server process. In this paper we also focus advantages Java provided.on the Secure Socket Layer needed for security Java was the first programming languagepurpose. Finally comparison between the designed with networking in mind. Java wasNetwork programming using C language and originally designed for proprietary cable televisionNetwork Programming using Java is shown. networks rather than the Internet, but its always had the network foremost in mind.Keywords— Network programming, java, socket, Java provides solutions to a number ofClient-Server, Secure socket layer problems. Another advantage of java is that it provides security. Biggest advantage of java is thatI. INTRODUCTION it makes writing the programs simpler. It is far In today’s world Internet is used by each simpler to write network program in java than in anyand everybody. The internet is all about connecting language. The network program written in javamachine together and communication. This is where would be easier to understand and less likely to haveNetwork Programming comes. Network error than program written in other language. Inprogramming allows Interprocess Communication. Java part of program that deals with network isIt means it involves writing computer programs that always short and simple. In short it is easy for javacommunicate with other program across a computer application to send and receive data and also tonetwork. Network program can do lots of work. A communicate over the internet.simple network Program can obtain information Java includes classes that help networkfrom many computers located all over the world. It program communicate with certain types of serverscan communicate with millions of people around the and process different types of data but not all theworld. servers and data. So Java allows you to write Network programming uses Client-Server protocol handlers to communicate with differentmodel, so network programming is also Client- server and process the data. The most excitingServer Programming. Where one program start the feature of Java is it contains an easy to use and crosscommunication called client process or program and platform model for communication which makeother who is waiting for communication to start network programming very easy to understand verycalled the server process or program. In the simplest, Client program sends the request to the server.Server sends the response. Client then obtains the II. RELATED WORKdata from the server and displays it. Complex clients In this Section the focus is mainly on thefilter and reorganize data, repeatedly obtain changed Client-server model, Socket programming and thedata, send data to other people , and allows real time secure socket Layer (SSL).In Client server modelchatting, multiplayer gaming. Complex servers often section different types of principal programmingdo a lot of processing on the data before answering models are described. In socket programming wethe question. describe about the sockets, function calls, types of socket and etc. Secure socket layer is mainly required for security purpose of the programs. 1299 | P a g e
  2. 2. Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, ppA. Client Server Model Network programming uses Client-ServerModel. Client program or process initiates thecommunication and servers are program or processwho waits for communication to start. Some timeprogram may be both a client and server. HereClient sends the request to which the server sendsthe response. Typically we have single servermultiple client’s model. The server does not need toknow anything about client even that it exists or not.The client should always know something about theserver atleast where it is located. The IP and Portnumber of the server is generally well known. Sothe client knows where to send the request. InContrast the port number on client side is generallyallocated automatically by the Kernel. Figure 2:-Iterative server Model working Algorithm Iterative Server begin Create Socket bind to well Know port while(1){ read request from client send reply to client exit } end Figure 1: - Client-Server Model Figure 3:-Iterative server using Socket In Client Server Program there are two A typical Iterative server using socket isprincipal programming models. They are Iterative shown in Figure.3.In this first socket is created.Server model and Concurrent Server Model. Then bind the socket to well-known port. When the request from the client has been received the serverI) Iterative Server Model sends the reply to the client. No new request will be In Iterative Server Model Only one request processed until the previous request is processed at a time. In this listener and server After request is processed exit and server acceptportions of the application coexist and run as part of new request and follows the same procedure.the same task. The server application holds thesocket until all application processing has II) Concurrent Server Modelcompleted. In this client has to wait until server does In Concurrent Server Model many requestnot respond. It is Easy to build and it is mostly used are processed at same time. In this the server role iswhen request can be completed in a small time. The to listen for service request from different host orproblem with this client server model it causes clients. Once the request has been received theunnecessary delay. servers fork the child process to handle it and server goes back to listening to other request. In this new request can be processed while other request still being served. It gives better performance than Iterative server model. The disadvantage of this model is it is difficult to design and build. 1300 | P a g e
  3. 3. Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, pp Internet Domain and the NS Domain. Java Basically supports the Internet Domain to maintain cross platform. In Internet Domain, the BSD Socket Interface is built on the top of either TCP/IP or UDP/IP or the raw Socket. Socket Programming is important to understand how internet based interprocess communication work but not at the level program developed but at a higher level that is compiled to set of Socket Programs. Before going in detail about Socket let’s focus on the Communication API. I) Communication API Communication APIs enable access to services for local and remote communication. They are present in computing system at different level. There are basically three levels High level Communication APIs, Low Level Communication APIs and Basic Level Communication APIs. Figure 4:-Concurrent server model working High Level Communication APIs are typically present in the User Space which provides Algorithm Concurrent Server programming language approach to middleware communication service. Low level APIs are present begin in kernel space. create socket The Basic level Communication APIs are bind to well know port usually located between the user space and Kernel while (1){ space. It usually allows application programs to gain read request from client services for Interprocess communication, network if(fork()==0){//child communication and device -communication. At this Serves request from Basic Level Communication Sockets are present client which is used for Interprocess Communication i.e exit for Network Programming. } else{ //parent } } end Figure 5:-Concurrent Server Using Socket A typical Concurrent server using socket isshown in Figure.5.In which first socket is created.Then bind the socket to well-known port. When therequest from the client has been received thenetwork connection is setup. The server then forks achild process to serve the request from client and itgoes back to listen to other client request. Here newrequest is accepted even if previous one is stillremaining to be served.B. Socket Programming Network programming makes use of socketfor Interprocess Communication. Due to which Figure 6:-levels of Communication APIsNetwork programming is also termed as socketprogramming. In Socket programming using Java, II) SocketsBSD style Socket to Interface with TCP/IP services Socket is an Interface between theis used. BSD Socket Interface provides facilities for application and the network. Where ApplicationInterprocess Communication. BSD Socket Interface create socket and socket type dictates style ofSupports different domain, the UNIX Domain, the communication i.e. connectionless or connection 1301 | P a g e
  4. 4. Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, pporiented. Socket basically acts as endpoint of class present in package. TheInterprocess communication. The interprocess class is used for doing client sidecommunication over Internet is based on versions of TCP operations. Client Socket act as an endpoint forsocket. Sockets can also be termed as network communication between two machine. The Socketsocket or Internet socket since communication class contains constructor that is used to createbetween computers is based on Internet protocol. stream socket that connects to specific port numberSockets are a means of using IP to communicate associated with exactly one host.TCP Client Socketbetween machines. Socket is one major feature that encapsulates a andallows Java to interoperate with legacy system by talking to existing server using predefined Client program written using Client Socket willprotocol. Socket also has some transparency that have following life cycle:-allows kernel to redirect output of one process to theinput of another machine. 1) Creates a stream socket using constructor Function Call in Socket API are divided into Socket(String host, int port).three categories. 2) Sockets try to connect to remote host. 3) Then, Input stream and output stream are used byi) Managing Connection:- client and server to send data to each other. Both In this we have socket(), bind(), listen(), Client and server agree upon handshaking beforeconnect() and accept(). Where socket() call is used sending create a Socket. An application opens a socket by 4) When communication is over or data transfer isusing the socket() call. bind() call is used to bind a complete one or both side close the connection.socket to local IP address and port. listen() call isused in passive open .it is used by server to b) Server Socket:-announce its objective of receiving incoming The Server Program or process basicallyconnection request. connect() call is used to start uses the TCP Server Socket. To implement serverconnection to another socket. accept() call is used to sockets class is used which isaccept a new connection. present in the package. The class is used for doing serverii) Sending and Receiving Data:- side TCP operations. It waits for request to come In this we have send(), sendto(), recv(), over the network and then perform operation basedrecvfrom() and etc. Where send() call sends data on on request. Once ServerSocket has setup connectiona connected socket. sendto() call is used to send the the server uses Socket object to send data to client.datagram to another UDP socket. recv() call is used The ServerSocket Class contains Constructor that isto receive message from socket. recvfrom() is used used to create new ServerSocket Object on ato read a datagram from a UDP socket. particular local port and also contains method like accept() to listen for connection on a specified port.iii) Managing Endpoint Characteristics:- In this we have Close() function which is Server program written using Serverbasically used to close the socket i.e. shutdown the Socket will have following life cycle:-connection. 1) Creates a ServerSocket on a particular port using Sockets are divided into different types. constructor ServerSocket().They are distinguished between TCP Client Socket, 2) Listen for connection to be made to thisTCP Server Socket and UDP Socket. ServerSocket by using accept() method. This method waits till client connects to server and theni) TCP Socket:- returns the Socket object. TCP Socket is that socket which is used for 3) Then, Input stream and output stream are used bymore reliable connection. They perform more client and server to send data to each other. Severreliable and in-order delivery of the packets. They hear the client using input stream. Server talk toare used when there is Connection oriented client using output stream.Communication.TCP socket is also called Stream 4) Both Client and server agree upon handshakingSocket. TCP Socket supports out of bound data before sending data.transmission. In TCP Socket there is TCP Client 5) When communication is over or data transfer isSocket and TCP Server Socket. complete one or both side close the connection. 6) The Server then returns back to second step anda) Client Socket waits for next connection. The Client Program or process basicallyuses the TCP Client Socket. Client Socket are also ii) UDP Socket:-just called Sockets. To implement the Client Socket UDP socket uses User Datagram Protocolthe Client program makes use of (UDP) alternative protocol for sending data. UDP 1302 | P a g e
  5. 5. Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, ppprovides connectionless communication. The SSL allows web browser to authenticate the webconnection here would be unreliable. It does not server. SSL requires web server to have digitalhave starting handshaking phase and here data certificate on it for SSL connection to be made.SSLreceived would not be in order or lost. Doing socket is mostly used as Secure transport below HTTP.programming using UDP no streams are attached to Secure hypertext transfer protocol i.e. HTTPs is asocket.UDP socket is also called as http who is using the SSL.SSL basically sit on top ofDatagramSocket. Datagram Socket is opened to the TCP i.e. it lays between TCP and other uppersend and receive DatagramPacket. layer can be seen in figure. Developer takes To implement the Datagram Socket the advantage of this by replacing all TCP socket callprogram make use of with the new SSL calls.Class which is present in SSL consist of two phases: Handshake andpackage.DatagramSocket class contains Constructor data transfer. During handshake phase Client andthat is used to construct a datagram socket and bind Server use a public key encryption Algorithm toit to the specified port on the local host machine. It determine secret key parameters. In this phase thealso contains method like receive() and send() to communicating parties optionally authenticate eachreceive and send the datagram packets. In this other and then exchange the session key. DuringSocket used by client and the server are almost same Data transfer phase both sides use the Secret key toonly thing they differ in whether they use encrypt the data transmission.anonymous or well-known port.So there are no SSL is an Asymmetric protocol. Itspecial socket class for server. differentiates between a client and server. The SSL handshake sequence may vary depending onClient program written using Datagram Socket will whether the RSA or Diffie Hellman key exchange ishave following life cycle:- used. Mostly SSL session make use of RSA key exchange algorithm with only server authenticated.1) Creates a Datagram socket using constructor Here client authentication is optional and is omittedDatagramSocket(). in most cases.2) Translate hostname to IP address.3) Create the Datagram Packet usingDatagramPacket (Data,Data.length,IP,Port)4) Send datagram Packet to server using send().5) Read datagram Packet from Server usingreceive().6) When communication is over or data transfer iscomplete close the connection.Server program written using Datagram Socket willhave following life cycle:-1) Creates a DatagramSocket on a particular portusing DatagramSocket(int port).2) Read Datagram Packet from client usingreceive().3) Get IP address and port no. of client. Figure 7:- SSL above Transport Layer4) Create the Datagram Packet usingDatagramPacket (Data,Data.length,IP,Port) I) Creating Secure Client Socket5) Send datagram Packet to client using send(). For creating Secure Client Socket instead6) The Server then returns back to second step and of creating or constructing objectwaits for next connection. with a constructor,use to create ClientC. Secure Sockets socket using its createSocket() method. Security is important on Internet and SSLSocketFactory is an abstract class that followspeople want authentication, integration and privacy. the abstract factory design pattern: public abstractSecure Socket layer(SSL) protocol basically helps in class SSLSocketFactory extends SocketFactoryachieving this security goals. SSL was developed by Since SSLSocketFactory is itself abstractNetscape as security measure for web server and you get an instance of it by invoking the staticweb browser. At present SSL is largely used in SSLSocketFactory.getDefault() method. Onceintranets and also in many public internets. SSL has reference to the factory is done, then createSocket()become de facto standard for providing secure e- methods to build an SSLSocket. The Socket thatcommerce transaction over the web. createSocket() method return will be a subclass of 1303 | P a g e
  6. 6. Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, pp Once secure socket iscreated use it like any other socket through its Network Programming using java have lots ofgetInputStream(), getOutputStream() and other advantages that why it is preferred over Networkmethods. programming using C. Writing the network program in java is simpler and network programs are easy toII) Creating Secure Server Socket understand and the programs are also small compare For creating Secure Server Socket instead to network program written in C. Also Java is crossof creating or constructing platform and support code portability. So Javaobject with a constructor, use network program written in java in Linux to create Server socket. will work on windows platform. It has more libraryLike SSL Socket all the constructors in this class are file for network programming compare to C soprotected.Like SSLSocket, instance of writing network program is simpler. It is easy toSSLServerSocket are created by an abstract factory send and receive data for communication using java.class Also it provides more security compared to C. It has Also like SSLSocketFactory an instance of SSL which can provide more security. In NetworkSSLServerSocketFactory is returned by a static programming using java network intensiveSSLServerSocketFactory.getDefault() method. Like programs like web servers and clients, almost all theSSLSocketFactory, SSLServerSocketFactory has code handles data manipulation or the user interface.CreateServerSocket() method that returns instance The part of the program that deals with the networkof SSLServerSocket. The factory that is always short and small. In Network programmingSSLServerSocket Factory.getDefault() returns in java using socket we have different types ofgenerally only support server authentication. It does socket class for Client and different socket class fornot support encryption. To get encryption server Server. At client side we use socket class and atside secure socket requires more initialization and server side we use ServerSocket class. Anothersetup. advantage is that java gives option of applet. In applet the code is not allowed any permanent accessIII) Secure Socket Benefit to the system. So code is executed but no damage to Secure Socket Layer Provides the the system. It is also possible for applets tofollowing Benefits:- communicate across the internet but they are limited1) Authentication: - It ensures that the end systems by security restriction.are the same systems that they say they are.2) Message privacy: - It ensures that the data Network programming Using java we haveexchanged is not viewed by other person other then Java Socket. Java socket has some Advantage andreceiver even if it is intercepted by unauthorized Disadvantage which are as follows:-party.3) Integrity: - It ensures that the data is not modified Advantages:-or tampered over the Internet. 1) Java sockets are flexible and are more powerful. 2) Efficient Socket based programming can beIII. DISCUSSION easily implemented for general communication. In this section comparison between 3) Java Socket causes low network traffic, ifnetwork programming in java and network efficiently used.programming in C is shown. 4) Unlike html forms and CGI scripts that generate and transfer whole web pages for each new request Network programming using C had some can only send necessary information.disadvantages due to which Network programming 5) Java socket provides a simplified interface tousing Java is used. In Network programming in C native socket such as BSD and winsock 2.writing the network program were Difficult to write 6) It hides much of the detail involved in traditionalalso they were difficult to understand and they are socket programming.length. Also C is not cross platform and it does notprovide code portability. So writing a network Disadvantages:-program using c in Linux environment will not work 1) Security restriction are sometimes over bearingin windows environment. It have less number of because a Java Applet running in a web browser islibrary file for network programming compare to only able to establish Connection to the machinejava so writing network program is little bit difficult. where it came from, and to nowhere else on theIt is difficult to send and receive data for network Despite all the useful and helpful feature.communication compare to java. Also it does not 2) Socket based communication allows only to sendprovide so much security. In Programmer writing packets of raw data between applications. Both thenetwork program using C should have considerable client-side and server-side have to provideknowledge of the system with which they are mechanism to make the data useful in any way.working with. 1304 | P a g e
  7. 7. Abhijit A. Sawant, Dr. B. B. Meshram / International Journal of Engineering Research and Applications (IJERA) ISSN: 2248-9622 Vol. 3, Issue 1, January -February 2013, ppIV. CONCLUSIONS [10] Elliotte Rusty Harold,‖Java Network This paper on Network programming in Programming,‖ O’Reilly, 3rd edition,java describe in detail about concepts used in October programming in java. It describes about [11] Sherali Zeadally and Jia Lu,‖ AJava network Programming and its application. performance Comparison ofNetwork programming is Client server Communication APIs on Solaris andprogramming, so it describe about different client windows Operating systems,‖ IEEE 2003.server model. It also explains about Socketprogramming, different types of sockets used likeTCP or UDP. From security perspective we haveSSL in network programming using java. This papergives details about SSL, Creating secure Socket andits benefit. Finally there is comparison of Networkprogramming using Java and Network programmingusing C, provided the advantages and disadvantagesof both. Network programming using Java have lotsof advantage due to which it is preferred.REFERENCES [1] Mengjou Lin, Jenwei Hsieh, David H.C.Du, Joseph P.Thomas and James A. MacDonald, ―Distributed Network Computing over Local ATM Networks,‖ IEEE Journal on Selected Areas in Communications, Vol. 13. No. 4, May 1995 [2] David K. Y. Yau and Simon S. Lam,‖ Migrating Socket – End System Support for Networking with Quality of Service Guarantees,‖IEEE/ACM Transaction on Networking, Vol. 6,No. 6 , December 1998. [3] Stefan Bocking,‖Socket++: A Uniform Application Programming Interface for Basic-Level Communication Services,‖ IEEE Communication Magazine December 1996. [4] Mark A. Holliday,J. Traynham Houston and E. matthew Jones,‖From Socket and RMI to Web Services,‖SIGCSE’08 , Portland, Oregon,USA. March 12-15,2008. [5] S.Kwong, K.T. Ng and W.N. Chau,‖ Design and Implementation of Remote Procedure Call Based on ISO/OSI Reference Model,‖IEEE TENCON Bejing,1993. [6] Mattew Cook and Syed(shawon)M. Rahman,‖ Java and C/C++ language feature in terms of Network Programming,‖ 2005. [7] George Apostolopoulos,Vinod Peris,Prashant Pradhan and Debanjan Saha,‖Securing Electronic Commerce:Reducing the SSL Overhead,‖ IEEE Network July/August 2000 [8] Li Zhao, Ravi Iyer, Srihari Makineni and Laxmin Bhuyan,‖Anatomy [9] Wesley Chou,‖ Inside SSL: The Secure Sockets layer Protocol,‖ IEEE IT pro, July/August 2002 . 1305 | P a g e