JAVA TCP/IP Socket Implementation (5)

Server Actions

Instantiate a ServerSocket object

  1. Instantiate a ServerSocket object
  2. Receive connections from clients
  3. Communicate with clients
    1. Recieve data/requests
    2. Send data/replys
  4. Close the socket
  • Instantiating a ServerSocket object creates a socket ready to accept client connections
  • Replaces the socket(), listen(), and
    bind() functions in C / C++
  • Three common constructors, arguments are...
  1. port number
  2. port number, listener backlog
  3. port number, listener backlog and IP address
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;

ServerSocket acceptSock;                   // Declare the ServerSocket

//  Instantiate a ServerSocket using constructor that takes only the port number
try {
	acceptSock = new ServerSocket(13214);
}

catch(IOException ioe) {
	System.out.println("Error opening server socket: " + ioe.getMessage());
	return;
}