Socket read

In this chapter you will learn:

  1. How to connect to a host
  2. How to get InputStream and OutputStream from Socket
  3. How to wrap DataOutputStream and DataInputStream for Socket
  4. Read Object from Socket
  5. Read float number from a Socket
  6. Compressed socket

Connect to an address

The following code uses a Socket to connect to a domain.

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
/*  j  av a  2s . c  o  m*/
public class Main {
  public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java2s.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    int timeoutMs = 2000; // 2 seconds
    sock.connect(sockaddr, timeoutMs);
  }
}

Socket input and out stream

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
// j a  va2s.com
class Whois {
  public static void main(String args[]) throws Exception {
    int c;
    Socket s = new Socket("internic.net", 43);
    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();
    String str = "java2s.com\n";
    byte buf[] = str.getBytes();
    out.write(buf);
    while ((c = in.read()) != -1) {
      System.out.print((char) c);
    }
    s.close();
  }
}

Wrap DataOutputStream and DataInputStream for Socket

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/* jav a  2s . c  o m*/
public class Main {
  public static void main(String[] args) throws Exception {
    Socket client = new Socket("127.0.0.1", 80);
    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();

  }
}

Read Object from Socket

import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.Hashtable;
/*from   j  a  v  a2 s . c o  m*/
public class Main{
  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);
    ois.close();
    sock.close();
  }
}

Read float number from a Socket

import java.io.DataInputStream;
import java.net.Socket;
/*from  j  a v a2s .  co m*/
public class Main {
  public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], 1234);
    DataInputStream dis = new DataInputStream(sock.getInputStream());
    float f = dis.readFloat();
    System.out.println("PI=" + f);
    dis.close();
    sock.close();
  }
}

Compressed socket

import java.io.BufferedReader;
import java.io.FileReader;
import java.net.Socket;
import java.util.zip.GZIPOutputStream;
//from j a  v a  2s .  co m
public class Main {
  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;
    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();
  }
}

Next chapter...

What you will learn in the next chapter:

  1. A simple HTTP client
Home » Java Tutorial » Socket

Socket

    Socket
    Socket creation
    Socket read
    HTTP client
    SMTP Client
    Cipher Socket
    Socket connection and thread

ServerSocket

    ServerSocket
    ServerSocket connection
    File server and client
    Thread based Server
    Time server
    SocketServer based zip server
    ServerSocketChannel
    Channel selector

SocketChannel

    SocketChannel Creation
    Read and write with SocketChannel
    SocketChannel based HTTP client
    SocketChannel Asynchronous

ServerSocketChannel

    ServerSocketChannel
    Channel selector

SSL

    SSL Client Socket
    SSL Server Socket

UDP

    DatagramSocket
    DatagramChannel
    Multicast Group

Cookie

    Cookies