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 = "localhost";

    Socket connection = null;/*from   w  w w.  java2  s  .  co m*/
    connection = new Socket(hostname, DEFAULT_PORT);
    Writer out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
    out.write("\r\n");
    out.flush();
    InputStream raw = connection.getInputStream();
    BufferedInputStream buffer = new BufferedInputStream(raw);
    InputStreamReader in = new InputStreamReader(buffer, "8859_1");
    int c;
    while ((c = in.read()) != -1) {
        if ((c >= 32 && c < 127) || c == '\t' || c == '\r' || c == '\n') {
            System.out.write(c);
        }
    }
    connection.close();

}

From source file:SquareClient.java

public static void main(String args[]) throws Exception {
    String server = args[0];//w  w w .ja v a2  s  . c om
    int port = Integer.parseInt(args[1]);
    double value = Double.valueOf(args[2]).doubleValue();

    Socket s = new Socket(server, port);
    OutputStream os = s.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeDouble(value);

    InputStream is = s.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    value = dis.readDouble();

    System.out.println(value);
    s.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");

    Socket socket = new Socket("127.0.0.1", 8080);

    String path = "/servlet";
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");

    wr.write(data);/*from   w ww  .  j a v  a  2s. c o  m*/
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String host = "host";
    int port = 25;
    String from = "from@from.net";
    String toAddr = "to@to.net";

    Socket servSocket = new Socket(host, port);
    DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
    DataInputStream is = new DataInputStream(servSocket.getInputStream());

    if (servSocket != null && os != null && is != null) {
        os.writeBytes("HELO\r\n");
        os.writeBytes("MAIL From:" + from + " \r\n");
        os.writeBytes("RCPT To:" + toAddr + "\r\n");
        os.writeBytes("DATA\r\n");
        os.writeBytes("X-Mailer: Java\r\n");
        os.writeBytes(//from   w  ww  . j  av  a 2s .c o  m
                "DATE: " + DateFormat.getDateInstance(DateFormat.FULL, Locale.US).format(new Date()) + "\r\n");
        os.writeBytes("From:" + from + "\r\n");
        os.writeBytes("To:" + toAddr + "\r\n");
    }

    os.writeBytes("Subject:\r\n");
    os.writeBytes("body\r\n");
    os.writeBytes("\r\n.\r\n");
    os.writeBytes("QUIT\r\n");
    String responseline;
    while ((responseline = is.readUTF()) != null) {
        if (responseline.indexOf("Ok") != -1)
            break;
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String serverName = args[0];//w  ww . j  a  v a  2 s .  c  o m
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Connecting to " + serverName + " on port " + port);
        Socket client = new Socket(serverName, port);

        System.out.println("Just connected to " + client.getRemoteSocketAddress());

        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeUTF("Hello from " + client.getLocalSocketAddress());

        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());

        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:lookForPorts2.java

public static void main(String[] args) {

    Socket theSocket;//from   www .j a v a  2s  .c o m
    String host = "localhost";

    if (args.length > 0) {
        host = args[0];
    }

    try {
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1024; i < 65536; i++) {
            try {
                System.out.println("Looking for port " + i);
                theSocket = new Socket(theAddress, i);
                System.out.println("There is a server on port " + i + " of " + host);
            } catch (IOException e) {
                // must not be a server on this port
            }
        }
    } catch (UnknownHostException e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String pageAddr = "http://www.google.com/index.htm";
    URL url = new URL(pageAddr);
    String websiteAddress = url.getHost();

    String file = url.getFile();// w  ww .ja  va2  s . c om
    Socket clientSocket = new Socket(websiteAddress, 80);

    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    OutputStreamWriter outWriter = new OutputStreamWriter(clientSocket.getOutputStream());
    outWriter.write("GET " + file + " HTTP/1.0\r\n\n");
    outWriter.flush();
    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    boolean more = true;
    String input;
    while (more) {
        input = inFromServer.readLine();
        if (input == null)
            more = false;
        else {
            out.write(input);
        }
    }
    out.close();
    clientSocket.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    byte buff[] = new byte[1024];
    InetAddress addr = InetAddress.getByName("www.java2s.com");
    Socket s = new Socket(addr, 80);

    OutputStream output = s.getOutputStream();
    InputStream input = s.getInputStream();
    String GetCmd = "GET /index.html HTTP/1.0\r\n\r\n";
    GetCmd.getBytes(0, GetCmd.length(), buff, 0);
    output.write(buff);// w  ww  . ja  v a  2 s .c o m
    input.read(buff, 0, buff.length);
    System.out.println(new String(buff, 0));

}

From source file:lookForPorts3.java

public static void main(String[] args) {

    Socket theSocket;//  w  ww.j  a v a 2s.  c  o  m
    String host = "localhost";

    if (args.length > 0) {
        host = args[0];
    }

    try {
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1; i <= 65535; i++) {
            try {
                theSocket = new Socket(host, i);
                System.out.println("There is a server on port " + i + " of " + host);
                theSocket.close();
            } catch (IOException e) {
                // must not be a server on this port
            }
        }
    } catch (UnknownHostException e) {
        System.err.println(e);
    }

}

From source file:MainClass.java

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

    String serverName = System.getProperty("WHOIS_SERVER", DEFAULT_HOST);

    InetAddress server = null;//  w ww  .j  a v  a 2s.c  o  m
    server = InetAddress.getByName(serverName);
    Socket theSocket = new Socket(server, DEFAULT_PORT);
    Writer out = new OutputStreamWriter(theSocket.getOutputStream(), "8859_1");
    out.write("\r\n");
    out.flush();
    InputStream raw = theSocket.getInputStream();
    InputStream in = new BufferedInputStream(theSocket.getInputStream());
    int c;
    while ((c = in.read()) != -1)
        System.out.write(c);
}