| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 774 人关注过本帖
标题:怎么实现从控制台输入字符串然后传给服务器端呢?
取消只看楼主 加入收藏
xiao84699024
Rank: 2
来 自:江西南昌
等 级:论坛游民
帖 子:25
专家分:39
注 册:2013-3-31
结帖率:100%
收藏
已结贴  问题点数:70 回复次数:0 
怎么实现从控制台输入字符串然后传给服务器端呢?
//服务器端
public class Server {
    public void strat() throws Exception{
        ServerSocket ss = new ServerSocket(8899);
        while(true){
            Socket socket = ss.accept();
            System.out.println(socket);
            Hunter thread = new Hunter(socket);
            thread.start();
        }
        
    }
    public class Hunter extends Thread{
        private Socket socket;
        public Hunter(Socket socket){
            this.socket = socket;
        }
        @Override
        public void run(){
            try{
                InputStream is = socket.getInputStream();
                OutputStream os = socket.getOutputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = br.readLine();
                System.out.println(line);
                OutputStreamWriter osw = new OutputStreamWriter(os);
                PrintWriter pw = new PrintWriter(osw);
                pw.println("小诺");
                pw.flush();
                socket.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws Exception{
        System.out.println("我是服务器端");
        Server1 server = new Server1();
        server.strat();
    }
}




客户端
import
import
import
import
import
import
import

public class Client {
    public void request() throws Exception{
        Socket socket = new Socket("localhost",8899);
        System.out.println(socket);
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        PrintWriter pw = new PrintWriter(osw);
       //就是想从这里打印自己输入的文字传到服务器端
       pw.println("我可以假装看不见,也可以偷偷的想念");
        pw.flush();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = br.readLine();
        System.out.println(line);
        socket.close();
        
    }
    public static void main(String[] args) throws Exception{
        System.out.println("你好,我是客户端");
        Client client = new Client();
        client.request();
    }
}

搜索更多相关主题的帖子: public Socket private 服务器 socket 
2013-04-29 03:53
快速回复:怎么实现从控制台输入字符串然后传给服务器端呢?
数据加载中...
 
   



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

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