服务器端:
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();
}
}为什么服务器端只输出“中华人”
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();
}
}为什么服务器端只输出“中华人”
而且一个中文字符占两个字节,
在下面循环读入的时候,你每次都是新建一个流,
BufferedInputStream bis=new BufferedInputStream(csocket.getInputStream());
在循环里面用
eof = bis.read();