日期:2014-05-20  浏览次数:20829 次

菜鸟求教Socket问题,求Debug...
客户端code如下:

Java code

package Socket_Test1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ClientTest {
    public static void main(String[] args) {
        ClientTest ct = new ClientTest();
    }
    public ClientTest()
    {
        Socket s = null;
        BufferedWriter bw = null;
        OutputStreamWriter osw = null;
        BufferedReader br= null;
        InputStreamReader isr = null;
        try
        {
            s = new Socket("127.0.0.1",8092);
            //req server
            osw =new OutputStreamWriter(s.getOutputStream());
            bw = new BufferedWriter(osw);
            bw.write("你是谁?");
            bw.flush();
                    
            //res server
            isr = new InputStreamReader(s.getInputStream());
            br= new BufferedReader(isr);
            
            String res =br.readLine();
            System.out.println("服务端发来:"+ res);        
            
        }catch(Exception ex)
        {
        ex.printStackTrace();    
        }
        finally
        {
            try {        
                br.close();
                isr.close();
                bw.close();
                osw.flush();
                osw.close();
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}




服务端代码如下:


Java code

package Socket_Test1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerTest{
    public static void main(String[] args) {
        ServerTest st = new ServerTest();

    }
    
    public  ServerTest()
    {
        ServerSocket ss = null;
        Socket s = null;
        BufferedReader br= null;
        InputStreamReader isr = null;
        BufferedWriter bw = null;
        OutputStreamWriter osw = null;
        try{
            ss = new ServerSocket(8092);
            System.out.println("服务端启动……");
            s = ss.accept();
            //res client
             isr = new InputStreamReader(s.getInputStream());
            br= new BufferedReader(isr);        
            String res =br.readLine();
            System.out.println("客户端发来:"+ res);
            
            //req client
            osw =new OutputStreamWriter(s.getOutputStream());
            bw = new BufferedWriter(osw);
            bw.write("我是服务器!");
            bw.flush();
        }catch(Exception ex)
        {
          ex.printStackTrace();    
        }
        finally
        {
            try {
                bw.close();
                osw.close();
                br.close();
                isr.close();
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    
}





问题是:从客户端往服务端写能成功,服务端也能显示
But,服务端再往客户端写,客户端就显示不出了

------解决方案--------------------
用readLine()来读取的话,要保证字符串以 \n 结束,这样有明确的换行标记才好。

比如:
bw.write("我是服务器!\n");