sara596286 2011-03-09 21:47
浏览 621
已采纳

请问用socket实现广播,客户端怎么监听啊

我看网上的代码都是用GUI或者多线程做的,如果不这样可以做吗?这个是我客户端的代码,一直收不到消息,不知道问题在哪啊?可以有人帮我改一下吗?谢谢了!
[code="java"]public class SocketClient{
static Socket server;
public static void main(String[] args)throws Exception{
server=new Socket(InetAddress.getLocalHost(),5678);
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(server.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));

while(true)
{
while(in.readLine()==null){
String str=wt.readLine();
out.println(str);
out.flush();

                           System.out.println(in.readLine());
                         }
                         String str=in.readLine();
                         System.out.println(in.readLine()); 
                         server.close();
                      }                  
             }
       }[/code]
  • 写回答

7条回答 默认 最新

  • changchanghust 2011-03-10 23:19
    关注

    服务器端其实也用到了多线程了的,这里其实就是为每一个客户端开一个单独的线程来处理。
    [code="java"] SocketThread mulserver = new SocketThread(ss, list);
    System.out.println("The number of clients is: " + count);
    mulserver.start();[/code]

    如果不用多线程的话,readLine操作就会被阻塞,在客户端的体验就是:等待服务器发来一行消息,然后才能继续读取键盘输入的文字;或者是等待键盘敲回车,才能继续读取服务器过来的下一行消息。

    你后来贴的代码貌似没看到receive方法的调用哦。在你初始化完了GUI和网络以后,可以在主线程里调用receive()方法。因为swing里会启动后台线程来监听你对界面到动作,响应界面事件,主线程的阻塞不会影响到界面的响应。

    我贴一下我根据你的思路写的一个简单的控制台客户端,仅用来说明我前面讲的思路,希望能对你有帮助~

    [code="java"]public class SocketClient {
    private Socket server;
    private BufferedReader wt = null;

    public void startClient() {
        try {
            server = new Socket(InetAddress.getLocalHost(), 5678);
            wt = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter out = new PrintWriter(server.getOutputStream());
            new ClientReadThread(server.getInputStream()).start();
            String line = null;
            while ((line = wt.readLine()) != null) {
                out.println(line);
                out.flush();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    
    public static void main(String[] args) throws Exception {
        new SocketClient().startClient();
    
    }
    

    }[/code]

    [code="java"]public class ClientReadThread extends Thread {

    private BufferedReader reader = null;
    
    public ClientReadThread(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
    }
    
    public void run() {
        String line = null;
        try {
            while((line = reader.readLine()) != null) {
                System.out.println("recv:" + line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    }[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置