Example usage for java.net Socket Socket

List of usage examples for java.net Socket Socket

Introduction

In this page you can find the example usage for java.net Socket Socket.

Prototype

public Socket(InetAddress address, int port) throws IOException 

Source Link

Document

Creates a stream socket and connects it to the specified port number at the specified IP address.

Usage

From source file:MainClass.java

public static void main(String[] args) {

    String hostname = "time.nist.gov";

    try {//from   w ww. j  a v  a  2 s .  co m
        Socket theSocket = new Socket(hostname, 13);
        InputStream timeStream = theSocket.getInputStream();
        StringBuffer time = new StringBuffer();
        int c;
        while ((c = timeStream.read()) != -1)
            time.append((char) c);
        String timeString = time.toString().trim();
        System.out.println("It is " + timeString + " at " + hostname);
    } // end try
    catch (UnknownHostException ex) {
        System.err.println(ex);
    } catch (IOException ex) {
        System.err.println(ex);
    }

}

From source file:getSocketInfo.java

public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
        try {/* w w w . j  a v  a  2 s . c o m*/
            Socket theSocket = new Socket(args[i], 80);
            System.out.println("Connected to " + theSocket.getInetAddress() + " on port " + theSocket.getPort()
                    + " from port " + theSocket.getLocalPort() + " of " + theSocket.getLocalAddress());
        } // end try
        catch (UnknownHostException e) {
            System.err.println("I can't find " + args[i]);
        } catch (SocketException e) {
            System.err.println("Could not connect to " + args[i]);
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    String host = "localhost";

    try {//from  ww  w.j a  v  a 2s  .  co  m
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1024; i < 65536; i++) {
            Socket theSocket = new Socket(theAddress, i);
            System.out.println("There is a server on port " + i + " of " + host);
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }

}

From source file:Main.java

License:asdf

public static void main(String[] arges) throws Exception {

    InetAddress addr = InetAddress.getByName(null);
    Socket sk = new Socket(addr, 8888);
    BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())), true);
    out.println("asdf");
    System.out.println(in.readLine());
}

From source file:MainClass.java

public static void main(String[] args) {

    String host = "localhost";

    try {/*w w w  .  j  ava2 s.c  o m*/
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1; i < 65536; i++) {
            Socket connection = null;
            connection = new Socket(host, i);
            System.out.println("There is a server on port " + i + " of " + host);
            if (connection != null)
                connection.close();
        } // end for
    } catch (Exception ex) {
        System.err.println(ex);
    }

}

From source file:POP3Demo.java

public static void main(String[] args) throws Exception {
    int POP3Port = 110;
    Socket client = new Socket("127.0.0.1", POP3Port);
    InputStream is = client.getInputStream();
    BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
    OutputStream os = client.getOutputStream();
    PrintWriter sockout = new PrintWriter(os, true);
    String cmd = "user Smith";
    sockout.println(cmd);/* ww  w  .  j a v  a 2s  .  com*/
    String reply = sockin.readLine();
    cmd = "pass ";
    sockout.println(cmd + "popPassword");
    reply = sockin.readLine();
    cmd = "stat";
    sockout.println(cmd);
    reply = sockin.readLine();
    if (reply == null)
        return;
    cmd = "retr 1";
    sockout.println(cmd);
    if (cmd.toLowerCase().startsWith("retr") && reply.charAt(0) == '+')
        do {
            reply = sockin.readLine();
            System.out.println("S:" + reply);
            if (reply != null && reply.length() > 0)
                if (reply.charAt(0) == '.')
                    break;
        } while (true);
    cmd = "quit";
    sockout.println(cmd);
    client.close();
}

From source file:Time867.java

public static void main(String[] args) {
    String hostname = "tock.usno.navy.mil";
    if (args.length == 1)
        hostname = args[0];/*from w  w w  .j av  a 2 s  . c o  m*/
    try {
        int c;
        Socket sock = new Socket(hostname, 13);
        InputStream is = sock.getInputStream();
        do {
            c = is.read();
            if (c != -1)
                System.out.print((char) c);
        } while (c != -1);
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:lookForPorts.java

public static void main(String[] args) {

    Socket theSocket;//from  w  ww. j  av a2s.  c  o  m
    String host = "localhost";

    if (args.length > 0) {
        host = args[0];
    }
    for (int i = 0; i < 1024; i++) {
        try {
            System.out.println("Looking for " + i);
            theSocket = new Socket(host, i);
            System.out.println("There is a server on port " + i + " of " + host);
        } catch (UnknownHostException e) {
            System.err.println(e);
            break;
        } catch (IOException e) {
            // must not be a server on this port
        }
    }
}

From source file:BufferedSocketClient.java

  public static void main(String args[]) throws Exception {
  Socket socket1;/*w ww  . j av  a  2  s  .  c  o  m*/
  int portNumber = 1777;
  String str = "initialize";

  socket1 = new Socket(InetAddress.getLocalHost(), portNumber);

  BufferedReader br = new BufferedReader(new InputStreamReader(socket1.getInputStream()));

  PrintWriter pw = new PrintWriter(socket1.getOutputStream(), true);

  pw.println(str);

  while ((str = br.readLine()) != null) {
    System.out.println(str);
    pw.println("bye");

    if (str.equals("bye"))
      break;
  }

  br.close();
  pw.close();
  socket1.close();
}

From source file:Finger.java

public static void main(String[] arguments) throws Exception {
    StringTokenizer split = new StringTokenizer(arguments[0], "@");
    String user = split.nextToken();
    String host = split.nextToken();

    Socket digit = new Socket(host, 79);
    digit.setSoTimeout(20000);/* w w w. ja v a 2 s  . c  om*/
    PrintStream out = new PrintStream(digit.getOutputStream());
    out.print(user + "\015\012");
    BufferedReader in = new BufferedReader(new InputStreamReader(digit.getInputStream()));
    boolean eof = false;
    while (!eof) {
        String line = in.readLine();
        if (line != null)
            System.out.println(line);
        else
            eof = true;
    }
    digit.close();
}