Networking Concepts in Java
Networking:
In this, it describes the concept of networking by using sockets.
Sockets:
- Socket is the name given, in one particular programming model, to the endpoints of a communication link between processes.
- Because of popularity of that particular programming model, the term socket has been reused in other programming models, including JAVA technology.
- When processes communicate over a network, JAVA technology uses the streams model.A socket can hold two streams: one input stream and one output stream.
- The process does send the data to another process through the network by writing to the output stream associated with the socket.
- A process does read the data written by another process by reading from the input stream associated with the socket.
- After the network connection is set up, using the streams associated with that connection is similar to using any other stream.
Setting up the Connection:
- To set up the connection, one machine must run a program that is waiting for a connection, and a second machine must try to reach the first.
- This is similar to a telephone system, in which one party must make the call, while the other party is waiting by the telephone when that call is made.
Networking with the JAVA technology:
In this, it describes the concept of networking by using JAVA technology.
Addressing the Connection:
- When you make a telephone call, you need to know the telephone number to dial.
- When you make a network connection, you need to know the address or the name of the remote machine.
- In addition, a network connection requires a port number, which you can think of as a telephone extension number.
- After you connect to the proper computer, you must identify a particular purpose for the connection.
- So, in the same way that you can use a paricular telephone extension number to talk to the accounts department, you can use a particular port number to communicate with the accounting program.
Port Numbers:
- Port numbers in TCP/IP systems are 16-bit numbers and the values range from 0-65535. In practice, port numbers below 1024 are reserved for the predefined services, and you should not use them, unless communicating with one of those services (such as telnet, Simple Mail Transport Protocol [SMTP] mail, FTP, and so on).
- Client port numbers are allocated by the host OS to something not in use, while server port numbers are specified by the programmer, and are used to identify a particular service.
- Both client and server must agree in advance on which port to use. If the port numbers used by the two parts of the system do not agree, communication does not occur.
JAVA Networking Model:
- In the JAVA programming language, TCP/IP socket communication are implemented with the classes in java.net package.
The steps given below illustrates what occurs on the server side and the client side:
- the server assigns a port number. When the client requests a connection, the server opens the socket connection with the accept() method.
- The client establishes a connection woth host on port port#.
- Both the client and server communicate by using an InputStream and the OutputStream.
Minimal TCP/IP Server:
- The TCP/IP server applications do rely on the ServerSocket and the Socket networking classes provided by the JAVA programming language.
- The ServerSocket class takes most of the work out of establishing a server connection.
- import java.net.*;
- import java.io.*;
- public class SimpleServer
- {
- public static void main(String args[])
- {
- ServerSocket s = null;
- // Register your service on port 5234
- try{
- s = new ServerSocket(5234);
- }catch(IOException e)
- {
- e.printStackTrace();
- }
- // run the listen/accept loop forever
- while(true)
- {
- try{
- // wait her and listen for a connection
- Socket s1 = s.accept();
- // get output stream associated with the socket
- OutputStream slout = s1.getOutputStream();
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(slout));
- // send your string!
- bw.write(“this is my first string!\n”);
- // close the connection, but not the server socket
- bw.close();
- sl.close();
- }catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
Minimal TCP/IP Client:
- The client side of a TCP/IP application rlies on the Socket class. Again, much of the work involved in establishing connections is done by the Socket class.
- The client attaches to the server presented in “Minimal TCP/IP server”, and then prints everything sent by the server to the console.
- import java.net.*;
- import java.io.*;
- public class SimpleClient
- {
- public static void main(String args[])
- {
- try{
- // open your connection to a server, at port 5234
- // localhost used here
- Socket s1 = new Socket(“127.0.0.1”,52341);
- // get an input stream from the socket
- InputStream is = s1.getInputStream();
- // decorate it with a “data” input stream
- DataInputStream dis = new DataInputStream(is);
- // read the input and print it to the screen
- System.out.println(dis.readUTF());
- // when done, just close the stream and connection
- br.close();
- sl.close();
- }catch(ConnectException e)
- {
- System.err.println(“Could not connect.”);
- }
- catch(IOException ioe)
- {
- // ignore
- }
- }
- }