All pastes #986311 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-04-15 18:31 UTC
rendered paste body
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.io.*;


class Thread1 extends Thread {
	mailbox box;
	Thread1(mailbox box){
		this.box = box;
		
		

	}
	public void run(){
		int i = 0;
		
		ServerSocket socket = null;
		int port = 25005;
		try {
			socket = new ServerSocket(port);
		} catch (IOException e) {
			System.out.println("Could not create socket!");
			System.exit(1);
		}
		client[] clients = new client[10];
		ThreadClear clear = new ThreadClear(box, clients); 
		clear.start();

		while(true){

			Socket clientSocket = null;
			try{
				clientSocket = socket.accept();
			}catch (IOException e) {
				System.out.println("Could not accept socket!");
				System.exit(1);
			}
			InetAddress clientInet = clientSocket.getInetAddress();
			System.out.println(new String(clientInet.toString()));
			clients[i] = new client();
			clients[i].startConnection(clientSocket,box);
			clear.setNbrOfClients(i);
			i++;


		}
	}
}


class clientThread extends Thread{
	InputStreamReader inR;
	InputStream in = null;
	OutputStream out = null;
	BufferedReader br = null;
	mailbox box;

	clientThread(Socket clientSocket,mailbox box){
		this.box = box;
		try{
			
			in = clientSocket.getInputStream();
			inR = new InputStreamReader (  in  );
			br = new BufferedReader(inR); 
			
			out = clientSocket.getOutputStream();
		} catch (Exception e) {
			System.err.println(e);
		}
	}
	public void run(){
		synchronized(box){
		String s = "";
		while(true){
			while(!box.isEmpty()){
				try{
					box.wait();

				}catch (InterruptedException e){
					System.exit(1);
				}
			}
			s = "";
			try{
				s = s+br.readLine() +"\n";
			}catch(IOException e){
				System.err.println(e);
				System.exit(1);
			}
			box.assign(s);
			box.notifyAll();


		}

		}
	}
	public void send(String s){
		try{
			out.write(s.getBytes());
		}catch (Exception e) {
			System.err.println(e);
			System.exit(1);

		}
	}
}







class client{
	clientThread thirdThread;
	client(){
		
	}
	public void startConnection(Socket clientSocket, mailbox box){

		
		this.thirdThread = new clientThread(clientSocket,box);
		thirdThread.start();
	}
	public void send(String s){
		thirdThread.send(s);
	}
}

class mailbox {
	String message;

	mailbox(String message){
		this.message = message;
	}
	public void assign(String s){
		this.message = s;

	}
	public boolean isEmpty(){
		if(message.equals("")){
			return true;
		}
		return false;
	}
	public String erase(){
		String s = message;
		message ="";
		return s;
	}
}

class ThreadClear extends Thread{
	mailbox box;
	client[] clients;
	int nbrOfClients = 0;
	ThreadClear(mailbox box, client[] clients){
		this.box = box;
		this.clients = clients;
	}
	public void setNbrOfClients(int nbrOfClients){
		this.nbrOfClients = nbrOfClients;
	}
	public void run(){
		String s;
		synchronized(box){
			while(true){
				s = "";

				while(box.isEmpty()){
					try{
						box.wait();

					}catch (InterruptedException e){
						System.exit(1);
					}
				}
				s = box.erase();
				if(s.equals("Q")){
					System.exit(1);
				}
				for(int i = 0; i<=nbrOfClients;i++){
					clients[i].send(s);
				}
				box.notifyAll();



			}
		}

	}
}

public class chatS{
	public static void main(String args[]) {
		mailbox box = new mailbox("");

		Thread1 firstThread = new Thread1(box);
		firstThread.start();

	}
}