JAVA TCP/IP Socket Implementation (7)

For More see Sun Web site : http://java.sun.com/docs/books/tutorial/networking/index.html

Server Actions

Communicate with clients

  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
  • Data is exchanged by reading and writing to input and output streams
  • Create a DataInputStream
  • When creating the DataInputStream tie it directly to the socket
try{
	//  Instantiate an input stream tied directly to the socket
	DataInputStream iStream = new DataInputStream(sock.getInputStream());
      
	//  Read a string and an int from the input stream, i.e from the socket
	//  You need to know the Semantics of the used protocol !
	String helloString = iStream.readUTF();
	int three = iStream.readInt();
}
catch(IOException ioe) {
	System.out.println("Read error: " + ioe.getMessage());
}

try{
    sock.close();
}	
catch(IOException ioe) {
	System.out.println("Close error: " + ioe.getMessage());
}