Java Socket Programming Basics

Please download to get full document.

View again

of 11
8 views
PDF
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Document Description
Java Socket Programming Basics Objectives: ã ã ã ã Appreciate low-level network communication using sockets Demonstrate client-server architecture through socket programming Fully understand the difference between datagram communication and stream communication Learn the Java classes that facilitate socket communication Concepts In order for us to write meaningful network-based application, we need to have a firm grasp of the factors involved in implementing these applications and the sound kno
Document Share
Documents Related
Document Tags
Document Transcript
  Java Socket Programming Basics Objectives: ã   Appreciate low-level network communication using sockets ã   Demonstrate client-server architecture through socket programming ã   Fully understand the difference between datagram communication and stream communication ã   Learn the Java classes that facilitate socket communication Concepts In order for us to write meaningful network-based application, we need to have a firm grasp of the factors involved in implementing these applications and the sound knowledge of the fundamentalnetwork programming models. SERVERCLIENTrequestrespond Network CLIENT-SERVERARCHITECTURE  Basically, network-based application systems consist of a server , a client and a media forcommunication. The server machine hosts an application that offers requested services from clients.The client machine runs a program that makes request for services.The setup shown above uses the networking services provided by the transport layer of the TCP/IP protocol stack . The most widely used programming interfaces for these protocols are sockets .There are two protocols used for communication  – datagram and stream communicationprotocol.The datagram protocol as known as User Datagram Protocol (UDP) is a connectionless protocol.It sends independent packets of data, called datagrams . The protocol does not guarantee you the arrivaland sequencing of the packets sent. During the communication, you need to send the local socketdescriptor and the receiving socket’s address.The stream communication is also known as the Transmission Control Protocol. It is aconnection-oriented protocol. A connection must be established between two sockets prior tocommunication. While the server socket listens for connection request, the other (client socket) asks for  a connection. Once the sockets have been connected, they can be used to transmit data in bothdirections. Socket and Socket Based Communication Sockets provide an interface for programming networks at the transport layer. Handling socketsis treated like file handle that is why socket programming is very much similar to performing file I/O.Socket-based communication is independent of a programming language used for implementingit.A server program runs on a specific computer and has a socket that is bound to a specific port.The server listens to the socket for a client to make a connection request. If everything goes well, theserver accept the connection . Upon acceptance, the server gets a new socket bound to a different port.It needs a new socket (a different port) so that it can continue to listen to the srcinal socket forconnection request s while serving the connected client. Java Programming and the Java.net class A socket is one end-point of a two-way communication link between two programs running onthe network. Socket is bound to a port number so that the TCP layer can identify the application thatdata is destined to be sent. Java provides a set of classes, defined in a package called java.net, to enablethe rapid development of network applications. Key classes, interfaces and exceptions in java.netpackage simplifying the complexity involved in creating client and server programs are: Classes : o   ContentHandler o   DatagramPacket o   DatagramSocket o   DatagramSocketImpl o   HttpURLConnection  o   InetAddress o   MulticastSocket o   ServerSocket o   Socket o   SocketImpl o   URL o   URLConnection o   URLEncoder o   URLStreamHandler Interfaces o   ContentHandlerFactory o   FileNameMap o   SocketImplFactory o   URLStreamHandlerFactory Exceptions o   BindException o   ConnectionException o   MalformedURLException o   NoRouteToHostException o   ProtocolException o   SocketException o   UnknownHostException TCP/IP Socket Programming ã   The two key classes from the java.net package used in creation of server and client programsare: o   ServerSocket o   Socket ã   A server program creates a specific type of socket that is used to listen for client requests. o   In the case of a connection requests, the program creates a new socket through which itwill exchange data with the client using input and output streams.   Algorithm for A Simple Server Program in Java 1.   Open the Server Socket: ServerSocket server = new ServerSocket(PORT); 2.   Wait for the client request. Socket client = server.accept(); 3.   Create I/O streams for communicating to the client DataInputStream is = new DataInputStream( client.getInputStream());DataOutputStream os = new DataOutputStream( client.getOutputStream()); 4.   Perform communication with client //Receive from clientString line = is.readLine( );//Send to clientos.writeBytes( “hello\n”); 5.   Close socket. client.close( );  Algorithm For A Simple Client Program in Java 1.   Create a Socket ObjectSocket client = new Socket ( SERVER, PORT );2.   Create I/O streams for communicating with the server.DataInputStream is = new DataInputStream(client.getInputStream( ));DataOutputStream os = new DataOutputStream(client.getOutputStream( ));3.   Perform I/O or communication with the server.//Receive data from the serverString line = is.readLine( );//Send data to the serveros.writeBytes(“Hello\n”);4.   Close the socket when done.client.close();
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks