Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
import java.net.Socket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;

/**
* A ChatConnection implements a connection between the chat server and a
* single chat client. The connection will run as a separate Thread.
*/
public class ChatConnection extends Thread{

/**
* The socket on which this connection will listen
*/
/**
* The server that this connection is associated with.
*/
ChatServer server;
/**
* The output stream that will be used to write messages to our client
*/
BufferedOutputStream out;
/**
* The input stream to read and receive messages from our client
*/
BufferedInputStream in;

/**
* Create a new ChatConnection. The socket and the server are initialised
* @param s The socket on which this connection will listen
* @param svr The server that this connection is associated with
*/
public ChatConnection(Socket s, ChatServer svr) {
socket = s;
server = svr;
}

/**
* Override the run() method of Thread. This method will read input from
* out client and broadcast that input to all clients of this ChatServer.
* All output is broadcast using the MultiOutputStream that the Server
* maintains.
*/
public void run() {
int numBytesRead;
byte[] buf = new byte[256];
try {
// Get the input stream from our socket
/**
* Enter your code Here
*/
// Get the output stream from our socket
/**
* Enter your code Here
*/
// Add our output stream to the server's MultiOutputStream
server.addOutputStream(out);
System.out.println("New ChatConnection now reading...");
// Loop to read from the input stream
while ((numBytesRead = in.read(buf,0,buf.length)) > -1) {
System.out.print("Broadcast: " + new String(buf,0,numBytesRead));
// broadcast everything we receive
server.broadcast(buf,numBytesRead);
}
} catch (IOException e) {
} finally {
// The connection to our client has been closed so
// we need to remove our output stream from the server's
// MultiOutputStream and also close our socket.
try {
if (out != null) {
server.removeOutputStream(out);
}
if (socket != null) {
//Close the Socket
/**
* Enter your code Here
*/
}
} catch (IOException e) {}
}
}
}

What I have tried:

I have tried everything and nothing seems to be working for me, i have less than 12 hours to get this completed.
Posted
Updated 16-Jul-16 21:35pm
Comments
Patrice T 16-Jul-16 20:50pm    
Help you to what ?
What is the problem ?
What is supposed to do the code ?

1 solution

What you have posted is a framework that presumably your tutor provided - it's full of helpful bits such as:
Java
// Get the input stream from our socket
/**
* Enter your code Here 
*/
which are telling you exactly what to do, and where to do it.
This is your homework: you are expected to think about it and the material covered in your course and apply the one to the other.
Until you do, there is no help we can give you: we are not here to do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

[edit]typo[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900