当前位置: 代码迷 >> Java相关 >> 怎么实现从控制台输入字符串然后传给服务器端呢?
  详细解决方案

怎么实现从控制台输入字符串然后传给服务器端呢?

热度:320   发布时间:2013-04-29 03:53:56.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 java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

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  

----------------解决方案--------------------------------------------------------
在服务器端程序中,首先用java.net包中的ServerSocket类创建一个服务器端侦听套接字。其后应用ServerSocket类的accept方法等待并接收用户请求。当服务器每接收到一个连接请求后,就启动一个线程来单独处理服务器和客户端的数据传输。服务器端数据的接收和发送与上面介绍的客户端数据的发送和介绍相同。
http://www.cnblogs.com/chun-xi/archive/2013/04/30/3052418    你参考下
----------------解决方案--------------------------------------------------------
  相关解决方案