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