问题描述
- 求 如何在Socket长连接中传参数
-
package cn.qmhd.socket;import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.CharBuffer;/**
- Socket长连接 服务端
*/
public class SocketServer {private ServerSocket serverSocket;
private Socket socket;
private int port = 5055;private void start() throws Exception {
serverSocket = new ServerSocket(port);
while (true) {
socket = serverSocket.accept();
new Thread(new SocketThread(socket)).start();// 多线程阻塞
}
}/**
- 处理socket连接s
*/
class SocketThread implements Runnable {
private Socket socket;
private String temp = "";public SocketThread(Socket socket) {
this.socket = socket;
}@SuppressWarnings("unused")
public void run() {
try {
Reader reader = new InputStreamReader(socket.getInputStream());
Writer writer = new OutputStreamWriter(socket.getOutputStream());
CharBuffer charBuffer = CharBuffer.allocate(1024);
int readIndex = -1;
while ((readIndex = reader.read(charBuffer)) != -1) {
charBuffer.flip();
temp = charBuffer.toString();
System.out.println("server-->" + temp);
writer.write("this is server");
writer.flush();} } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { if (!socket.isClosed()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
}
}
public static void main(String[] args) {
SocketServer s = new SocketServer();
try {
s.start();
} catch (Exception e) {
e.printStackTrace();
}
} - 处理socket连接s
}
package cn.qmhd.socket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.*;
import java.nio.CharBuffer;public class SocketClient {
private String host = "127.0.0.1";
private int port = 5055;
private String strs = "aaaa";
private static PrintWriter pw = null;
private Socket socket = null;public PrintWriter getInstance(){ if(pw==null){ try { System.out.println("pw=null"); // 创建Socket socket = new Socket(host, port); pw = new PrintWriter(new OutputStreamWriter( socket.getOutputStream())); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("pw!=null"+pw); return pw; } public PrintWriter getPw() { return pw; } public void setPw(PrintWriter pw) { this.pw = pw; } /** * 数据发送线程 */ public static void send(SocketClient sc,String thisstrs){ sc.getInstance(); System.out.println(pw); pw.write(thisstrs); } class SendThread implements Runnable { public void run() { while (true) { try { pw.write(strs); pw.flush(); Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } } } } class ReceiveThread implements Runnable { private Socket socket; public ReceiveThread(Socket socket) { this.socket = socket; } public void run() { while (true) { try { Reader reader = new InputStreamReader( socket.getInputStream()); CharBuffer charBuffer = CharBuffer.allocate(8192); @SuppressWarnings("unused") int charIndex = -1; while ((charIndex = reader.read(charBuffer)) != -1) { charBuffer.flip(); System.out.println("client--> " + charBuffer.toString()); } } catch (IOException e) { e.printStackTrace(); } } } } private void start() { new Thread(new SendThread()).start();// 启动读线程
// send("abcdefg");
new Thread(new ReceiveThread(socket)).start();// 启动收线程}
public static void main(String[] args) { SocketClient sc = new SocketClient(); sc.getInstance(); sc.start(); sc.send(sc,"--------------------------------");
// sc.send("abcdefg");
}}
package cn.qmhd.socket;
import java.io.PrintWriter;
public class Test {
public static void main(String[] args) { System.out.println("asdfgfhgj"); SocketClient sc = new SocketClient(); sc.send(sc,"--------------------------------");
// PrintWriter pw = sc.getInstance();
// System.out.println("Test:"+sc.getInstance());}
}
- Socket长连接 服务端