Java的socket连接以及string字符串长度过长解决

一、socket连接服务:
1.客户端:Java

package cn.lw;

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

import com.alibaba.fastjson.JSONObject;

public class SocketServer {
	public void StartServer() {
		ServerSocket serverSocket=null;
		try {
			serverSocket=new ServerSocket(8998);
			System.out.println("服务端启动。。。。。。");
			while(true) {
				Socket socket=serverSocket.accept();//socket服务端接入启动
				new Thread(new MyRuns(socket)).start();//新建线程启动运行socket
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(serverSocket!=null) {
					serverSocket.close();
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	class MyRuns implements Runnable{
		Socket socket;
		BufferedReader reader;
		BufferedWriter writer;
		public MyRuns(Socket socket) {
			super();
			this.socket=socket;
		}
		@Override
		public void run() {
			try {
				reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
				writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
				String lineString=null;
				String cloud="";
				while((lineString=reader.readLine())!=null) {
					cloud+=lineString;
				}
				System.out.println("接收客户端消息:"+cloud);
				JSONObject jsonObject=new JSONObject();//json对象服务器发送数据
				jsonObject.put("success", true);
				jsonObject.put("Object", "{数据集合以,隔开}");
				writer.write(jsonObject.toJSONString());
				writer.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				try {
					if(reader!=null) {
						reader.close();
					}
					if(writer!=null) {
						writer.close();
					}
					if(socket!=null) {
						socket.close();
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) {
		SocketServer server=new SocketServer();
		server.StartServer();
	}
}

2.客户端:Java代码

package cn.lw;

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



public class SocketClient {
	public static void main(String[] args) {
		System.out.println("客户端执行。。。");
		getData(new byte[0]);
	}
	/**
	 * socket连接的建立
	 * @return
	 */
	public static String getData(byte[] bs) {
		BufferedReader br = null;
        InputStream ins = null;
        BufferedWriter pw = null;
        OutputStream os = null;
        Socket sk = null ;
        try{
            String address = "localhost";//IP地址
            int  port = 18000;//端口
            sk = new Socket(address,port);//建立连接
            os = sk.getOutputStream();//向服务器发送数据
            pw = new BufferedWriter(new OutputStreamWriter(os));
            
            
            pw.write("{ \"img\": \""+bs+"\" }");//数据以JSON格式发送
            pw.flush();
            sk.shutdownOutput();
            ins = sk.getInputStream();//从服务器获取数据
            br = new BufferedReader(new InputStreamReader(ins));
            String str = null ;
            StringBuffer sb = new StringBuffer();//数据存储位置
            while((str = br.readLine()) !=null){
                sb.append(str);
            }
            System.out.println("接收服务端返回消息:"+sb);
            
    		return "success";
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try {
                br.close();
                pw.close();
                ins.close();
                br.close();
                if(null != sk)  {
                    sk.close();     
                }
            } catch (IOException e) {
                e.printStackTrace();
            }  
        }
		return "";
	}
}

3.Java中string字符串长度过长解决办法

//通过stringbuild分割加入
StringBuilder sb = new StringBuilder();
sb.append(文本的一部分);
sb.append(文本剩下的一部分);
.....
String str = sb.toString();
此时可以对str进行操作
System.out.printLn(str);

你可能感兴趣的:(部分知识)