加上这句out.close();客户端:
import java.io.*;
import java.net.*;class Client {
    public static void main (String args[]) {
        try {
            Socket s = new Socket("localhost", 5050);
            BufferedOutputStream out = new BufferedOutputStream(s.getOutputStream());
            BufferedInputStream in = new BufferedInputStream(s.getInputStream());            //向服务器端发送信息
            String msg = "send msg";
            byte b[] = msg.getBytes();
            out.write(b, 0, b.length);
            out.close();      ---------------------------这里    /*********
            //获取服务器端返回信息
            int word = -1, count = 0;
            byte d[] = new byte[1024];
            while ((word = in.read()) != -1) {
                d[count++] = (byte)word;
            }
            System.out.println("return: " + new String(d, 0, count));
    *********/            out.close();
            in.close();
            s.close();        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
}你这个程序还有错误
自己稍微调调就行了
涨涨功力

解决方案 »

  1.   

    就算是将注释去掉也正常编译,运行也没有问题,只是程序这样写有隐含的错误,比如Server.java中在执行语句out.write("return msg".getBytes(), 0, "return msg".length());时发生异常如何处理,所以最好使用try{...}catch(Exception e){...}finally{...}结构
      

  2.   

    hlding(淋东),我已经用try{}catch(){}结构包起来了呀,
    再在,去掉注释能正常编译,可是运行有问题的。
    客户端得不到服务器端的返回值,你试一试就知道了。
      

  3.   

    package test.server;import java.io.*;
    import java.net.*;public class Server {  ServerSocket ss = null;  public Server() {
        try {
          ss = new ServerSocket(5050);
        }
        catch (Exception e) {
        }
      }  public Object readInput() {
        try {
          Socket c = ss.accept();
          BufferedInputStream in = new BufferedInputStream(c.getInputStream());
          int word = -1, count = 0;
          byte b[] = new byte[1024];
          while ( (word = in.read()) != -1) {
            b[count++] = (byte) word;
          }
          String msg = new String(b, 0, count);
          in.close();
          c.close();
          return msg;
        }
        catch (Exception e) {
          return null;
        }
      }  public void writeOutput(Object obj) {
        try {
          Socket c = ss.accept();
          BufferedOutputStream out = new BufferedOutputStream(c.getOutputStream());
          String retmsg = (String)obj;
          byte r[] = retmsg.getBytes();
          out.write(r, 0, r.length);
          System.out.println("return stream close");
          out.flush();
          out.close();
          c.close();
        }
        catch (Exception e) {
        }
      }  public void close() {
        try {
          ss.close();
        }
        catch (Exception e) {
        }
      }  public static void main(String args[]) {
        Server server = new Server();
        //接收客户端发送的信息
        System.out.println("msg: " + server.readInput());
        //向客户端发送信息
        server.writeOutput("return ms");
        server.close();
      }
    }
    package test.client;import java.io.*;
    import java.net.*;public class Client {  public Client() {}  public void writeOutput(Object obj) {
        try {
          Socket s = new Socket("localhost", 5050);
          BufferedOutputStream out = new BufferedOutputStream(s.getOutputStream());
          String msg = (String) obj;
          byte b[] = msg.getBytes();
          out.write(b, 0, b.length);
          out.close();
          s.close();
        }
        catch (Exception e) {
          System.out.println(e);
          e.printStackTrace();
        }
      }  public Object readInput() {
        try {
          Socket s = new Socket("localhost", 5050);
          BufferedInputStream in = new BufferedInputStream(s.getInputStream());
          int word = -1, count = 0;
          byte d[] = new byte[1024];
          while ( (word = in.read()) != -1) {
            d[count++] = (byte) word;
          }
          String val = new String(d, 0, count);
          in.close();
          s.close();
          return val;
        }
        catch (Exception e) {
          System.out.println(e);
          e.printStackTrace();
          return null;
        }
      }  public static void main(String args[]) {
        Client client = new Client();
        client.writeOutput("send msg");
        System.out.println("return: " + client.readInput());
      }
    }
      

  4.   

    我对socket也是初步懂得。
    我试验过,若两端同时都发送和接收,像flybird116 (玲珑飞鸟)那样写总会有问题,而,只一方发送另一方接收,就如预想。
    根据经验,我简单地修改程序如上,而后工作正常。至于socket为什么这样,其原理我也不知道。
    程序修改后还是很粗糙的,仅限于实验。有谁知道这个原理?
      

  5.   

    你传送数据
    得告诉对方什么时候停止啊就这么简单的原理注释不去掉 当然可以
    客户端传送完了数据 关了socket
    注释去掉 客户端传了数据  服务器端接受到了 
    但它不知道结束没有 开始阻塞 而客户端还想从socket里取数据两边都取 还不死啊
      

  6.   

    illmoon(璨月) 多谢,现在程序可以正常运行了。
    可是我还是不明白,你只是把程序的读和写操作分别放在不同的方法里,然后在main方法里调用,而我原来是全在main方法里实现,为什么我的不行,你的就行?
    有点不明白呀。:)
      

  7.   

    jokerjava(冷血):illmoon(璨月)修改后的程序也没有告诉服务器什么时候结束呀
    怎么现在可以运行了?能说说其中上道理吗?