| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1656 人关注过本帖
标题:运行后就是显示不出来是第几个客户端
只看楼主 加入收藏
brokenheart
Rank: 2
等 级:论坛游民
帖 子:64
专家分:25
注 册:2015-11-7
结帖率:76.92%
收藏
已结贴  问题点数:20 回复次数:1 
运行后就是显示不出来是第几个客户端
实现一个网络应用:
  客户端会给服务器发送一个字符串服务器会把这个字符串转换成大写形式发回给客户端并由客户端显示,同时,服务器会告知客户端,他是第几个客户端。
程序代码:
import *;
import *;

public class Server {
    static int i=1;
    public Server() {
        try {
            ServerSocket ss = new ServerSocket(11038);
            Socket s = ss.accept();
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            dos.writeUTF("hihi");
            System.out.println("原字母为:"+dis.readUTF());
            dos.flush();
        } catch (Exception e) {
            System.out.println("Exception:" + e.getMessage());
        }
    }
    public static void main(String[] args) {
        
         new Server();
         while(true){
            System.out.println("你是第"+i+"个客户端");
            i++;
        }
    }
}

程序代码:
import *;
import *;

public class client {
    public client() {
        try {
            Socket s = new Socket("localhost", 11038);
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            dos.writeUTF("hello");
            String info = dis.readUTF().toUpperCase();
            System.out.println("转化为大写字母后为:"+info);
            dos.flush();
            dos.close();
            dis.close();
            s.close();
        } catch (Exception e) {
            System.out.println("Exception:" + e.getMessage());
        }
    }
    public static void main(String args[]) {
        new client();
    }
}

运行后,程序就跑不停,各位大神帮我看看该如何
搜索更多相关主题的帖子: 网络应用 服务器 字符串 客户端 
2016-06-19 00:24
jinjoxie
Rank: 2
等 级:论坛游民
威 望:1
帖 子:19
专家分:61
注 册:2015-6-3
收藏
得分:20 
import *;
import *;

public class Server {
    static int i=1;
    public Server() {
        try {
            ServerSocket ss = new ServerSocket(11038);

            while(true){
                Socket s = ss.accept();
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                DataInputStream dis = new DataInputStream(s.getInputStream());
                dos.writeUTF("hihi");
                System.out.println("原字母为:"+dis.readUTF());
                dos.flush();
                System.out.println("你是第"+i+"个客户端");
                i++;
            }
        } catch (Exception e) {
            System.out.println("Exception:" + e.getMessage());
        }
    }
    public static void main(String[] args) {        
         new Server();
    }  
}

ServerSocket每次调用accept后就一直等待下个客户端接入,调用一次只接入一个的,你想监听多个客户端就要多次掉用accept
2016-06-21 09:25
快速回复:运行后就是显示不出来是第几个客户端
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019865 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved