Socket thread clients cant chat with each other - java

I wanted to program an chat server with sockets. I wanted , that at least two but theoretically even more can connect and chat in one big "room".
I have 2 classes for this, one server and one client class. But somehow they can only chat with themself.
The Server:
package Threads.tcp;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class ChatServer {
private static int PORT = 10023;
public static List<Socket> verbindungen = new ArrayList<>();
public static List<Thread> threads = new ArrayList<>();
public static void main(String[] args) {
// Server auf Port 10023 horchen lassen
try (ServerSocket server = new ServerSocket(PORT)) {
System.out.println("Echo-Server bereit am Port " + PORT);
while (true) {
// Auf ankommende Verbindung warten, accept() blockiert so
lange
Socket verbindung = server.accept();
verbindungen.add(verbindung);
Thread t = new Thread(new Client(verbindung));
threads.add(t);
t.start();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
The Client:
package Threads.tcp;
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class Client implements Runnable {
protected List<String> clientList = new ArrayList();
protected int ID;
protected Socket verbindung;
public Client( Socket verbindung) throws IOException {
this.verbindung = verbindung;
}
#Override
public void run() {
// Client-Thread (z.B. EchoHandler.java) ausgelagert werden.
Writer w = null;
try {
w = new OutputStreamWriter(verbindung.getOutputStream(),
Charset.forName("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(w);
Reader r = null;
try {
r = new InputStreamReader(verbindung.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(r);
try {
System.out.println("Verbindung angenommen von " +
verbindung.getRemoteSocketAddress());
w.write("Hallo dies ist der Chat Server der 3ci" + "\r\n");
w.flush();
w.write("Bitte /quit zum beenden):\r\n");
w.flush();
w.write("Welchen Spitznamen moechtest du haben: ");
w.flush();
BufferedReader name = new BufferedReader(new
InputStreamReader(verbindung.getInputStream()));
String clientName;
do {
clientName = name.readLine();
if (clientList.contains(clientName)) {
w.write("Der Spitzname ist schon vergeben waehle einen
anderen: ");
w.flush();
}
} while (clientList.contains(clientName));
// Add name to list
clientList.add(clientName);
ID = clientList.size() - 1;
w.write(clientList.get(ID) + " hat den Raum betreten." + "\r\n");
w.flush();
while (true) {
// Eingabeaufforderung senden und Zeile einlesen
bw.write(clientList.get(ID) + ">");
bw.flush();
String zeile = br.readLine();
if (zeile.equals("/quit")) {
clientList.remove(ID);
break;
}
if (zeile.trim().isEmpty()) {
bw.write("\r\n");
bw.flush();
} else {
zeile += "\r\n";
bw.write(zeile);
}
}
System.out.println("Verbindung beendet mit " +
verbindung.getRemoteSocketAddress());
verbindung.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I connected my clients with PUTTY and I used RAW , because I am programming on windows.
I hope you guys can help.
Regards Lukas.

Related

Java IOException Stream Closed in Server Client program

I am trying to make a Server Client program that allows sending multiple messages from server to client or vice versa without waiting for a response. The program works fine when the first client is connected and disconnected. But when I connect the client again, I get the error. Here is my server code:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
class Q2Server implements Runnable{
private ServerSocket serverSocket;
private Socket socket;
private DataOutputStream out;
private BufferedReader in1;
private DataInputStream in2;
private Thread read, write;
private String clientMsg, serverMsg;
public Q2Server (int port) throws IOException{
serverSocket = new ServerSocket(port);
while(true) {
try {
System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
socket = serverSocket.accept();
System.out.println("Just connected to " + socket.getRemoteSocketAddress());
out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("Thanks for connecting to " + socket.getLocalSocketAddress());
clientMsg = "";
serverMsg = "";
read = new Thread(this);
write = new Thread(this);
read.start();
write.start();
read.join();
write.join();
} catch(IOException e) {
e.printStackTrace();
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
}
public void run () {
try {
if(Thread.currentThread() == write) {
while(true) {
try {
if(clientMsg.equals("close")) {
break;
} else {
in1 = new BufferedReader(new InputStreamReader(System.in));
out = new DataOutputStream(socket.getOutputStream());
serverMsg = in1.readLine();
out.writeUTF(serverMsg);
if(serverMsg.equals("close")) {
socket.close();
in1.close();
in2.close();
out.close();
System.out.println("Closing connection...");
break;
}
}
} catch (SocketException s) {
break;
}
}
} else {
while(true) {
try {
if(serverMsg.equals("close")) {
break;
}
in2 = new DataInputStream(socket.getInputStream());
clientMsg = in2.readUTF();
System.out.println("Client: " + clientMsg);
if(clientMsg.equals("close")) {
socket.close();
in1.close();
in2.close();
out.close();
System.out.println("Closing connection...");
break;
}
} catch(SocketException s) {
break;
}
}
}
} catch (IOException i) {
i.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
Q2Server server = new Q2Server(8080);
}
}
Client code:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.rmi.UnexpectedException;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.lang.Thread;
class Q2Client implements Runnable {
private Socket socket;
private Thread read, write;
private BufferedReader in1;
private DataInputStream in2;
private DataOutputStream out;
private String clientMsg, serverMsg;
public Q2Client(int port) {
try {
socket = new Socket("localHost",port);
System.out.println("Connected to port: " + port);
clientMsg = serverMsg = "";
read = new Thread(this);
write = new Thread(this);
in2 = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
System.out.println(in2.readUTF());
read.start();
write.start();
read.join();
write.join();
} catch(UnexpectedException u) {
u.printStackTrace();
} catch(IOException i) {
i.printStackTrace();
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
public void run() {
try {
if(Thread.currentThread() == write) {
while(true) {
try {
if(serverMsg.equals("close")) {
break;
}
in1 = new BufferedReader(new InputStreamReader(System.in));
out = new DataOutputStream(socket.getOutputStream());
clientMsg = in1.readLine();
out.writeUTF(clientMsg);
if(clientMsg.equals("close")) {
socket.close();
in1.close();
in2.close();
out.close();
System.out.println("Closing connection...");
break;
}
} catch (SocketException s) {
break;
}
}
} else {
while(true) {
try {
if(clientMsg.equals("close")) {
break;
}
in2 = new DataInputStream(socket.getInputStream());
serverMsg = in2.readUTF();
System.out.println("Server: " + serverMsg);
if(serverMsg.equals("close")) {
socket.close();
in1.close();
in2.close();
out.close();
System.out.println("Closing connection...");
break;
}
} catch (SocketException s) {
break;
}
}
}
} catch (IOException i) {
i.printStackTrace();
}
}
public static void main(String[] args) {
Q2Client client = new Q2Client(8080);
}
}
Here is the stacktrace of the exception:
java.io.IOException: Stream closed
at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:176)
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:342)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
at Q2Server.run(Q2Server.java:65)
at java.base/java.lang.Thread.run(Thread.java:835)
When either of the server or client sends "close" the connection closes. The client can connect again. But when I run the client code again, I get the exception. What is going wrong and how do I fix this?
You're getting an exception because you're trying to read from a BufferedReader which no longer exists, the in1 in particular. At the first run, all your streams and readers open as they should, but after getting the command close from the client, your server closes the in1. Then, when the client tries to reconnect, the program tries to assign the value of in1.readLine() to serverMsg which is a String, but since in1 is no more, the IOException occurs since the BufferedReader is closed and nothing can be read from it.
I suppose since you want to leave the server running while the client(s) can connect and disconnect at any given time, which totally makes sense, maybe you shouldn't close the BufferedReader which supplies keyboard commands to the server in your case. Closing it doesn't make sense to me, since you're not stopping the whole server when the client disconnects, you just close the connection, but the server still should be able to accept commands.
Hope this helps.

Java Concurrent Socket Programming

Below is my code for a simple Concurrent Server. Whenever I run multiple clients, the server only prints out the input of the first client. I'm not sure what I've done wrong. Any help would be appreciated.
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8001);
while (true){
Socket clientSocket = serverSocket.accept();
System.out.println(clientSocket);
ConcurrentServer client = new ConcurrentServer(clientSocket);
client.start();
}
} catch (IOException i){}
}
public void run(){
try {
inputStream = new BufferedReader(new InputStreamReader(concurrentSocket.getInputStream()));
outputStream = new PrintWriter(new OutputStreamWriter(concurrentSocket.getOutputStream()));
String testString = inputStream.readLine();
System.out.println(testString);
} catch (IOException i){}
}
This code might help you to understand how to run multiple clients concurrently. :)
What this code does? TCP Client sends a string to the server and TCP server sends back the string in UPPERCASE format & the server can do this concurrently with multiple connections.
I have included 3 files for the server and one more for testing the server with multiple clients(ClientTest.java)
Main.java
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
new Server(3000).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;
public class Server {
private ServerSocket sSocket;
private boolean run;
private int port;
public Server(int port) throws IOException {
this.port = port;
this.sSocket = new ServerSocket(this.port);
}
public void start() {
this.run = true;
Logger.getLogger(getClass().getName()).info("Server is listening on port: " + port);
try {
while (run) {
Socket cs = sSocket.accept();
Logger.getLogger(getClass().getName())
.info("New Client Connected! " + cs.getPort());
new Thread(new Client(cs)).start(); // Put to a new thread.
}
} catch (IOException e) {
Logger.getLogger(getClass().getName()).severe(e.getMessage());
}
}
public void stop() {
this.run = false;
}
}
Client.java (Client Process on server)
import java.io.*;
import java.net.Socket;
import java.util.logging.Logger;
public class Client implements Runnable {
private Socket clientSocket;
private DataOutputStream out; // write for the client
private BufferedReader in; // read from the client
public Client(Socket clientSocket) {
this.clientSocket = clientSocket;
}
#Override
public void run() {
// Do client process
outToClient(inFromClient().toUpperCase());
closeConnection();
}
private String inFromClient() {
String messageFromClient = "";
/*
* Do not use try with resources because once -
* - it exits the block it will close your client socket too.
*/
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
messageFromClient = in.readLine();
} catch (IOException e) {
Logger.getLogger(getClass().getName()).severe("InFromClientErr - " + e.getMessage());
}
return messageFromClient.trim().equals("") ? "No Inputs given!" : messageFromClient;
}
private void outToClient(String message) {
try {
out = new DataOutputStream(clientSocket.getOutputStream());
out.writeBytes(message);
} catch (IOException e) {
Logger.getLogger(getClass().getName()).severe("OutToClientErr - " + e.getMessage());
}
}
private void closeConnection() {
try {
in.close();
out.close();
clientSocket.close();
} catch (NullPointerException | IOException e) {
Logger.getLogger(getClass().getName()).severe(e.getMessage());
}
}
}
ClientTest.java (For Testing clients)
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class ClientTest {
public static void main(String[] args) {
Socket clientSocket;
try {
clientSocket = new Socket("localhost", 3000);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.writeBytes(new Scanner(System.in).nextLine() + '\n'); // Get user input and send.
System.out.println(inFromServer.readLine()); // Print the server response.
} catch (IOException e) {
e.printStackTrace();
}
}
}
The issue was instead with the client. Not the server. The socket was declared outside of the for loop, and therefore only one connection was being created. Like so below:
public static void main(String[] args) {
try {
socket = new Socket("127.0.0.1", 8001);
for (int i = 0; i < 5; i++){
System.out.println("Starting client: " + i);
ConcurrentClient concurrentClient = new ConcurrentClient(socket, i);
concurrentClient.run();
}
} catch (IOException io) {
}
}
The Socket should be declared inside the for loop like so:
public static void main(String[] args) {
try {
for (int i = 0; i < 5; i++){
socket = new Socket("127.0.0.1", 8001);
System.out.println("Starting client: " + i);
ConcurrentClient concurrentClient = new ConcurrentClient(socket, i);
concurrentClient.run();
}
} catch (IOException io) {
}
}
I really don't know why you need so complex structure of input and output streams. It is better to use Scanner that will wait for the new input.
Also you can use PrintWriter to output the results of your conversation.
Here is server that accepts multiple clients:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ConcurrentServer extends Thread {
private Socket concurrentSocket;
public ConcurrentServer(Socket clientSocket) {
this.concurrentSocket = clientSocket;
}
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8001);
while (true){
Socket clientSocket = serverSocket.accept();
System.out.println(clientSocket);
ConcurrentServer client = new ConcurrentServer(clientSocket);
client.start();
}
} catch (IOException i){}
}
public void run(){
try {
InputStream inputStream = concurrentSocket.getInputStream();
Scanner scanner = new Scanner(inputStream);
OutputStream outputStream = concurrentSocket.getOutputStream();
PrintWriter pw = new PrintWriter(outputStream);
while(scanner.hasNextLine()){
String line = scanner.nextLine();
System.out.println(line);
pw.println("message: " + line);
pw.flush();
}
} catch (IOException i){}
}
}

My chat server is responding to the correct thread/client but send button must be clicked equal to the number of clients

I have made a chat server using socket that can handle multiple clients and can reply to individual clients..the client can chat only with the server..But as the number client increases, the server has to click send button as same as the client's number.....i.e; one click to reply when there is only one client connected...click twice when there is two clients and so on..... how can i solve it???
The server is like:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import javax.swing.SwingUtilities;
public class NewClass1{
private static ServerSocket serverSocket = null;
static Socket clientSocket = null;
private static final int maxClientsCount = 10;
private static final clientThread[] threads = new clientThread[maxClientsCount];
private static LinkedHashMap<String, clientThread> hm = new LinkedHashMap<String, clientThread>();
public static HashMap<String, clientThread> getinfo(){
return hm;
}
public static void main(String args[]) {
int portNumber = 22222;
if (args.length < 1) {
System.out.println("Usage: java MultiThreadChatServerSync <portNumber>\n"
+ "Now using port number=" + portNumber);
} else {
portNumber = Integer.valueOf(args[0]).intValue();
}
try {
serverSocket = new ServerSocket(portNumber);
} catch (IOException e) {
System.out.println(e);
}
while (true) {
try {
clientSocket = serverSocket.accept();
int i = 0;
for (i = 0; i < maxClientsCount; i++) {
if (threads[i] == null) {
(threads[i] = new clientThread(clientSocket, threads)).start();
System.out.println("hiiiiiiiiiii");
break;
}
}
if (i == maxClientsCount) {
PrintStream os = new PrintStream(clientSocket.getOutputStream());
os.println("Server too busy. Try later.");
os.close();
clientSocket.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
class clientThread extends Thread {
static String clientName = null;
private BufferedReader br=null;
private DataInputStream is = null;
private PrintStream os = null;
private static Socket clientSocket = null;
private final clientThread[] threads;
private int maxClientsCount;
private static HashMap<String, clientThread> hm = new HashMap<>();
public clientThread(Socket clientSocket, clientThread[] threads) {
this.clientSocket = clientSocket;
this.threads = threads;
maxClientsCount = threads.length;
this.hm=NewClass1.getinfo();
}
public void run() {
final int maxClientsCount = this.maxClientsCount;
final clientThread[] threads = this.threads;
try {
br= new BufferedReader(new InputStreamReader(System.in));
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
os.flush();
String name;
while (true) {
os.println("Enter your name.");
name = is.readLine().trim();
if (name.indexOf('#') == -1) {
break;
} else {
os.println("The name should not contain '#' character.");
}
}
System.out.println("\n"+name+" has joined");
os.println("Welcome " + name
+ " to our chat room.\nTo leave enter /quit in a new line.");
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] == this) {
clientName = "#"+name;
hm.put(clientName, this);
break;
}
}
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this) {
threads[i].os.println("*** A new user " + clientName
+ " entered the chat room !!! ***");
}
}
}
new Thread(new New()).start();
while(true){
String line=br.readLine();
if (line.startsWith("#")) {
String[] words = line.split("\\s", 2);
if (words.length > 1 && words[1] != null) {
words[1] = words[1].trim();
if (!words[1].isEmpty()) {
for(Map.Entry<String,clientThread> e:hm.entrySet()){
if(words[0].equals(e.getKey())){
clientThread get=e.getValue();
get.os.println("Server:\t" + words[1]);
os.flush();
System.out.println("\nServer to "+e.getKey()+":\t"+words[1]);
break;
}
else{
continue;
}
}
}}}
else { System.out.println("Please type #recipent's name.");
}}
} catch (IOException e) {
}
}
}
class New extends Thread{
public void run(){
DataInputStream is = null;
try {
is = new DataInputStream(NewClass1.clientSocket.getInputStream());
String lin=null;
while((lin=is.readLine())!=null)
{
//lin=is.readLine();
System.out.println(clientThread.clientName+": "+lin);
}
} catch (IOException ex) {
Logger.getLogger(New.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
is.close();
} catch (IOException ex) {
Logger.getLogger(New.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
The client is like:
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class NewClass implements Runnable {
// The client socket
private static Socket clientSocket = null;
// The output stream
private static PrintStream os = null;
// The input stream
private static DataInputStream is = null;
private static BufferedReader inputLine = null;
private static boolean closed = false;
public static void main(String[] args) {
// The default port.
int portNumber = 22222;
// The default host.
String host = "localhost";
if (args.length < 2) {
System.out
.println("Usage: java MultiThreadChatClient <host> <portNumber>\n"
+ "Now using host=" + host + ", portNumber=" + portNumber);
} else {
host = args[0];
portNumber = Integer.valueOf(args[1]).intValue();
}
/*
* Open a socket on a given host and port. Open input and output streams.
*/
try {
clientSocket = new Socket(host, portNumber);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + host);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the host "
+ host);
}
/*
* If everything has been initialized then we want to write some data to the
* socket we have opened a connection to on the port portNumber.
*/
if (clientSocket != null && os != null && is != null) {
try {
/* Create a thread to read from the server. */
new Thread(new NewClass()).start();
while (!closed) {
String msg=inputLine.readLine();
os.println(msg.trim());
os.flush();
System.out.println("\nClient: \t"+msg);
}
/*
* Close the output stream, close the input stream, close the socket.
*/
os.close();
is.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
/*
* Create a thread to read from the server. (non-Javadoc)
*
* #see java.lang.Runnable#run()
*/
public void run() {
/*
* Keep on reading from the socket till we receive "Bye" from the
* server. Once we received that then we want to break.
*/
String responseLine;
try {
while ((responseLine = is.readLine()) != null) {
System.out.println("\n"+responseLine);
if (responseLine.indexOf("*** Bye") != -1)
break;
}
closed = true;
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}

Java: I don't get the messages from other clients?

does anyone know whats wrong with my code?
When I write something with client1 i just see it on the server and on the client1 but not on client2.
run() in Client.java:
public void run() {
Scanner input = new Scanner(System.in);
try {
Socket client = new Socket(host, port);
System.out.println("client started");
OutputStream out = client.getOutputStream();
PrintWriter writer = new PrintWriter(out);
InputStream in = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String i = input.nextLine();
writer.write(clientname + ": " + i + newline);
writer.flush();
String s = null;
while((s = reader.readLine()) != null) {
System.out.println(s);
}
writer.close();
reader.close();
client.close();
}
If you need the Server code or anything else just ask.
Thanks in advance!!
Additionally the Server:
public class Server {
public static void main(String[] args) {
int port = 40480;
int max = 10;
ExecutorService executor = Executors.newFixedThreadPool(max);
try {
ServerSocket server = new ServerSocket(port);
System.out.print("server started" + "\n");
while(true) {
try {
Socket client = server.accept();
executor.execute(new Handler(client));
}
catch (IOException e) {
e.printStackTrace();
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
And the Handler:
public class Handler implements Runnable{
private Socket client;
public Handler(Socket client) {
this.client = client;
}
#Override
public void run() {
try {
OutputStream out = client.getOutputStream();
PrintWriter writer = new PrintWriter(out);
InputStream in = client.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String s = null;
while((s = reader.readLine()) != null) {
writer.write(s + "\n");
writer.flush();
System.out.println(s);
}
writer.close();
reader.close();
client.close();
}
catch(Exception e) {
}
}
}
This is an example - it is not complete but should give you an idea how you could multicast output to a number of listening clients. There are better ways to do this, but I wrote it similar to how you appeared to be doing the sockets. It also lacks error checking in many places and I have left that as an exercise for the reader. This code was also written so that it can be used on Java 1.6 or higher.
The code uses a list of connected Clients maintained in the Server object. When input is received from one client, the output is multicast to each client in the Client list. Writing is done via a write method in the Client class.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MulticastEchoServer {
List<Client> clientList = new LinkedList<Client>();
ExecutorService executor;
int port = 40480;
int max = 10;
public MulticastEchoServer() {
this.executor = Executors.newFixedThreadPool(max);
}
public void writeToAllClients(String string) throws IOException {
// Multiple threads access this so it must be in synchronized block
synchronized (this.clientList) {
Iterator<Client> iter = this.clientList.iterator();
while (iter.hasNext())
iter.next().write(string);
}
}
public void addClient(Client client) {
// Multiple threads access this so it must be in synchronized block
synchronized (this.clientList) {
clientList.add(client);
}
}
public void removeClient(Client client) {
// Multiple threads access this so it must be in synchronized block
synchronized (this.clientList) {
clientList.remove(client);
}
}
public void listen() {
try {
ServerSocket server = new ServerSocket(port);
System.out.println("server started and listening for connections");
while (true) {
try {
Socket socket = server.accept();
System.out.print("connection accepted" + "\n");
Client newClient = new Client(this, socket);
this.addClient(newClient);
this.executor.execute(newClient);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new MulticastEchoServer().listen();
}
private class Client implements Runnable {
Socket socket;
PrintWriter writer;
BufferedReader reader;
MulticastEchoServer server;
public Client(MulticastEchoServer server, Socket socket) throws IOException {
this.server = server;
this.socket = socket;
this.writer = new PrintWriter(this.socket.getOutputStream());
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
synchronized public void write(String string) throws IOException {
writer.write(string);
writer.flush();
}
public void close() {
this.writer.close();
try {
this.reader.close();
} catch (IOException e) {
}
try {
this.socket.close();
} catch (IOException e) {
}
}
#Override
public void run() {
System.out.println("Client Waiting");
String inString = null;
try {
while ((inString = this.reader.readLine()) != null) {
this.server.writeToAllClients(inString + "\n");
System.out.println(inString);
}
} catch (IOException e1) {
}
server.removeClient(this);
this.close();
System.out.println("Client Closed");
}
}
}
In your handler:
while((s = reader.readLine()) != null) {
writer.write(s + "\n");
writer.flush();
System.out.println(s);
}
You are only writing the string back to the sender, not to all connected sockets

Java socket - simple program doesn't work

I've spent lot of time to find out where is the problem but with no success. Server is launching correctly, but when I launch Client I get "Unexpected Error" exception. I've changed ports too with no effects. What should I do to make this working?
/* Server.java */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private static final int PORT = 50000;
static boolean flaga = true;
private static ServerSocket serverSocket;
private static Socket clientSocket;
public static void main(String[] args) throws IOException
{
serverSocket = null;
try
{
serverSocket = new ServerSocket(PORT);
}
catch(IOException e)
{
System.err.println("Could not listen on port: "+PORT);
System.exit(1);
}
System.out.print("Wating for connection...");
Thread t = new Thread(new Runnable()
{
public void run()
{
try
{
while(flaga)
{
System.out.print(".");
Thread.sleep(1000);
}
}
catch(InterruptedException ie)
{
//
}
System.out.println("\nClient connected on port "+PORT);
}
});
t.start();
clientSocket = null;
try
{
clientSocket = serverSocket.accept();
flaga = false;
}
catch(IOException e)
{
System.err.println("Accept failed.");
t.interrupt();
System.exit(1);
}
final PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);
final BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
t = new Thread(new Runnable()
{
public void run()
{
try
{
Thread.sleep(5000);
while(true)
{
out.println("Ping");
System.out.println(System.currentTimeMillis()+" Ping sent");
String input = in.readLine();
if(input.equals("Pong"))
{
System.out.println(System.currentTimeMillis()+" Pong received");
}
else
{
System.out.println(System.currentTimeMillis()+" Wrong answer");
out.close();
in.close();
clientSocket.close();
serverSocket.close();
break;
}
Thread.sleep(5000);
}
}
catch(Exception e)
{
System.err.println(System.currentTimeMillis()+" Unexpected Error");
}
}
});
t.start();
}
}
and the Client class
/* Client.java */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client
{
private static final int PORT = 50000;
private static final String HOST = "localhost";
public static void main(String[] args) throws IOException
{
Socket socket = null;
try
{
socket = new Socket(HOST, PORT);
}
catch(Exception e)
{
System.err.println("Could not connect to "+HOST+":"+PORT);
System.exit(1);
}
final PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Thread t = new Thread(new Runnable()
{
public void run()
{
long start = System.currentTimeMillis();
while (true)
{
try
{
String input = in.readLine();
if (input != null)
{
System.out.println(System.currentTimeMillis() + " Server: " + input);
}
if (input.equals("Ping"))
{
if(System.currentTimeMillis()-start>30000)
{
out.println("Pon g");
System.out.println(System.currentTimeMillis() + " Client: Pon g");
break;
}
out.println("Pong");
System.out.println(System.currentTimeMillis() + " Client: Pong");
}
}
catch (IOException ioe)
{
//
}
}
}
});
t.start();
out.close();
in.close();
socket.close();
}
}
Here is the output on running
Wating for connection............
Client connected on port 50000
1368986914928 Ping sent
java.lang.NullPointerException
at Server$2.run(Server.java:84)
at java.lang.Thread.run(Thread.java:722)
You're making a big mistake with those catch blocks that are empty or print out your useless message.
You'll get more information if you print or log the stack trace. It's simply a must.
You need some intro instruction - have a look at this and see how it's different from yours.
http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
It shows your out object is null. Instead of input.equals("Pong") use input != null && input.equals("Pong") in line 84 of Server.java. I guess you would have received Pong received but in later stages when you are listening to nothing you could have got this NPE.

Categories

Resources