java socket 输入流问题,java的socket接收输入流问题,javasocket输入流,我自己机器上为什么每次接...

java的socket接收输入流问题,javasocket输入流,我自己机器上为什么每次接

我自己机器上为什么每次接受的输入流数据的长度只能是65536个字节,我声明的缓冲字节远大于65536,请问为什么。这是代码:客户端:public class Client { static Socket client = null; static final int PORT = 1234; static final String IP = "127.0.0.1"; static final String PATH = "savefile.xml";// static InputStream inputStream = null; static DataInputStream inputStream = null; static OutputStream outputStream = null; public static void main(String[] A) { init(); receiveData1(); } public static void init() { try { client = new Socket(IP, PORT); outputStream = new FileOutputStream(PATH); } catch (UnknownHostException e) { e.printStackTrace(); System.out.println("未知的服务器."); } catch (IOException e) { e.printStackTrace(); System.out.println("输入输出服务器出现异常."); } } public static void receiveData1() { try {// inputStream = client.getInputStream(); inputStream= new DataInputStream(new BufferedInputStream(client.getInputStream())); long startTime= System.currentTimeMillis(); String fileName= inputStream.readUTF(); long length= inputStream.readLong(); System.out.println("文件名以及长度:"+fileName+ " :"+ length); StringBuffer content= new StringBuffer(); byte[] temp= new byte[102400]; int numFlag= inputStream.read(temp); System.out.println("numFlag:"+numFlag); while(numFlag!= -1) { content.append(temp); numFlag= inputStream.read(temp); System.out.println("numFlag:"+numFlag); } System.out.println("接受的总时间是:"+(System.currentTimeMillis()- startTime)); SAVEINFILE(content); } catch (IOException e) { e.printStackTrace(); System.out.println("接收服务器返回数据时出现异常."); } } public static void receiveData2() { try {// inputStream = client.getInputStream(); byte[] temp= new byte[1000000]; StringBuffer content= new StringBuffer(); int numFlag= inputStream.read(temp); while(numFlag!= -1) { content.append(temp); numFlag= inputStream.read(temp); } System.out.println(content.toString()); SAVEINFILE(content); } catch (IOException e) { e.printStackTrace(); System.out.println("接收服务器返回数据时出现异常."); } } public static final void SAVEINFILE(final Object content) { System.out.println("content的类是:"+content.getClass()); if(content instanceof StringBuffer) { try { System.out.println(content); outputStream.write(content.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); System.out.println("存储到文件中出现异常."); } }else{ if(content instanceof String) { try { outputStream.write(((String) content).getBytes()); } catch (IOException e) { e.printStackTrace(); System.out.println("存储到文件中出现异常."); } }else{ System.out.println("传入的参数没有定义."); } } CLOSE(); } public static final void CLOSE() { try { outputStream.flush(); inputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("关闭输入输出流的时候出现异常."); } }}服务端:public class Server { static ServerSocket server= null;// static OutputStream outputStream= null; static DataOutputStream outputStream= null; static InputStream inputStream= null; static String fileName= ""; static long length= 0; static final int PORT= 1234; static Socket socket= null; static final String PATH= "E:/download/logs/test/SystemOut.log"; public static void main(String[] A) { init(); sendFile(); } public static void init() { try { server= new ServerSocket(PORT); } catch (IOException e) { e.printStackTrace(); System.out.println("服务器启动出现异常"); } System.out.println("服务器已启动."); } public static final void saveFile(final String path, Object content) { /*try { outputStream= new FileOutputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("读取文件时出现异常."); }*/ } public static void readFile(final String path) { try { File file= new File(path); fileName= file.getName(); length= file.length(); inputStream= new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("存储文件时出现异常."); } } public static void sendFile() { while(true) { try { socket = server.accept(); readFile(PATH);// outputStream= socket.getOutputStream(); outputStream= new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); } catch (IOException e) { e.printStackTrace(); System.out.println("等待客户端连接时出现错误."); } System.out.println("开始发送数据."); try { int i= 2; outputStream.writeUTF(fileName); outputStream.flush(); outputStream.writeLong(length); outputStream.flush(); byte[] temp= new byte[1024000]; int n= inputStream.read(temp); System.out.println("本次发送"+n+"个字节."); while(n!= -1) { System.out.println("本次发送了"+n+"个字节;"+"第"+i); outputStream.write(temp); n= inputStream.read(temp); i++; } CLOSE_CONNECTION(); } catch (IOException e) { e.printStackTrace(); System.out.println("发送数据时出现错误."); } } } public static void receiveFile() { try { inputStream= socket.getInputStream(); } catch (IOException e) { e.printStackTrace(); System.out.println("接收文件时出现异常."); } } public static final void CLOSE_CONNECTION() { try { outputStream.flush(); outputStream.close(); socket.close();// server.close(); } catch (IOException e) { e.printStackTrace(); } }}//该片段来自于http://byrx.net

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值