Home » socket

Tag Archives: socket

Java Nio SocketChannel Example

SocketChannel is a selectable channel belonging to the java.nio.channels package and is used for reading or writing the stream-oriented data. In this tutorial, we learn how to use the SocketChannel and how it is used for reading or writing the stream-oriented data by using the TCP based protocol.               1. Introduction Java Nio was ...

Read More »

Java Nio SSL Example

This is an example of a non-blocking I/O provided by java.nio using SSL handshake.                     1. Definition of Secure Sockets Layer Protocol (SSL) SSL is the secure communication protocol of choice for a large part of the Internet community. There are many applications of SSL in existence, since it is capable ...

Read More »

java.net.ConnectException – How to solve Connect Exception

In this example we are going to discuss about one of the most common exceptions you will come across when dealing with network programming in Java: ConnectException. ConnectException is a subclass of SocketException, and that alone reveals that it is meant to inform you about an error that occurred when you tried to create or access a socket. To be ...

Read More »

Write text to Socket

In this example we shall show you how to write text to a Socket. To write text to a Socket one should perform the following steps: Get the output stream of the socket, using getOutputStream() API method of Socket. Create an OutputStreamWriter with the socket ouputstream. Create a BufferedWriter that uses a default-sized output buffer. Use write(String str) API method to ...

Read More »

Read text from Socket

With this example we are going to demonstrate how to read text from a Socket. A Socket is an endpoint for communication between two machines. In short, to read text from a socket you should: Get the socket input stream, using getInputStream() API method of Socket. Create a new BufferedReader, using a new InputStreamReader with the socket input stream. Use readLine() ...

Read More »

Send HTTP POST request with Socket

This is an example of how to send an HTTP POST request with a Socket. A socket is an endpoint for communication between two machines. Sending an HTTP POST request using a Socket implies that you should: Get the InetAddress of a specified host, using the host’s name, with getByName(String host) API method of InetAddress. Create a new Socket and connect ...

Read More »

Create client Socket with timeout

In this example we shall show you how to create a client Socket with timeout. A client socket is an endpoint for communication between two machines. To create a client socket with timeout one should perform the following steps: Get the InetAddress of a specified host, using the host’s name, with getByName(String host) API method of InetAddress. Create a new ...

Read More »

Create client Socket

With this example we are going to demonstrate how to create a client Socket in Java. In short, to create a socket and connect to a remote server you should : Define the Address of the server socket to connect to Define the specific port the server process running on the remote socket listens to Create a new socket and ...

Read More »