gpt4 book ai didi

java - 无法在 SSLSocket 上写入数据?

转载 作者:行者123 更新时间:2023-11-29 03:39:06 25 4
gpt4 key购买 nike

我编写了一个使用 SSL 套接字的客户端/服务器套接字程序。服务器和客户端之间正在建立连接,但我无法在流上写入数据。以下是服务端和客户端的一段代码

SERVER(这会创建 SSLServerSocket 并为每个客户端创建新线程)

System.setProperty("javax.net.ssl.keyStore", "D:\\client\\server_keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "helloworld");
boolean listening = true;
SSLServerSocketFactory sslserversocketfactory = null;
SSLServerSocket sslserversocket = null;

try {

sslserversocketfactory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
sslserversocket =
(SSLServerSocket) sslserversocketfactory.createServerSocket(Integer.parseInt(args[0]));

} catch (IOException e) {

System.out.println("Input/output error occured :" + e.getMessage());
System.exit(-1);
}

while (listening) {
try {
Thread t = new Thread(new DeprovisionServerThread(sslserversocket.accept()));
if (t != null) {
System.out.println("New thread created: " + t.getName());
}

t.start();
} catch (IOException ex) {
Logger.getLogger(DeprovisionServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
sslserversocket.close();
} catch (IOException ex) {
Logger.getLogger(DeprovisionServer.class.getName()).log(Level.SEVERE, null, ex);
}

服务器(线程处理代码)

 DeprovisionServerThread(Socket socket) {
// throw new UnsupportedOperationException("Not yet implemented");

sslsocket = (SSLSocket) socket;


}

@Override
public void run() {
//throw new UnsupportedOperationException("Not supported yet.");
System.out.println("New client socket connection accpeted from: " + sslsocket.getInetAddress() + " : " + sslsocket.getLocalPort());
try {
//PrintWriter out = new PrintWriter(sslsocket.getOutputStream(), true);

BufferedWriter w = new BufferedWriter(new OutputStreamWriter(sslsocket.getOutputStream()));
BufferedReader in = new BufferedReader(
new InputStreamReader(
sslsocket.getInputStream()));


String inputLine, outputLine;


// out.println(outputLine);

while ((inputLine = in.readLine()) != null) {
// System.out.println(inputLine);
if(inputLine!= null)System.out.println("command received" + inputLine);
w.write("success");

}
// out.close();
in.close();
sslsocket.close();

} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

客户端代码:

System.setProperty("javax.net.ssl.trustStore", "D:\\server\\server_keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "helloworld");
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
PrintStream out = System.out;
SSLSocketFactory f =
(SSLSocketFactory) SSLSocketFactory.getDefault();
try {
SSLSocket c =
(SSLSocket) f.createSocket("localhost", 4444);
// c.s
c.addHandshakeCompletedListener(new MyListener());

c.startHandshake();
Thread.sleep(5000l);
printSocketInfo(c);
BufferedWriter w = new BufferedWriter(
new OutputStreamWriter(c.getOutputStream()));
BufferedReader r = new BufferedReader(
new InputStreamReader(c.getInputStream()));

w.write("deprovision command",0, 10);
System.out.println("send..");
w.flush();
String m ;
while ((m = r.readLine()) != null) {
System.out.println("status received: " + m);


}
System.out.println("after while");
w.close();
r.close();
c.close();
} catch (IOException e) {
System.err.println(e.toString());
}
}

private static void printSocketInfo(SSLSocket s) {
System.out.println("Socket class: " + s.getClass());
System.out.println(" Remote address = "
+ s.getInetAddress().toString());
System.out.println(" Remote port = " + s.getPort());
System.out.println(" Local socket address = "
+ s.getLocalSocketAddress().toString());
System.out.println(" Local address = "
+ s.getLocalAddress().toString());
System.out.println(" Local port = " + s.getLocalPort());
System.out.println(" Need client authentication = "
+ s.getNeedClientAuth());
SSLSession ss = s.getSession();
System.out.println(" Cipher suite = " + ss.getCipherSuite());
System.out.println(" Protocol = " + ss.getProtocol());

}
}

我正在本地系统上测试这段代码。我也用listener检查握手是否没有问题。当一个新客户端到来时,服务器接受来自客户端的连接,并打印我在 printSocketInfo() 方法中打印的所有信息。握手监听器也收到通知。但是当我将数据从客户端套接字写入服务器时,没有任何反应,也不异常(exception)。请帮忙 。提前致谢

最佳答案

你只写了“deprovision command”字符串的前 10 个字符,服务器期望字符串以换行符结尾。将您的客户端代码更改为以下内容:

w.write("deprovision command",0, 10);
w.newLine();
System.out.println("send..");
w.flush();

关于java - 无法在 SSLSocket 上写入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com