All pastes #250073 Raw Edit

Someone

public text v1 · immutable
#250073 ·published 2006-11-16 16:20 UTC
rendered paste body

import java.net.*;
import java.io.*;

public class ServerThreadedRandomNumbers extends Thread {//Thread Technology!
	
	String randomNumbers ;
	InputStreamReader in = null;
	BufferedReader buf = null;
	PrintWriter out = null;
	Socket theConnection;
	int Num = 25; 					// Largest random number issued by server
	
	//constructor: used to pass new connection (thread) 
	public ServerThreadedRandomNumbers(Socket s) {
		theConnection = s;
	}
	
	public void run() { // implements the processes of the thread
		
		try {
			// the buffer used for writing/reading strings from/to the socket
			out = new PrintWriter(theConnection.getOutputStream(), true);
			buf = new BufferedReader(new InputStreamReader(theConnection. getInputStream()));
			// Send "GO" command to the client
			out.println("GO");
			
			while (true) {
				String as = buf.readLine(); // read client's requests
				if (as==null) break;
				// Issue random numbers to client 
				if (as.equals("1")){
					int a=(int)(Math.random() * Num);		
					randomNumbers=String.valueOf(a);
					randomNumbers=randomNumbers.concat(" ");
					System.out.print("Pack send to client: "+theConnection.getInetAddress().toString()
							+":"+theConnection.getPort() );
					System.out.println("// randomNumbers issued: "+randomNumbers);					
					out.println(randomNumbers);	 // Send random number to client
					
				}
				
			}
			System.out.println("Finished client:" + theConnection.getInetAddress().toString()+":"+theConnection.getPort());
			
		}
		catch (IOException ex) {
		}
	}

}