Check if socket connected java

Check if socket connected java

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

In Java how to check if Socket is Alive, Connection is Active on specific Port? isSocketAlive() Utility

In Java Jow to check if Socket is Alive and Connection is Active on specific Port? isSocketAlive() Utility

This is a very simple free Java Utility which tells you if you could establish a socket connection to different hosts on specific ports. Feel free to use it in production environment.

In Java InetSocketAddress creates a socket address from a hostname and a port number . We call it as server socket in Java. Socket library with Non blocking IO and Simple IO is a very big topic.

In this tutorial we will go over simple utility which we are calling isSocketAlive (String hostname, int port) which returns boolean result – true/false.

Let’s get started:

Step-1

Create class CrunchifyIsSocketAliveUtility .java

Step-2

Create utility isSocketAliveUitlitybyCrunchify (hostname, port) which returns true/false

Step-3

In main method, we will do 5 tests

  1. Start Apache Tomcat Server in Eclipse IDE and try to connect to localhost on port 8080
    • Install Apache Tomcat in Eclipse Tutorial link
  2. Now connect to port 8081 which will be a failure scenario
  3. We will also connect to site crunchify.com on port 80 . As Crunchify.com runs on Apache Server by default port is 80.
  4. We will connect to site crunchify.com on port 80 –> Success
  5. Connect to site crunchify.com and 81 –> It will be failure

Provide default timeout to 2 seconds.

socket.connect (socketAddress, timeout) connects socket to the server with a specified timeout value.

A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.

We are catching SocketTimeException and IOException in case of error.

This Java Program will help you in case you have below questions:

  • Java socket programming tutorial
  • Writing the Server Side of a Socket
  • Sockets programming in Java
  • java socket connection example
  • Java client/server application with sockets
  • java serversocket example
  • java.net.socket example
  • listen socket connection java example

Complete Java Program:

package crunchify.com.tutorials; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketTimeoutException; /** * @author Crunchify.com * */ public class CrunchifyIsSocketAliveUtility < public static void main(String[] args) < // Run Apache Tomcat server on Port 8080 in Eclipse to see success // result log(isSocketAliveUitlitybyCrunchify("localhost", 8080)); // Now change port to 8081 and you should see failure log(isSocketAliveUitlitybyCrunchify("localhost", 8081)); // Connect to Crunchify.com on port 80 log(isSocketAliveUitlitybyCrunchify("crunchify.com", 80)); // Connection to crunchify.com on port 81 log(isSocketAliveUitlitybyCrunchify("crunchify.com", 81)); // Connection to crunchify.com on port 80 log(isSocketAliveUitlitybyCrunchify("crunchify.com", 80)); >/** * Crunchify's isAlive Utility * * @param hostName * @param port * @return boolean - true/false */ public static boolean isSocketAliveUitlitybyCrunchify(String hostName, int port) < boolean isAlive = false; // Creates a socket address from a hostname and a port number SocketAddress socketAddress = new InetSocketAddress(hostName, port); Socket socket = new Socket(); // Timeout required - it's in milliseconds int timeout = 2000; log("hostName: " + hostName + ", port: " + port); try < socket.connect(socketAddress, timeout); socket.close(); isAlive = true; >catch (SocketTimeoutException exception) < System.out.println("SocketTimeoutException " + hostName + ":" + port + ". " + exception.getMessage()); >catch (IOException exception) < System.out.println( "IOException - Unable to connect to " + hostName + ":" + port + ". " + exception.getMessage()); >return isAlive; > // Simple log utility private static void log(String string) < System.out.println(string); >// Simple log utility returns boolean result private static void log(boolean isAlive) < System.out.println("isAlive result: " + isAlive + "\n"); >>

Output:

hostName: localhost, port: 8080 isAlive result: true hostName: localhost, port: 8081 IOException - Unable to connect to localhost:8081. Connection refused isAlive result: false hostName: crunchify.com, port: 80 isAlive result: true hostName: crunchify.com, port: 81 SocketTimeoutException crunchify.com:81. connect timed out isAlive result: false hostName: crunchify.com, port: 80 isAlive result: true

If you liked this article, then please share it on social media. Have a question or suggestion? Please leave a comment to start the discussion.

Suggested Articles.

Источник

Check if socket connected java

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Читайте также:  Php http auth fastcgi
Оцените статью