发送到多线程客户端服务器中的所有客户端

时间:2018-10-25 00:33:01

标签: java networking server client client-server

我目前在我的代码方面遇到问题。客户端应能够发送字符串“ messge# somestring ”,并且如果服务器注意到它包含“ message#”子字符串,则应将其发送给所有用户。但是,发生的事情是它只发送给了一个发送了我试图实现的“伪请求”的用户。这是我的代码供参考:

import java.io.*;
import java.net.*;
import java.util.Vector;

public class server {
    static ServerSocket ss;
    static Socket s;
    static DataInputStream dis;
    static DataOutputStream dos;
    static Integer maxNumofUsers=4;
    static Vector<UserHandler> users;
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Integer currNumofUsers=0;
        ss=new ServerSocket(7777);
        users=new Vector<UserHandler>();

        while(currNumofUsers<maxNumofUsers) {
            s=ss.accept();
            UserHandler uh= new UserHandler(s, "Player "+Integer.toString(currNumofUsers+1));
            users.add(uh);
            users.get(users.size()-1).start();//start a thread
            currNumofUsers++;
            for(int i=0; i<users.size(); i++) {//updates number of users present pero userhandler
                users.get(i).updateUserSet(users);
            }
        }

        System.out.println("Maximum number of players reached.");
    }

}

class UserHandler extends Thread{
    Socket s;
    DataInputStream dis;
    DataOutputStream dos;
    Vector<UserHandler> users=new Vector<UserHandler>();
    String username;
    public UserHandler(Socket s, String username) {
        // TODO Auto-generated constructor stub
        this.s=s;
        this.username=username;
    }

    public void run(){
        try {
            dis=new DataInputStream(s.getInputStream());
            dos=new DataOutputStream(s.getOutputStream());

            while(true) {
                String message=dis.readUTF();
                if(message.contentEquals("exit")) {
                    dos.writeUTF("exit granted");
                    close();
                    break;
                }else if(message.contains("message#")){//group sms
                    for(int i=0; i<users.size(); i++) {
                            users.get(i).dos.writeUTF("henlo");
                            System.out.println("Wrote to "+users.get(i).username);
                    }

                }else {
                        dos.writeUTF("Some String");
                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            try {
                s.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            e.printStackTrace();
        }

    }

    public void close() {
        try {
            for(int i=0; i<users.size(); i++) {
                if(users.get(i).username==this.username) {
                    users.remove(i);
                    System.out.println("out");
                    for(int j=0; j<users.size(); j++) {
                        users.get(j).updateUserSet(users);
                    }
                    break;
                }
            }
            dis.close();
            dos.close();
            s.close();
        }catch(IOException e) {
            e.printStackTrace();
        }

    }

    public void updateUserSet(Vector<UserHandler> users) {
        // TODO Auto-generated method stub
        this.users=users;
    }


}

对于服务器。为客户

import java.net.*;
import java.util.Scanner;
import java.io.*;

public class client {
    static Socket s;
    static DataInputStream dis;
    static DataOutputStream dos;
    static gameWindow GUI;
    static String username;
    public static void main(String[] args) throws Exception {

        try {
            s=new Socket();
            s.connect(new InetSocketAddress("127.0.0.1",7777), 5000);
            System.out.println("Socket "+s.getInetAddress()+" has connected.");
            username= JOptionPane.showInputDialog("Please input username: ");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Cannot connect to server. Either the Server was not established or maximum number of players was reached.");
        }

        if(s.isConnected()){
            try {
                dis=new DataInputStream(s.getInputStream());
                dos=new DataOutputStream(s.getOutputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Scanner sc=new Scanner(System.in);
            String msg;
            while(true) {

                msg=sc.nextLine();
                dos.writeUTF(msg);
                String rcv=dis.readUTF();
                if (rcv.contentEquals("exit granted")) {
                    System.out.println("Socket "+s.getInetAddress()+" is closed.");
                    close();
                    break;
                }else if(rcv.contains("array#")) {
                    String tobearr=rcv;
                    String[] arred=tobearr.split("#");
                    for(int i=0; i<arred.length; i++) {
                        System.out.println(arred[i]);
                    }
                    tobearr="";
                }else if(rcv.contains("message#")) {
                    String grpsms=rcv.substring(8, rcv.length());
                    System.out.println(grpsms);
                }
                System.out.println(rcv);
                rcv="";
            }
        }

    }

    private static void close() {
        // TODO Auto-generated method stub
        try {
            dis.close();
            dos.close();
            s.close();
        }catch(IOException e) {
            e.printStackTrace();
        }

    }

}

0 个答案:

没有答案