Java Socket Programing: Things to Pay Attention to

learn java socket programing

What is socket programming in Java PPT? Before we dig deep into Java Socket programing in Ppt, and what to understand, we have to go through some definitions. First of all, what is a socket? A socket’s main function is to provide a pathway between two computers using a TCP. Now, I know you’re probably wondering, what is a TCP? Well, this acronym stands for Transmission Control Protocol.

Its mains function is to allow for a reliable pathway of communication between two applications. Most of the time, TCP is used instead of Internet Protocol. Now let’s get back to how socket programming works in Java. The client’s program will then create a socket from its source, which will then attempt to connect to the server’s socket.

After communication is successful, the server will then take charge. It will create a socket object on its end. That means that the client and server can now read and write each other’s data without breaking a sweat!

The java.net.Socket Class

In Java, this class represents a socket while the java.net.ServerSocket will provide a mechanism for the program to ‘pay attention’ to clients needs and requirements and create an ideal connection with them. In fact, here is a breakdown of how the TCP connection occurs between computers using sockets.

  • The server first highlights which port number on which communication is to take place from by activating a ServerSocket object.
  • The server then instantiates the accept() method which gives a window of time for the client to connect to the given port activated by the server.
  • While the server waits, the client can activate a Socket object on the specified port and socket of the server. After communication has been successful, the client can now access the Socket object of the server.
  • Now, the server can utilize the accept()method to acknowledge that the server has connected to the client socket.

how socket programming works in java scheme

After establishing a solid connection pathway, communication can now take place through the use of an I/O set of streams. Each of the sockets has both Input and Output streams.  Now that you have an idea of how socket programming works, let’s get into some of the important features that you need to understand about it.

Main Features of Socket Programming in Java

1. How to Open a Socket


Opening a socket in Java is pretty simple. In fact, have a look at the code below;

Socket MyClient;
MyClient = new Socket("Machine name", PortNumber);

Whereby Machine name refers to the computer that you are attempting to instantiate a connection with; while the Port Number refers to the numerical digits of the port that are present on the server you are attempting to connect to. Do note that port numbers having a range from 0 all the way to 1023 are usually reserved for VIP users; users that are usually requesting standard services such as HTTP and email. So when selecting a port number, it is advisable that you select above 1023.

Find out how to start FTP programming with Java!

When opening a socket, it is advisable to handle exceptions. Thus, our initial code will be written as:

Socket MyClient;
try{
MyClient = new Socket("Machine name",PortNumber);
}
catch (IOException e) {
System.out.println(e);
}

However, if you are programming as a server in this case, then you should open a socket as shown below:

ServerSocket MyService;
try {
MyServerice = new ServerSocket(PortNumber);
}
catch (IOException e) {
System.out.println(e);
}

As a server, it is also vital that you create a socket object using the ServerSocket so that you can get requests and accept connections from your clients. Therefore, you can rewrite the code as follows:

Socket clientSocket = null;
try {
serviceSocket = MyService.accept();
}
catch (IOException e) {
System.out.println(e);
}

2. How to Open an Input Stream


As a client, you can utilise the DataInputStream in order to create an input stream that receives information from the server output stream. This is shown below:

DataInputStream input;
try {
input = new DataInputStream(MyClient.getInputStream());
}
catch (IOException e) {
System.out.println(e);
}

The DataInputStream is a unique class which encapsulates plenty of other functions. You can be able to read Java primitives in a portable way. It also has other methods such as readLine, readInt and so forth. As a server, you can also use DataInputStream to receive information from your client as shown below:

DataInputStream input;
try {
input = new DataInputStream(serviceSocket.getInputStream());
}
catch (IOException e) {
System.out.println(e);
}

3. How to Create an Output Stream


As a client, you can create an output stream to relay data to the socket of the server. You can do this by accessing the class PrintStream as shown below:

PrintStream output;
try {
output = new PrintStream(MyClient.getOutputStream());
}
catch (IOException e) {
System.out.println(e);
}

The class PrintStream is useful because it carries methods for easier textual representation for Java primitives. Apart from PrintStream you can also utilize the DataOutputStream:

DataOutputStream output;
try {
output = new DataOutputStream(MyClient.getOutputStream());
}
catch (IOException e) {
System.out.println(e);
}

This method also allows you to write Java primitives. If you are operating as a server programmer on the other hand, you can utilize the PrintStream Java socket class to communicate with the client as shown in a sample code:

PrintStream output;
try {
output = new PrintStream(serviceSocket.getOutputStream());
}
catch (IOException e) {
System.out.println(e);
}

4. How to Close Sockets


Before closing your socket, as a rule of thumb, always make sure to close the output stream and input stream first.

As a client programmer, you can use the following code:

try {
output.close();
input.close();
MyClient.close();
}
catch (IOException e) {
System.out.println(e);
}

As a programming server:

try {
output.close();
input.close();
serviceSocket.close();
MyService.close();
}
catch (IOException e) {
System.out.println(e);
}

As you can see, java socket programing is actually pretty simple once you get the hang of it. And if you’d like to know more about java socket programming and how to create codes for processes such as simple mail transfer protocol, we will assist you in getting your bearings right! Feel free to use our Java programming help!

So get in touch with us today and you will receive all the java socket programing help you need!