SocketServer based zip server

In this chapter you will learn:

  1. Zip server socket

Zip server socket

The following code creates a zip stream based socket by extending Socket class.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.net.Socket;
 //ja  va  2  s . c o  m
class ZipSocket extends Socket {

    private InputStream in;
    private OutputStream out;

    public ZipSocket() { super(); }

    public ZipSocket(String host, int port) 
        throws IOException {
        super(host, port);
    }

    public InputStream getInputStream() 
        throws IOException {
        if (in == null) {
            in = new ZipInputStream(super.getInputStream());
        }
        return in;
    }

    public OutputStream getOutputStream() 
        throws IOException {
        if (out == null) {
            out = new ZipOutputStream(super.getOutputStream());
        }
        return out;
    }

   
    public synchronized void close() throws IOException {
        OutputStream o = getOutputStream();
        o.flush();
        super.close();
    }

}

The following code creates a Zip stream based Server socket by extending ServerSocket.

import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
/*  ja v  a2 s.  c  o  m*/
public class ZipServerSocket extends ServerSocket
{
  public ZipServerSocket(int port) throws IOException
  { 
  super(port);
  }

  public Socket accept() throws IOException
  { 
     Socket socket = new ZipSocket();
     implAccept(socket);
     return socket;
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Create SocketChannel from IP address
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