Client-Server Multi-Threading - java

I am having a problem implementing a simple multi threaded server in Java.
My idea is for a server to broadcast a message to all clients(without the use of a multicast).
For this I am implementing a broadcast method. This method will use a for loop, and loop through each client Thread, which I have stored in a collection. It will call each threads send(), which outputs writeUTF().
My problem is for 2 clients A+B.
A outputs: hello
B outputs: hi there
B will not recieve hello, and when B type again, it will the recieve A's message.Code example:
import java.net.*;
import java.io.;
import java.util.;
public class ServerThreadHandler extends Thread {
private AuctionServer server = null;
private Socket socket = null;
private String name = null;
private int ID = -1;
private DataInputStream dataIn = null;
public DataOutputStream dataOut = null;
private Thread thread;
protected static Vector handlers = new Vector();
// reason server is used here is because ian was calling a server method broadcast
// from inside the
public ServerThreadHandler(AuctionServer server, Socket socket, String name) throws IOException{
this.server = server;
this.socket = socket;
this.name = name;
dataIn = new DataInputStream( new
BufferedInputStream(socket.getInputStream()));
dataOut = new DataOutputStream(socket.getOutputStream());
}
// handles a specific client.
public void run(){
System.out.println("Server running..");
while(true){
try{
// broadcast to all clients. This will only be one client in this case.
server.broadcast(dataIn.readUTF());
int pause = (int)(Math.random() * 3000);
Thread.sleep(pause);
}
catch(IOException e){
System.out.println(e.getMessage());
}
catch(InterruptedException e){
System.out.println(e.getMessage());
}
}
}
public void send(String msg){
try{
dataOut.writeUTF(msg);
dataOut.flush();
}
catch(IOException e){
}
}
Server code:
// broadcast this to clients.
public void broadcast(String msg){
for(int i = 0; i < clientCount; i++){
clients[i].send(msg);
}
}
Where clients[] is
private ServerThreadHandler clients[] = new ServerThreadHandler[3];

I'm going to give you a rough outline, you can fill in the rest of the details:
public class Server {
private ServerSocket serverSocket;
private List<Socket> clientSockets;
// constructor(s)
private class ThreadTask implements Runnable {
private Socket clientSocket;
public ThreadTask(Socket clientSocket) {
this.clientSocket = clientSocket;
}
#Override
public void run() {
while(true) {
// read message from clientSocket
for(Socket peerSocket: clientSockets) {
// send message on peerSocket
}
}
}
}
private void acceptConnections() {
while(true) {
try {
Socket clientSocket = serverSocket.accept();
clientSockets.add(clientSocket);
Thread t = new Thread(new ThreadTask(clientSocket));
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Related

Server won't connect to more than one client (Java)

my server class
import java.net.*;
import java.io.*;
public class server
{
private Socket socket;
private ServerSocket server;
// constructor with port
public void start(int port){
try {
server = new ServerSocket(port);
while(true){
socket = server.accept();
new ConnectionHandler(socket).run();
}
}catch(IOException i){
}
}
}
class ConnectionHandler implements Runnable{
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
public ConnectionHandler(Socket socket){
this.socket=socket;
}
#Override
public void run() {
InputStream inp = null;
BufferedReader brinp = null;
DataOutputStream out = null;
try
{
System.out.println("Waiting for a client ...");
System.out.println(server);
System.out.println("Client accepted");
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String line = "";
// reads message from client until "Over" is sent
while (!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String[] args) {
server serverr = new server();
serverr.start(4000);
}
}
Here's my client class.
import java.net.*;
import java.io.*;
public class Client
{
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
String line = "";
// keep reading until "Over" is input
while (!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 4000);
}
}
Trying to develop pretty simple chat application works via terminal, but I think there are plenty of bugs I have in my code.
The server can handle one client, but when another client comes up it doesn't connect to other clients.
What am I have to do now?
I couldn't find out where my problem is, waiting your helps.
Note: I am completely new to socket programming concept.
The ConnectionHandler class is a thread class, and you must wrap its object to a Thread instance and then call start() instead of run().
So in the Server class change
new ConnectionHandler(socket).run();
with
new Thread(ConnectionHandler(socket)).start();

How to stop continuous execution of while loop with same connection

I am making a server for a Chat application in Java.
The while loop is supposed to connect to new clients but the code keeps connecting to the first client repeatedly even after it has connected resulting in Bind Failed error. What should I change?
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ServerM
{
public static void main(String args[])
{
while(true)
{
Listener l = new Listener();
l.run();
}
}
}
class Listener implements Runnable
{
static InetAddress arr[] = new InetAddress[10];
static int i = 0;
public void run()
{
try
{
ServerSocket ss = new ServerSocket(44444);
System.out.println("Waiting...");
Socket s = ss.accept();
System.out.println("Connected!\n");
DataInputStream din=new DataInputStream(s.getInputStream());
String ip = din.readUTF();
InetAddress addr = InetAddress.getByName(ip);
for(int j=0; j<=i; j++)
{
if(arr[j] == addr)
return;
}
arr[i++] = addr;
ChatThread c = new ChatThread(addr,s);//This creates a thread to allow communication with Client
c.run();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Your problem is in the design of the solution. You are running a multiple instances of the Server Socket (and using the same port, what will end in an exception) and using this thread for getting the client connections, therefore you can only have one connection.
What you should do is, run a Thread for the Server Socket who will listen for all the connections, and then run every client instance (new Thread) in an infinite loop.
public class ServerM
{
public static void main(String args[])
{
Listener l = new Listener();
l.run();
}
}
class Listener implements Runnable
{
static InetAddress arr[] = new InetAddress[10];
static int i = 0;
public void run()
{
try
{
ServerSocket ss = new ServerSocket(44444);
System.out.println("Waiting...");
while (true) {
Socket s = ss.accept();
ClientListener clientListener = new ClientListener(s);
clientListener.run();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class ClientListener implements Runnable {
private Socket socket;
public ClientListener(Socket socket) {
this.socket = socket;
}
public void run() {
System.out.println("Connected!\n");
DataInputStream din=new DataInputStream(s.getInputStream());
String ip = din.readUTF();
InetAddress addr = InetAddress.getByName(ip);
for(int j=0; j<=i; j++)
{
if(arr[j] == addr)
return;
}
arr[i++] = addr;
ChatThread c = new ChatThread(addr,socket);
c.run();
}
}
You have to do this because you only need one instance of ServerSocket listening for new connections at a specific port, and then you need [1..n] client instances for handling each connection.
Catch Exception is bad practice
catch(Exception e)
{
e.printStackTrace();
}
Try to forward an custom exception (MyException.class) to the top level and handle this exception in ServerM class in try..catch. And don't forget to close socket in finally block
ss.close();
s.close();

Stopping java threads [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I wrote a java application for both Server and Client. What I want to do is stop the Client's application(and all of it's threads) when the user enters the word: "logout". I've tried everything I could find so kinda desperate here. Please send help!
Here is my code for Client.java
package client;
//Java implementation for multithreaded chat client
//Save file as Client.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client extends Thread
{
final static int ServerPort = 1234;
private volatile static boolean running = true;
public static void main(String args[]) throws UnknownHostException, IOException
{
Scanner scn = new Scanner(System.in);
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection
Socket s = new Socket(ip, ServerPort);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// sendMessage thread
Thread sendMessage = new Thread(new Runnable()
{
#Override
public void run() {
while (running) {
// read the message to deliver.
try {
String msg = scn.nextLine();
if(msg == "logout") {
running = false;
dis.close();
dos.close();
scn.close();
s.close();
Thread.currentThread().interrupt();
break;
}
dos.writeUTF(msg);
}
catch (IOException e) {
if(!running) {
System.out.println("Closing...");
System.exit(0);
}
}
} }
});
// readMessage thread
Thread readMessage = new Thread(new Runnable()
{
#Override
public void run() {
while (running) {
// read the message sent to this client
try {
String msg = dis.readUTF();
if(sendMessage.isInterrupted()) {
running = false;
dis.close();
dos.close();
scn.close();
s.close();
Thread.currentThread().interrupt();
break;
}
System.out.println(msg);
} catch (IOException e) {
if(!running) {
System.out.println("Closing...");
System.exit(0);
}
}
}
}
});
sendMessage.start();
readMessage.start();
}
}
And this is my Server.java
package server;
//Java implementation of Server side
//It contains two classes : Server and ClientHandler
//Save file as Server.java
import java.io.*;
import java.util.*;
import java.net.*;
//Server class
public class Server
{
// Vector to store active clients
static Vector<ClientHandler> ar = new Vector<>();
// counter for clients
static int i = 0;
public static void main(String[] args) throws IOException
{
// server is listening on port 1234
ServerSocket ss = new ServerSocket(1234);
Socket s;
// running infinite loop for getting
// client request
while (true)
{
// Accept the incoming request
s = ss.accept();
System.out.println("New client request received : " + s);
// obtain input and output streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Creating a new handler for this client...");
// Create a new handler object for handling this request.
ClientHandler mtch = new ClientHandler(s,"client " + i, dis, dos);
// Create a new Thread with this object.
Thread t = new Thread(mtch);
System.out.println("Adding this client to active client list");
// add this client to active clients list
ar.add(mtch);
// start the thread.
t.start();
// increment i for new client.
// i is used for naming only, and can be replaced
// by any naming scheme
i++;
}
}
}
//ClientHandler class
class ClientHandler implements Runnable
{
private String name;
final DataInputStream dis;
final DataOutputStream dos;
Socket s;
boolean isloggedin;
// constructor
public ClientHandler(Socket s, String name,
DataInputStream dis, DataOutputStream dos) {
this.dis = dis;
this.dos = dos;
this.name = name;
this.s = s;
this.isloggedin=true;
}
#Override
public void run() {
String received;
while (true)
{
try
{
// receive the string
received = dis.readUTF();
if(received.equals("logout")){
break;
}
// break the string into message and recipient part
StringTokenizer st = new StringTokenizer(received, "#");
String MsgToSend = st.nextToken();
String recipient = st.nextToken();
// search for the recipient in the connected devices list.
// ar is the vector storing client of active users
for (ClientHandler mc : Server.ar)
{
// if the recipient is found, write on its
// output stream
if (mc.name.equals(recipient) && mc.isloggedin==true)
{
mc.dos.writeUTF(this.name+" : "+MsgToSend);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
try
{
// closing resources
this.dis.close();
this.dos.close();
this.s.close();
this.isloggedin=false;
}catch(IOException e){
e.printStackTrace();
}
}
}
Code reference: Multithread GroupChat 1
Multithread GroupChat 2
Don't compare Strings with == but with equals(). msg == "logout" Should be msg.equals("logout").

Multithreading Java Socket

I have been asked to take this post down, and in particular the code, by a superior of mine
Problem 1: Client did not receive message
Solution: Make sure port matches sending's port
Problem 2: Could not broadcast message
Solution: Use a broadcast address
// Client REceive
DatagramSocket socket = new DatagramSocket(null);
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress("127.0.0.1", 4002));
// ClientSEnd
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
socket = new DatagramSocket();
socket.setReuseAddress(true);
Just add the port number to the Datagram socket in the receive. it will work fine.
Class - ClientReceive:
DatagramSocket socket = new DatagramSocket(4001);
Set the reuse address to true ..
that will make use of the address whatever it is 4001 4002..etc
socket.setReuseAddress(true);
The problem seems to be that DatagramSocket allows you to send a datagram to a given destination. In your case you are sending to localhost, so that all messages are sent to the local machine only and not to other clients. If you want to reach all network clients should use a broadcast address or use the MulticastSocket class instead DatagramSocket.
import java.net.*;
import java.io.*;
public class ClientSend implements Runnable
{
private Thread t;
private DatagramSocket socket;
private String name;
private String sendingMessage;
private int port;
public ClientSend(int port)
{
this.port = port;
}
public void run()
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
socket = new DatagramSocket();
socket.setReuseAddress(true);
while(true)
{
sendingMessage = br.readLine();
byte sendingData[] = sendingMessage.getBytes();
InetAddress clientAddress = InetAddress.getByName("224.0.0.3");
DatagramPacket sendingPacket = new DatagramPacket(sendingData, sendingData.length, clientAddress, 4011);
socket.send(sendingPacket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void start()
{
t = new Thread(this);
t.start();
}
public static void main(String args[]) throws Exception
{
ClientSend CS = new ClientSend(4011);
CS.start();
}
}
import java.net.;
import java.io.;
public class ClientReceive implements Runnable
{
private Thread t;
public ClientReceive()
{
}
public void run()
{
try {
// DatagramSocket socket = new DatagramSocket(null);
MulticastSocket socket = new MulticastSocket(4011);
InetAddress group = InetAddress.getByName("10.10.222.120");
socket.joinGroup(group);
//socket.setReuseAddress(true);
//socket.bind(new InetSocketAddress("10.10.222.120", 4011));
while(true)
{
byte receivingData[] = new byte[1024];
DatagramPacket receivingPacket = new DatagramPacket(receivingData, receivingData.length);
socket.receive(receivingPacket);
String receivingMessage = new String(receivingPacket.getData(), 0, receivingPacket.getLength());
System.out.println("Received: " + receivingMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void start()
{
t = new Thread(this);
t.start();
}
public static void main(String args[]) throws Exception
{
ClientReceive CR = new ClientReceive();
CR.start();
}
}

Java Socketing: My server class constantly takes in input but my client doesn't?

Try to do some concurrent messaging between the server and the client. When they first connect to eachother and the Server sends the test string, the client gets it perfectly fine the first time. And the client can SEND messages just fine to the Server. But my Client class cant constantly check for messages like my Server can and idk what's wrong. Any suggestions?
Server class code:
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
String testMessage = "You are now connected and can begin chatting!";
boolean connected = false;
int port;
public Server(int port) {
this.port = port;
}
public void Open() {
//creates Threadpool for multiple instances of chatting
final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);
Runnable serverTask = new Runnable() {
#Override
public void run() {
try {
System.out.println("Opening...");
ServerSocket srvr = new ServerSocket(port);
while (true) {
Socket skt = srvr.accept();
clientProcessingPool.submit(new ClientTask(skt));
}
} catch (Exception e) {
try {
System.out.println(e);
System.out.print("You're opening too many servers in the same location, fool!\n");
ServerSocket srvr = new ServerSocket(port);
while (true) {
Socket skt = srvr.accept();
clientProcessingPool.submit(new ClientTask(skt));
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
Thread serverThread = new Thread(serverTask);
serverThread.start();
}
private class ClientTask implements Runnable {
private final Socket skt;
private ClientTask(Socket skt) {
this.skt = skt;
}
#Override
public void run() {
//for sending messages
if (connected == false) {
System.out.println("======================");
System.out.println("Server has connected!");
processMessage(testMessage);
}
//for receiving messages
while (true) {
try {
// Read one line and output it
BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
String incomingMessage = br.readLine();
if (incomingMessage != null) {
System.out.println("Server: Received message: " + incomingMessage);
processMessage(incomingMessage);
}
//br.close();
//skt.close(); //maybe delete
} catch (Exception e) {
System.out.println("Server had error receiving message.");
System.out.println("Error: " + e);
}
}
}
//for processing a message once it is received
public void processMessage(String message) {
PrintWriter out = null;
try {
out = new PrintWriter(skt.getOutputStream(), true);
} catch (IOException ex) {
System.out.println(ex);
System.out.println("Server had error sending message.");
}
System.out.print("Server: Sending message: " + message + "\n");
out.print(message);
out.flush();
connected = true;
try {
skt.shutdownOutput();
//out.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Client class code:
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
class Client {
public String message;
Socket skt;
public int port;
public Client(int port) {
this.port = port;
}
//for receiving messages from Server
public void receiveMessage() {
final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);
Runnable serverTask = new Runnable() {
#Override
public void run() {
try {
skt = new Socket(InetAddress.getLocalHost().getHostName(), port);
while (true) {
clientProcessingPool.submit(new Client.ClientTask(skt));
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
Thread serverThread = new Thread(serverTask);
serverThread.start();
}
//for sending messages to Server
public void sendMessage(String outgoingMessage) throws IOException {
try {
skt = new Socket(InetAddress.getLocalHost().getHostName(), port);
PrintWriter pw = new PrintWriter(skt.getOutputStream());
System.out.println("Client: Sending message: " + outgoingMessage);
pw.print(outgoingMessage);
pw.flush();
skt.shutdownOutput();
//skt.close(); //maybe delete
} catch (Exception e) {
System.out.println(e);
System.out.print("Client had error sending message.\n");
JOptionPane.showMessageDialog(null, "That User is not currently online.", "ERROR!!", JOptionPane.INFORMATION_MESSAGE);
}
}
private class ClientTask implements Runnable {
private final Socket skt;
private ClientTask(Socket skt) {
this.skt = skt;
}
#Override
public void run() {
while (true) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
//while (!in.ready()) {}
String incomingMessage = in.readLine();
if (incomingMessage != null) {
System.out.println("Client: Received message: " + incomingMessage); // Read one line and output it
message = incomingMessage;
}
//skt.shutdownInput();
//in.close();
//skt.close(); //maybe delete
} catch (Exception e) {
System.out.print("Client had error receiving message.\n");
}
}
}
}
}
Streams cannot be re-wrapped. Once assigned to a wrapper, they must use that wrapper for the entire life-cycle of the stream. You also shouldn't close a stream until you are done using it, which in this case isn't until your client and server are done communicating.
In your current code, there are a few times where you re-initialize streams:
while (true) {
try {
//Each loop, this reader will attempt to re-wrap the input stream
BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
String incomingMessage = br.readLine();
if (incomingMessage != null) {
System.out.println("Server: Received message: " + incomingMessage);
processMessage(incomingMessage);
}
//don't close your stream and socket so early!
br.close();
skt.close();
} catch (Exception e) {
//...
}
You get the idea; you can use this knowledge to find the stream problems in your client code as well.
With that said, servers are the middle-man between multiple clients. If you want to be able to type in the server's console to send a message to clients, it shouldn't go to only 1 client (unless you had a system that allowed you to specify a name). You need to store every connection in some kind of collection so when you type in the server's console, it goes to every client that's connected. This also helps when a client wants to send a message to every other client (global message). The server's main thread is primarily for accepting clients; I created another thread to allow you to type in the console.
As for your streams, you should create them whenever you start the ClientTask, both server side and client side:
public class Server {
private ExecutorService executor = Executors.newFixedThreadPool(10);
private Set<User> users = new HashSet<>();
private boolean running;
private int port;
public Server(int port) {
this.port = port;
}
public void start() {
running = true;
Runnable acceptor = () -> {
try(ServerSocket ss = new ServerSocket(port)) {
while(running) {
User client = new User(ss.accept());
users.add(client);
executor.execute(client);
}
} catch(IOException e) {
//if a server is already running on this port;
//if the port is not open;
e.printStackTrace();
}
};
Runnable userInputReader = () -> {
try(Scanner scanner = new Scanner(System.in)) {
while(running) {
String input = scanner.nextLine();
for(User user : users) {
user.send(input);
}
}
} catch(IOException e) {
//problem sending data;
e.printStackTrace();
}
};
Thread acceptorThread = new Thread(acceptor);
Thread userThread = new Thread(userInputReader);
acceptorThread.start();
userThread.start();
}
public void stop() {
running = false;
}
public static void main(String[] args) {
new Server(15180).start();
System.out.println("Server started!");
}
}
In the run() method is where the streams should be wrapped.
class User implements Runnable {
private Socket socket;
private boolean connected;
private DataOutputStream out; //so we can access from the #send(String) method
public User(Socket socket) {
this.socket = socket;
}
public void run() {
connected = true;
try(DataInputStream in = new DataInputStream(socket.getInputStream())) {
out = new DataOutputStream(socket.getOutputStream());
while(connected) {
String data = in.readUTF();
System.out.println("From client: "+data);
//send to all clients
}
} catch(IOException e) {
//if there's a problem initializing streams;
//if socket closes while attempting to read from it;
e.printStackTrace();
}
}
public void send(String message) throws IOException {
if(connected) {
out.writeUTF(message);
out.flush();
}
}
}
It's pretty much the same idea with the client:
1. Connect to Server
2. Create "communication" thread
3. Create "user input" thread (to receive input from console)
4. Start threads
public class Client {
private final String host;
private final int port;
private boolean connected;
private Socket socket;
public Client(String host, int port) {
this.host = host;
this.port = port;
}
public void start() throws IOException {
connected = true;
socket = new Socket(host, port);
Runnable serverInputReader = () -> {
try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
while (connected) {
String data = in.readUTF();
System.out.println(data);
}
} catch (IOException e) {
// problem connecting to server; problem wrapping stream; problem receiving data from server;
e.printStackTrace();
}
};
Runnable userInputReader = () -> {
try (DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Scanner scanner = new Scanner(System.in)) {
while (connected) {
String input = scanner.nextLine();
out.writeUTF(input);
}
} catch (IOException e) {
//problem wrapping stream; problem sending data;
e.printStackTrace();
}
};
Thread communicateThread = new Thread(serverInputReader);
Thread userThread = new Thread(userInputReader);
communicateThread.start();
userThread.start();
}
public static void main(String[] args) throws IOException {
new Client("localhost", 15180).start();
}
}
There are a few things I used in the code above that you may not be familiar with. They help simplify the syntax for your code:
Lambda Expressions - Prevents the need to create an anonymous class (or subclass) to declare a method
Try-With-Resources - Closes the resources specified automatically once the try block as ended
EDIT
When a user connects, you should store their connection by name or id. That way, you can send data to specific users. Even if your client is running on the same machine as the server, it's still the same idea: client connects to server, server sends message to client based on name or id:
while(running) {
User client = new User(ss.accept());
users.add(client); //add to set
executor.execute(client);
}
Right now, you are simply adding users to a Set. There is currently no way to grab a specific value from this set. What you need to do is give it some kind of "key". To give you an idea, here's an old algorithm I used to use. I have an array full of empty slots. When someone connects, I look for the first empty slot. Once an empty slot is found, I pass the user the index of the array it's being stored at (that will be the user's id), then store the user in the array at the specified index. When you need to send a message to someone, you can use the id to access that specific array index, grab the user you want and send a message:
class Server {
private int maxConnections = 10;
private ExecutorService executor = Executors.newFixedThreadPool(maxConnections);
private User[] users = new User[maxConnections];
//...
while(running) {
Socket socket = ss.accept();
for(int i = 0; i < users.length; i++) {
if(users[i] == null) {
users[i] = new User(socket, i);
executor.execute(users[i]);
break;
}
}
}
//...
public static void sendGlobalMessage(String message) throws IOException {
for(User user : users)
if(user != null)
user.send(message);
}
public static void sendPrivateMessage(String message, int id) {
User user = users[id];
if(user != null) {
user.send(message);
}
}
}
class User {
private Socket socket;
private int id;
private DataOutputStream out;
public User(Socket socket, int id) {
this.socket = socket;
this.id = id;
}
public void send(String message) throws IOException {
out.writeUTF(message);
out.flush();
}
public void run() {
DataInputStream in;
//wrap in and out streams
while(connected) {
String data = in.readUTF();
//Server.sendGlobalMessage(data);
//Server.sendPrivateMessage(data, ...);
sendMessage(data); //sends message back to client
}
}
}

Categories