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.

 

 

Network Programming In  Java

 

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.

 

  1. import java.net.*;
  2. import java.io.*;
  3. public class SimpleServer
  4. {
  5. public static void main(String args[])
  6. {
  7. ServerSocket s = null;
  8. // Register your service on port 5234
  9. try{
  10. s = new ServerSocket(5234);
  11. }catch(IOException e)
  12. {
  13. e.printStackTrace();
  14. }
  15. // run the listen/accept loop forever
  16. while(true)
  17. {
  18. try{
  19. // wait her and listen for a connection
  20. Socket s1 = s.accept();
  21. // get output stream associated with the socket
  22. OutputStream slout = s1.getOutputStream();
  23. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(slout));
  24. // send your string!
  25. bw.write(“this is my first string!\n”);
  26. // close the connection, but not the server socket
  27. bw.close();
  28. sl.close();
  29. }catch(IOException e)
  30. {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  35. }

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.
  1. import java.net.*;
  2. import java.io.*;
  3. public class SimpleClient
  4. {
  5. public static void main(String args[])
  6. {
  7. try{
  8. // open your connection to a server, at port 5234
  9. // localhost used here
  10. Socket s1 = new Socket(“127.0.0.1”,52341);
  11. // get an input stream from the socket
  12. InputStream is = s1.getInputStream();
  13. // decorate it with a “data” input stream
  14. DataInputStream dis = new DataInputStream(is);
  15. // read the input and print it to the screen
  16. System.out.println(dis.readUTF());
  17. // when done, just close the stream and connection
  18. br.close();
  19. sl.close();
  20. }catch(ConnectException e)
  21. {
  22. System.err.println(“Could not connect.”);
  23. }
  24. catch(IOException ioe)
  25. {
  26. // ignore
  27. }
  28. }
  29. }

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment