ServerSocket connection

In this chapter you will learn:

  1. How to connect to ServerSocket
  2. How to create a hello server
  3. How to create a data server
  4. Write Float value
  5. Write Object
  6. Work with Compressed socket

Connect to ServerSocket

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*ja va  2  s  .  co m*/
public class Main {
  public static void main(String[] args) throws Exception {
    ServerSocket server = new ServerSocket(8123);
    while (true) {
      Socket sock = server.accept();
      InetAddress addr = sock.getInetAddress();
      System.out.println("Connection made to " + 
              addr.getHostName() + 
              " (" + 
              addr.getHostAddress()+ 
              ")");
      Thread.sleep(5000);
      sock.close();
    }
  }
}

Hello Server

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//from j ava2s  . c  o m
public class MainClass {

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

    int port = 2345;
    ServerSocket ss = new ServerSocket(port);
    while (true) {
      try {
        Socket s = ss.accept();

        String response = "Hello " + s.getInetAddress() + " on port " + s.getPort() + "\r\n";
        response += "This is " + s.getLocalAddress() + " on port " + s.getLocalPort() + "\r\n";
        OutputStream out = s.getOutputStream();
        out.write(response.getBytes("US-ASCII"));
        out.flush();
        s.close();
      } catch (IOException ex) {
      }
    }
  }
}

Data server

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*from  jav  a2  s  .  co  m*/
public class MainClass {

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

    int port = 37;
    ServerSocket server = new ServerSocket(port);
    while (true) {
      Socket connection = null;
      connection = server.accept();
      OutputStream out = connection.getOutputStream();
      out.write(123);
      out.flush();
      connection.close();
    }
  }

}

Write Float value

import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//from  jav  a  2  s  . co m
public class Main {
  public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    while (true) {
      System.out.println("Listening");
      Socket sock = ssock.accept();

      DataOutputStream dstream = new DataOutputStream(sock.getOutputStream());
      dstream.writeFloat(3.14159265f);
      dstream.close();
      sock.close();
    }
  }
}

Write Object

import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;
//from  j  a  va 2s .  c  o m
public class Main {
  public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Hashtable hash = new Hashtable();
    hash.put("A", "a");

    while (true) {
      Socket sock = ssock.accept();
      ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream());
      ostream.writeObject(hash);
      ostream.close();
      sock.close();
    }
  }
}

Compressed socket

import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.GZIPInputStream;
// j ava2s.c o  m
public class Main {
  public static void main(String[] args) throws Exception {
    ServerSocket ssock = new ServerSocket(Integer.parseInt(args[0]));
    Socket sock = ssock.accept();
    GZIPInputStream zip = new GZIPInputStream(sock.getInputStream());
    while (true) {
      int c;
      c = zip.read();
      if (c == -1)
        break;
      System.out.print((char) c);
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Transfer a file via Socket
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