Socket Openeration Test : Server : Network Protocol : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  Network Protocol   » [  Server  ]  Screenshots 
 



Socket Openeration Test

/**
 * version 1.00 1999-08-27 author Cay Horstmann
 */

import java.io.IOException;
import java.net.Socket;

public class SocketOpenerTest {
  public static void main(String[] args) {
    String host;
    if (args.length > 0)
      host = args[0];
    else
      host = "www.java2s.com";

    int port;
    if (args.length > 1)
      port = Integer.parseInt(args[1]);
    else
      port = 80;

    int timeout = 5000;
    Socket s = SocketOpener.openSocket(host, port, timeout);

    if (s == null)
      System.out.println("The socket could not be opened.");
    else
      System.out.println(s);
  }
}

class SocketOpener implements Runnable {
  public static Socket openSocket(String aHost, int aPort, int timeout) {
    SocketOpener opener = new SocketOpener(aHost, aPort);
    Thread t = new Thread(opener);
    t.start();
    try {
      t.join(timeout);
    catch (InterruptedException exception) {
    }
    return opener.getSocket();
  }

  public SocketOpener(String aHost, int aPort) {
    socket = null;
    host = aHost;
    port = aPort;
  }

  public void run() {
    try {
      socket = new Socket(host, port);
    catch (IOException exception) {
    }
  }

  public Socket getSocket() {
    return socket;
  }

  private String host;

  private int port;

  private Socket socket;
}


           
       
Related examples in the same category
1.  A generic framework for a flexible, multi-threaded server
2.  Server allows connections on socket 6123 Server allows connections on socket 6123
3.  This server displays messages to a single client This server displays messages to a single client
4.  The client can specify information to control the output of a server The client can specify information to control the output of a server
5.  A server can use specialized streams to deliver typed data A server can use specialized streams to deliver typed data
6.  Serve entire objects using ObjectOutputStream Serve entire objects using ObjectOutputStream
7.  A multithreaded server A multithreaded server
8.  Base class to build multithreaded servers easily
9.  Manage a pool of threads for clients
10.  Client estimates the speed of the network connection to the server
11.  URL Connection Test URL Connection Test
12.  This server retrieves the time using the RFC867 protocol. This server retrieves the time using the RFC867 protocol.
13.  Quote Server
14.  Client and Server Demo
15.  Reflector








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.