愤怒的八戒 2016-07-09 08:41 采纳率: 0%
浏览 976

写了Socket通讯实现群聊,可是发给服务器后,别的客户端缺收不到服务器转发的信息

服务端:
public static void main(String args[]) throws IOException {
QueueSocket queueSocket = new QueueSocket();
queueSocket.ini();

}

public void ini() {
    ServerSocket ss = null;
    try {
        ss = new ServerSocket(8082);
        System.out.println("server is starting");
        while (true) {
            Socket socket = ss.accept();
            System.out.println("New connection accepted "
                    + socket.getInetAddress() + ":" + socket.getPort());
            new HandlerThread(socket);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (ss != null) {
                ss.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

class HandlerThread implements Runnable {
    private Socket socket;
    String buffer;

    public HandlerThread(Socket client) {
        this.socket = client;
        new Thread(this).start();
    }

    @Override
    public void run() {

        try {
            //读取服务器端数据    
            DataInputStream input = new DataInputStream(socket.getInputStream());    
            //向服务器端发送数据    
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());    
            String ret = input.readUTF();     
            out.writeUTF(ret);    
            out.flush();
            System.out.println("客户端返回过来的是: " + ret);

            input.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

客户端:
public static void main(String[] args) {

    String buffer = "";
    Socket socket = null;
    try {
        socket = new Socket("192.168.1.107", 8082);
        if (socket == null) {
            System.out.println("error");
        }
           //读取服务器端数据    

        DataInputStream input = new DataInputStream(socket.getInputStream());    
        //向服务器端发送数据    
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());    
        out.writeUTF("A10104");    
        out.flush();
        String ret = input.readUTF();     
        System.out.println("服务器端返回过来的是: " + ret);    
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

另一个客户端:
  @Override
public void run() {

    try {
        socket = new Socket("192.168.1.107", 8082);
        System.out.println("socket客户端" + Util.getCall(context) + "运行");
        if (socket == null) {
            System.out.println("error");
        }
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeUTF("A10104");
        out.flush();
        while(true){
            DataInputStream input = new DataInputStream(socket.getInputStream());
            String ret = input.readUTF();
            if (!ret.equals("")&&ret!=null) {
                System.out.println("服务器端返回过来的是: " + ret);
            }
        }

        //读取服务器端数据    
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
求大神帮我看看,项目着急卡在这里了
  • 写回答

1条回答 默认 最新

  • threenewbee 2016-07-09 14:49
    关注

    客户端先要主动连服务器端,服务器用数组将建立的连接的socket保存起来,转发的时候找到连接并且发送。

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?