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) throws Exception {

    String hostname = "tock.usno.navy.mil";

    Socket theSocket = new Socket(hostname, 13);
    InputStream timeStream = theSocket.getInputStream();
    StringBuffer time = new StringBuffer();
    int c;//from   www  . j a va2 s .  c o  m
    while ((c = timeStream.read()) != -1)
        time.append((char) c);
    String timeString = time.toString().trim();
    System.out.println("It is " + timeString + " at " + hostname);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket socket = new Socket("localhost", 8000);
    File file = new File("C:/Users/abc/Desktop/image.jpg");
    FileInputStream fileInputStream = new FileInputStream(file);

    byte[] fileBytes = new byte[(int) file.length()];
    OutputStream outputStream = socket.getOutputStream();
    int content;//from   www  .ja  v  a  2 s  .  c  o  m
    while ((content = fileInputStream.read(fileBytes)) != -1) {
        outputStream.write(fileBytes, 0, (int) file.length());
    }

    System.out.println("file size is " + fileBytes.length);
    for (byte a : fileBytes) {
        System.out.println(a);
    }
    socket.close();
    fileInputStream.close();
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "asdfasdfasdf\n";
    byte buf[] = str.getBytes();
    out.write(buf);/*from www  .  j  a  va  2s.c om*/
    int c;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);
    }

    SocketChannel socketChannel = s.getChannel();
    System.out.println(socketChannel.getLocalAddress());

    s.close();
}

From source file:Main.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;/* w ww .  j  a  va2s. c om*/
    BufferedReader bis = new BufferedReader(new FileReader(args[2]));
    while (true) {
        line = bis.readLine();
        if (line == null)
            break;
        line = line + "\n";
        zip.write(line.getBytes(), 0, line.length());
    }
    zip.finish();
    zip.close();
    sock.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;

    Socket socket = new Socket(addr, port);
}

From source file:SocketDemo.java

public static void main(String args[]) throws Exception {
    String server = args[0];/*www  . j  av a 2s . c  o m*/
    int port = Integer.parseInt(args[1]);
    Socket s = new Socket(server, port);

    InputStream is = s.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    System.out.println(dis.readInt());

    s.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String hostname = "localhost";

    Socket theSocket = new Socket(hostname, 7);
    BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
    BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(theSocket.getOutputStream());
    System.out.println("Connected to echo server");

    while (true) {
        String theLine = userIn.readLine();
        if (theLine.equals("."))
            break;
        out.println(theLine);/*from   w w  w  . j  a v a 2  s.  c o  m*/
        out.flush();
        System.out.println(networkIn.readLine());
    }
    networkIn.close();
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) {

    String host = "localhost";

    for (int i = 1; i < 1024; i++) {
        try {/*from w  w  w .ja  v  a  2 s  . com*/
            Socket s = new Socket(host, i);
            System.out.println("There is a server on port " + i + " of " + host);
        } catch (UnknownHostException ex) {
            System.err.println(ex);
            break;
        } catch (IOException ex) {
        }
    }
}

From source file:MainClass.java

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

    String hostname = "time.nist.gov";
    int port = 37;

    InputStream raw = null;//from w ww . j  a va  2s .  c  o m
    Socket theSocket = new Socket(hostname, port);
    raw = theSocket.getInputStream();

    System.out.println(raw.read());

    raw.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket socket = new Socket("localhost", 12900);
    System.out.println("Started client  socket at " + socket.getLocalSocketAddress());
    BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    BufferedWriter socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

    String promptMsg = "Please enter a  message  (Bye  to quit):";
    String outMsg = null;//from  w  ww . j av a  2  s  . co m

    System.out.print(promptMsg);
    while ((outMsg = consoleReader.readLine()) != null) {
        if (outMsg.equalsIgnoreCase("bye")) {
            break;
        }
        // Add a new line to the message to the server,
        // because the server reads one line at a time.
        socketWriter.write(outMsg);
        socketWriter.write("\n");
        socketWriter.flush();

        // Read and display the message from the server
        String inMsg = socketReader.readLine();
        System.out.println("Server: " + inMsg);
        System.out.println(); // Print a blank line
        System.out.print(promptMsg);
    }
    socket.close();
}