java的socket读取一行就结束运行了?使用这种方法可以读取多行数据!

51 阅读2分钟

目录

引出问题

第一种(只能读取一行数据)

第二种(可以读取多行数据)


引出问题

写一个socket的公共方法,结果发现socket读取一行数据就结束了,百思不得其解。

在网上也找了一些资料,很多也有坑,这里贴出来自己用的两种方法,以后就不用一直找解决方案了。

第一种(只能读取一行数据)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

	final static String ADDRESS = "127.0.0.1";
	final static int PORT = 8765;
	
	public static void main(String[] args) {
		
		Socket socket = null;
		BufferedReader in = null;
		PrintWriter out = null;
		
		try {
			socket = new Socket(ADDRESS, PORT);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);
			
			//向服务器端发送数据
			out.println("接收到客户端的请求数据...");
			String response = in.readLine();
			System.out.println("Client: " + response);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out != null){
				try {
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(socket != null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			socket = null;
		}
	}
}

第二种(可以读取多行数据)

import java.io.*;
import java.net.Socket;

public class PostmanSocketController {


	
	
	public String socketClient(String ip, Integer port, String reqInfo, String bm) throws Exception {
		Socket s = null;
        OutputStream out = null;
        InputStream in = null;
        String rs = "";
        try {
            s = new Socket(ip, port);
            s.setSendBufferSize(4096);
            s.setTcpNoDelay(true);
            s.setSoTimeout(60*1000);
            s.setKeepAlive(true);
            out = s.getOutputStream();
            //in = s.getInputStream();
            
            //准备报文msg
            //编码
            out.write(reqInfo.getBytes(bm));
            out.flush();
            
            rs = readAll(s, bm);
            
            
        } catch (Exception e) {
            throw e;
        }finally{
            try {
                if(out!=null){
                    out.close();
                    out = null;
                }
                if(in!=null){
                    in.close();
                    in = null;
                }
                if(s!=null){
                    s.close();
                    s = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        return rs;
	}
	
	public static String readAll(Socket socket, String bm) throws IOException {
	    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), bm));
	    StringBuilder sb = new StringBuilder();
	    String line;
	    while ((line = reader.readLine()) != null)
	        sb.append(line).append("\n");
	    return sb.toString();
	}
	
}