服务器端:
import java.io.*;
import java.net.*;
public class MTServer{
public static void main(String[] args)
{
byte[] b=new byte[6] ;
int eof=0;
try
{
ServerSocket ssocket=new ServerSocket(12345);
Socket csocket=ssocket.accept();
while(eof!=-1)
{
eof=new BufferedInputStream(csocket.getInputStream()).read(b);
System.out.println(eof);
if(eof!=-1)System.out.println(new String(b).trim());
}
csocket.close();
}
catch(IOException e){e.printStackTrace();}

}
}客户端:
public class MTClient 
{
public static void main(String[] args) throws Exception
{  byte[] a=("中华人民共和国").getBytes();
Socket csocket=new Socket("127.0.0.1",12345);
OutputStream w=new BufferedOutputStream(csocket.getOutputStream());
w.write(a);
w.close();
}
}为什么服务器端只输出“中华人”

解决方案 »

  1.   

    是因为你b数组定义的长度太小了的原因。客户端发送了中华人民共和国,但是(csocket.getInputStream()).read(b)只能接收前6个字符。所以输入就是中华人了
      

  2.   

    byte[] b=new byte[6] ;
    而且一个中文字符占两个字节,
    在下面循环读入的时候,你每次都是新建一个流,
    BufferedInputStream bis=new BufferedInputStream(csocket.getInputStream());
    在循环里面用
    eof = bis.read();