MultiClient server - Java

廉价感情. 提交于 2019-12-11 05:28:51

问题


I have a program ready that acts as a client and another that acts as a server.

I want to be able to send a message to the server, and the server will then forward the message to another client that is also connected to the server.

So the the server is supposed to forward the message to the other client.

How would i be able to do that, and what do I need to read up on?

This is what i got now.

Server.java

package server;

import java.net.*;
import java.io.*;

import javax.swing.*;

public class Server{

public static void run() {
    ServerSocket serverSocket = null;
    Socket socket = null;
    try{
        while(true){
            serverSocket = new ServerSocket(5555);
            socket = serverSocket.accept();
            InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(streamReader);
            String message = bufferedReader.readLine();
            System.out.println(message);
            if(message != null){
                PrintStream printStream = new PrintStream(socket.getOutputStream());
                printStream.println("Message receivd!");
            }
            streamReader.close();
            socket.close();
            serverSocket.close();
            bufferedReader.close();
        }
    }catch(Exception e){}
 //     try{
 //         
 //     }catch(Exception e){}
     }
    public static void main(String[]args){
    Server s = new Server();
    s.run();
}

}

And then I also got TCPClient.java

package client;

 import java.net.*;
 import java.io.*;

 public class TCPClient {
private String serverIP = "localhost";
private int serverPort = 1111;
private int count = 0;
private Thread thread;

public TCPClient() {
    this.thread = new Thread(new ConnectAndListenToServer());
  //        thread.start();
}
public void sendMessage(int count){
    this.count = count;
    this.thread = new Thread(new ConnectAndListenToServer());
    thread.start();
}
private class ConnectAndListenToServer implements Runnable {
    Socket socket = null;
    public void run() {
        BufferedReader bufferedReader = null;
        InputStreamReader streamReader = null;
        try {
            socket = new Socket(serverIP,serverPort);
            PrintStream printStream = new PrintStream(socket.getOutputStream());
            printStream.println(count);
            streamReader = new InputStreamReader(socket.getInputStream());
            bufferedReader = new BufferedReader(streamReader);
            String message = bufferedReader.readLine();
            if(message != null){
                System.out.println(message);
            }

        }catch(Exception e){}
        try{
            socket.close();
            bufferedReader.close();
            streamReader.close();
        }catch(Exception ee){}
    }
}

}

How would i be able to forward the messeage already received on the server to another client?


回答1:


How would i be able to forward the messeage already received on the server to another client?

I have already posted some samples in the same context.

Till now you have done well, to complete it please have a look Client-Server Communication. I have described it step by step just follow the thread to find other samples as well.

Please let me know if still you have any issue!




回答2:


This just works if you have Client 2 connected while Client 1 is also connected.

This is possible if you write a multithreading application or use a Selector.



来源:https://stackoverflow.com/questions/23000708/multiclient-server-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!