• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Socket Server Serving Numbers

Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()




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();
Thread thread = new Thread(runnable);

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

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

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



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;

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



You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic