JAVA TCP/IP Socket Implementation (5)
Server Actions
|
Instantiate a ServerSocket object
|
- Instantiate a ServerSocket object
- Receive connections from clients
- Communicate with clients
- Recieve data/requests
- Send data/replys
- 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...
- port number
- port number, listener backlog
- 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;
}