Thursday, April 21, 2011

Tutorial Synchronous Socket Programming using JAVA (For Server)

When you have setup code for client application, after a connection is established, a server application uses the same kind of Socket object for its side of the communications. However, to accept a connection from a client, it must first create a ServerSocket, bound to the correct port. Let's recreate the previous conversation from the server's point of view:





// Meanwhile, on foo.bar.com...
try {
ServerSocket listener = new ServerSocket( 1234 );

while ( !finished ) {
Socket client = listener.accept( );  // wait for connection

InputStream in = client.getInputStream(  );
OutputStream out = client.getOutputStream(  );

// read a byte
byte someByte = (byte)in.read(  );

// read a newline or carriage-return-delimited string
BufferedReader bin =
new BufferedReader( new InputStreamReader( in ) );
String someString = bin.readLine( );

// write a byte
out.write(43);

// say goodbye
PrintWriter pout = new PrintWriter( out, true );
pout.println("Goodbye!");

// read a serialized Java object
ObjectInputStream oin = new ObjectInputStream( in );
Date date = (Date)oin.readObject(  );

client.close(  );
}

listener.close(  );
}
catch (IOException e ) { ... }
catch (ClassNotFoundException e2 ) { ... }

First, our server creates a ServerSocket attached to port 1234. On some systems, there are rules about what ports an application can use. Port numbers below 1024 are usually reserved for system processes and standard, well-known services, so we pick a port number outside of this range. The ServerSocket is created only once; thereafter, we can accept as many connections as arrive.

Next, we enter a loop, waiting for the accept( ) method of the ServerSocket to return an active Socket connection from a client. When a connection has been established, we perform the server side of our dialog, then close the connection and return to the top of the loop to wait for another connection. Finally, when the server application wants to stop listening for connections altogether, it calls the close( ) method of the ServerSocket.

No comments:

Post a Comment