File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Socket Server Serving Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket Server Serving Numbers" Watch "Socket Server Serving Numbers" New topic

Socket Server Serving Numbers

Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91

This time the post IS related to Sockets.

Objective : To modify the program so that when run() is invoked the println() which currently prints out Thread ID's and Numbers to stdout, will do the SAME on a Socket.

I would appreciate ANY hint/help.

1. I don't think i should move the "creation" of ServerSocket to run(), since that would result in creation of ServerSocket objects
based on the for (int i = 0; i < 5; i++) loop. Just wanted to Confirm.

2. Should i move the whole ServerSocket, i,e Instantiating a ServerSocket, Listening on a port , instantiating a Socket etc... to ?

3. Should i create a new class SocketServer ? and call SocketServer form my main()




public class Server implements Runnable {
int id;
public static final int PORT = 8088;
//static final's are difined above main()

public static void main(String[] args)
throws java.lang.InterruptedException, IOException {

ServerSocket s = new ServerSocket(PORT, 1000);
System.out.println("Server Started: " + s);

Socket socket = s.accept();
System.out.println("after s.accept.... : ");

BufferedReader in =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("after BufferedReader.... : ");

for (int i = 0; i < 5; i++) {
Server runnable = new Server();
Thread thread = new Thread(runnable);

while (true);
public void run() {
while (true) {

int num = NumberServer.getInstance().getNextNumber();
System.out.println("id: " + + ": " + num);
try {
} catch (InterruptedException e) {

public void setId(int i) {
id = i;



public class NumberServer {

//There is a static variable called NumberServerInstance (of type NumberServer).
//This is to make Sure that ONLY One instance of NumberServer is created

private static NumberServer NumberServerInstance = new NumberServer();
private static int count = 0;
int port= 10013;
private Socket server;

// Exists only to defeat instantiation.
// Now, the reason i put Creation of Socket here is that ONLY one instance is
//guranteed to be created, when NumberServer is instantiated
private NumberServer() {

//getInstance method which returns an instance of the Class NumberServer
//to get an instance of that class "Numberserver" , we have the static getInstance method
public static NumberServer getInstance() {
return NumberServerInstance;

//accessor Method
public int getNextNumber() {
//retval variable is used to store value before increment it via count++;
int retval = count;
return retval;
//This method will send the same Data as is sent by getNextNumber, Over a Socket

public void sendNumOverSocket() {


//When implementing a server you also need to create a socket object from the
//ServerSocket in order to listen for and accept connections from clients.



I agree. Here's the link:
subject: Socket Server Serving Numbers
It's not a secret anymore!