很简单的 客户端 服务器程序,收发都有。就是不正常。 总是停在哪里。和Linux 没关系吧!Windows上没试public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(4321);
        Socket cs = ss.accept();
        BufferedReader rcs = new BufferedReader(new InputStreamReader(cs.getInputStream()));
        PrintWriter wcs = new PrintWriter(cs.getOutputStream());        String s = rcs.readLine();
        System.out.println(s);
        wcs.println(s);
        s = rcs.readLine();
        System.out.println(s);
        wcs.println(s);
        s = rcs.readLine();
        System.out.println(s);
        wcs.println(s);
        s = rcs.readLine();
        System.out.println(s);
        wcs.println(s);
        s = rcs.readLine();
        System.out.println(s);
        wcs.println(s);        rcs.close();
        wcs.close();
        cs.close();
        ss.close();
    }
}public class Client {
    public static void main(String[] args) throws IOException {
        Socket ss = new Socket("localhost", 4321);
        BufferedReader rss = new BufferedReader(new InputStreamReader(ss.getInputStream()));
        PrintWriter wss = new PrintWriter(ss.getOutputStream());        wss.println("Why?");
        String s;
        s = rss.readLine();
        System.out.println(s);
        wss.println(s);
        s = rss.readLine();
        System.out.println("1");
        wss.println(s);
        s = rss.readLine();
        System.out.println("1");
        wss.println(s);
        s = rss.readLine();
        System.out.println("1");
        wss.println(s);
        s = rss.readLine();
        System.out.println(s);        rss.close();
        wss.close();
        ss.close();    }
}给点建议~~

解决方案 »

  1.   

    很简单的 客户端 服务器程序,收发都有。就是不正常。 总是停在哪里。和Linux 没关系吧!Windows上没试怎么样个不正常法呢?你这样写的程序就只能是服务器说句话,然后客户端回一句,然后服务器再说一句,因为readline()方法是阻塞式的
      

  2.   

    PrintWriter wcs = new PrintWriter(cs.getOutputStream(),true);
    PrintWriter wss = new PrintWriter(ss.getOutputStream(),true);
      

  3.   


            System.out.println(s);
            wcs.println(s);
            s = rcs.readLine();
            System.out.println(s);
            wcs.println(s);
            s = rcs.readLine();
            System.out.println(s);
            wcs.println(s);
            s = rcs.readLine();
            System.out.println(s);
            wcs.println(s);
            s = rcs.readLine();
            System.out.println(s);
            wcs.println(s);建议循环。
      

  4.   

    用死循环,服务器一直等待连接就是了,while(true);