aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket Server Serving Numbers" Watch "Socket Server Serving Numbers" New topic
Author

Socket Server Serving Numbers

Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Hi,

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
NumberServer.java ?

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

Thanks,

-Kamal.


Server.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

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();
runnable.setId(i);
Thread thread = new Thread(runnable);
thread.start();
}

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

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

e.printStackTrace();
}
}
}
public void setId(int i) {
id = i;
}
}


----

NumberServer.java


import java.net.Socket;

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;



//constructor
// 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;
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.

}


Thanks,

-Kamal.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket Server Serving Numbers
 
Similar Threads
Global Variables in Java
Gloal Variables in Java
Producer/Consumer Design impacts exception handling
Socket is not receiving the first request after long Ideal time
Singleton ( a Deeper Understanding )