0% found this document useful (0 votes)
8 views1 page

Java IMServer Implementation

This document contains the code for an IMServer class written in Java. The IMServer class extends the Thread class and runs a server on port 7777 that accepts incoming client connections and broadcasts messages between all connected clients. When a new client connects, it is added to a pool of PrintWriters and its messages are broadcast to all other clients. When a client disconnects, it is removed from the pool.

Uploaded by

hj43us
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Java IMServer Implementation

This document contains the code for an IMServer class written in Java. The IMServer class extends the Thread class and runs a server on port 7777 that accepts incoming client connections and broadcasts messages between all connected clients. When a new client connects, it is added to a pool of PrintWriters and its messages are broadcast to all other clients. When a client disconnects, it is removed from the pool.

Uploaded by

hj43us
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd

Archivo: /home/misan/workplace/IMServer/IMServer.

java Página 1 de 1

import [Link].*;
import [Link].*;
import [Link].*;

class IMServer extends Thread {


Scanner in;
PrintWriter out;
static ArrayList<PrintWriter> pool = new ArrayList<PrintWriter>();

public IMServer(Socket s) throws IOException {


in = new Scanner([Link]());
out = new PrintWriter([Link](),true);
[Link](out);
start();
}

public static void main(String args[]) throws IOException {


ServerSocket ss = new ServerSocket(7777);
while(true) new IMServer([Link]());
}

public void broadcast(String l ) {


synchronized (pool) { for(PrintWriter output : pool) // NEW HERE
if ( ![Link](out) ) [Link](l); }
}

public void run() {


broadcast("NEW CLIENT");
try {
while(true) {
String line = [Link]();
if ([Link]("quit")) break; else broadcast(line);
}
} catch (Exception e) {[Link]("Exception "+e);}
synchronized (pool) {[Link](out);} // NEW HERE
[Link]();
broadcast("CLIENT GONE");
}
}

You might also like