多线程服务器只从一个客户端JAVA接收数据 [英] Multithreaded Server receives data from just one client JAVA

查看:167
本文介绍了多线程服务器只从一个客户端JAVA接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个简单的多线程服务器。每次它接受客户端的一个DataInputStream和DataOutputStream创建和交际启动

I ve created a simple MultiThreaded server. Everytime it accepts client a DataInputStream and DataOutputStream is created and the comunication start

服务器:

public class Connection implements Runnable{
boolean isAlreadyOpened = false;

@Override
public void run() {
    // TODO Auto-generated method stub  
    try {           
        ServerSocket ss = new ServerSocket(7000);
        while(true){
            System.out.println("Il Server sta cercando Connessioni");
            Socket s = ss.accept();
            System.out.println("Il Server ha accettato un Client");

            Thread t2 = new Thread(new Runnable(){
                public void run(){             
                   try {

                        DataInputStream dis = new DataInputStream(s.getInputStream());
                        isAlreadyOpened = true;
                        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                        while(true){
                            String test = dis.readUTF();
                            dos.writeUTF(test);
                            System.out.println(test);
                            dos.flush();
                        }                       
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        isAlreadyOpened = false;
                    }  
                }           
            });
            t2.start();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }  
}

我已经尝试连接一个Android应用程序和JavaApp;每40秒这两个计划的通过writeUTF发送一个字符串。连接从两个客户端正确建立但服务器刚刚从连接到服务器的最后一个接收数​​据。
我怎样才能让服务器接收/从/发送数据到所有客户端?

I ve tried to connect an Android application and a JavaApp; every 40 seconds both of these programs send a string via writeUTF. The connection is established correctly from both Clients but the server receives Data just from the last one that connects to server. How can I allow the server to receive/send the data from/to ALL the clients?

编辑:

我已经尝试设置这样的:

I ve tried to set this:

public class Connection implements Runnable {

 @Override
  public void run() {

    try {
        ServerSocket ss = new ServerSocket(7000);
        while (true) {
            System.out.println("Server is listening");
            Socket s = ss.accept();
            System.out.println("Client Accepted");

            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        DataInputStream dis = new DataInputStream(s.getInputStream());
                        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                        while (true) {
                            String test = dis.readUTF();
                            dos.writeUTF(test);
                            System.out.println(test);
                            dos.flush();
                        }
                    } catch (IOException 
                        e.printStackTrace();
                    } finally {
                        try {
                            s.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });                 
    t2.start();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

但结果是相同的

编辑:

的Andorid客户端code

Andorid Client Code

public class Connection implements Runnable {
@Override
public void run() {
    try {
        Socket s = new Socket("127.0.0.1", 7000);
        DataInputStream dis = new DataInputStream(s.getInputStream());
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());

        while(true){
           dos.writeUTF("FromAndroid");
            Log.d("InputStreammmm", dis.readUTF());
         Thread.sleep(10000)
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

JAVA应用程序客户端code

JAVA APP CLIENT CODE

@Override
public void run() {
     try {

        Socket socket = new Socket("127.0.0.1", 7000);
        System.out.println("Connessooo");
         DataInputStream dis = new DataInputStream(socket.getInputStream());
         DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

         while(true){
         dos.writeUTF("Invioooooooooooooooooooooooooo");
         result = dis.readUTF();
         System.out.println(result);
         Thread.sleep(10000);
         }

    } catch (IOException e) {

        e.printStackTrace();
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
}

使用LIST插座编辑:

EDIT WITH LIST OF SOCKET:

public class Connection implements Runnable {
List<Socket> sList = new ArrayList<>();
Socket s;
int i = 0;

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        ServerSocket ss = new ServerSocket(7000);
        while (true) {
            System.out.println("Server Listening");
            s = ss.accept();
            sList.add(s);

            System.out.println("Accepted Client --- " +s.toString());
            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    try {
                        DataInputStream dis = new DataInputStream(s.getInputStream());  

                        while (true) 
                        {
                            String test = dis.readUTF();
                            System.out.println("Message sent from -- " + sList.get(i).toString());
                            System.out.println(test);

                            while(i < sList.size()){
                                 DataOutputStream dos = new DataOutputStream(sList.get(i).getOutputStream());
                                 dos.writeUTF(test);    
                                 System.out.println("Message Sent to -- " + sList.get(i).toString());
                                 dos.flush();
                                 ++i;
                             }
                            i=0;
                        }
                    } catch (IOException e) 
                    {
                        e.printStackTrace();
                    } finally
                    {
                        try
                        {                      
                            System.out.println("Closing Socket --- " + sList.get(i).toString());
                            sList.get(i).close();
                            sList.remove(i);

                        }
                        catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            });
            t2.start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

在这种方式由EJP ...

Doing in this way my problem is solved as suggested by EJP...

推荐答案

无法重现。

测试程序,从编辑code复制,粘贴,并添加最后插座S 我是使用Java 7,并添加客户端code:

Test program, copy-pasted from your edited code and added final to Socket s as I am using Java 7, and added client code:

public class Connection implements Runnable
{

    @Override
    public void run()
    {

        try
        {
            ServerSocket ss = new ServerSocket(7000);
            while (true)
            {
                System.out.println("Server is listening");
                final Socket s = ss.accept();
                System.out.println("Client Accepted");

                Thread t2 = new Thread(new Runnable()
                {
                    public void run()
                    {
                        try
                        {
                            DataInputStream dis = new DataInputStream(s.getInputStream());
                            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                            while (true)
                            {
                                String test = dis.readUTF();
                                dos.writeUTF(test);
                                System.out.println(test);
                                dos.flush();
                            }
                        }
                        catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                        finally
                        {
                            try
                            {
                                s.close();
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                t2.start();
            }
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws InterruptedException
    {
        Connection c = new Connection();
        Thread  t1 = new Thread(c);
        t1.setDaemon(true);
        t1.start();
        Runnable    r = new Runnable()
        {
            public void run()
            {
                try (Socket s = new Socket("localhost", 7000))
                {
                    DataOutputStream    dos = new DataOutputStream(s.getOutputStream());
                    DataInputStream dis = new DataInputStream(s.getInputStream());
                    for (int i = 0; i < 10; i++)
                    {
                        dos.writeUTF("Hello from "+Thread.currentThread().getName());
                        String  reply = dis.readUTF();
                        Thread.sleep(10*1000);
                    }
                }
                catch (IOException|InterruptedException exc)
                {
                    exc.printStackTrace();
                }
            }
        };
        Thread  t2 = new Thread(r);
        Thread  t3 = new Thread(r);
        t2.start();
        t3.start();
        t2.join();
        t3.join();
    }
}

输出:

Server is listening
Client Accepted
Server is listening
Client Accepted
Server is listening
Hello from Thread-1
Hello from Thread-2
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-2
Hello from Thread-1
Hello from Thread-1
Hello from Thread-2
Hello from Thread-2
Hello from Thread-1
Hello from Thread-2
Hello from Thread-1

这篇关于多线程服务器只从一个客户端JAVA接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆