标题:怎么实现从控制台输入字符串然后传给服务器端呢?
只看楼主
xiao84699024
Rank: 2
来 自:江西南昌
等 级:论坛游民
帖 子:25
专家分:39
注 册:2013-3-31
结帖率:100%
已结贴  问题点数:70 回复次数:1 
怎么实现从控制台输入字符串然后传给服务器端呢?
//服务器端
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
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2391
专家分:13384
注 册:2013-3-3
得分:70 
在服务器端程序中,首先用包中的ServerSocket类创建一个服务器端侦听套接字。其后应用ServerSocket类的accept方法等待并接收用户请求。当服务器每接收到一个连接请求后,就启动一个线程来单独处理服务器和客户端的数据传输。服务器端数据的接收和发送与上面介绍的客户端数据的发送和介绍相同。
http://    你参考下

Maybe
2013-04-30 21:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-405875-1-1.html




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

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