因此,我刚刚学习了如何用Java制作//global directive
Vue.directive('flab',{
inserted:function(el){
el.setAttribute('fab');
el.setAttribute('flat')
}
})
//while using v-btn anywhere (as you have global directive now)
<v-btn
v-flab> //using directive flab with v- prefix
{{btnName}}
</v-btn>
和所有这些好东西,所以我的第一次尝试是从客户端收到一条消息,然后客户端崩溃了。应该发生的是从客户端收到一条消息,如果该消息与此相等,则将数据发送回去。但是,即使消息是正确的,也无法触发socket
函数来确定消息是否正确。
即使我删除了if
函数以检查字符串是否正确,该程序仍然冻结。顺便说一句,我的服务器是一个控制台应用程序,而我的客户端是一个SWT应用程序。
以下是带有删除的if
函数的服务器代码:
if
和客户端(按下按钮时将触发):
try {
System.out.println("Waiting for a connection...");
// Start a server
ServerSocket server = new ServerSocket(3211);
// Listen for anyone at that port
Socket socket = server.accept();
System.out.println("The client has connected!");
// Get the data being sent in
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
DataOutputStream ouputStream = new DataOutputStream(socket.getOutputStream());
// Turn that into UTF-8
String data = inputStream.readUTF();
System.out.println("Received " + data);
ouputStream.writeUTF("Great!");
System.out.println("Awesome!");
socket.close();
inputStream.close();
ouputStream.close;
server.close();
System.out.println("Socket closed\n-----------------------------");
}
catch(IOException e) {
System.out.println(e);
}
当我按下按钮尝试并开始连接(服务器已在运行)时,客户端立即冻结。它甚至不发送任何“连接服务器”之类的东西。
任何想法都出了什么问题,以及如何解决此问题?
答案 0 :(得分:-1)
您的套接字无法发送数据,因为您没有在.flush()
引用中调用outputstream
方法。使用此方法,您不必在流上显式编写flush()
和close()
方法
服务器代码
System.out.println("Waiting for a connection...");
// Start a server
try (ServerSocket server = new ServerSocket(3211)) {
// Listen for anyone at that port
try (Socket socket = server.accept()) {
System.out.println("The client has connected!");
// Get the data being sent in
try (DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()))) {
try (DataOutputStream ouputStream = new DataOutputStream(socket.getOutputStream())) {
// Turn that into UTF-8
String data = inputStream.readUTF();
System.out.println("Received " + data);
ouputStream.writeUTF("Great!");
System.out.println("Awesome!");
}
}
}
System.out.println("Socket closed\n-----------------------------");
} catch (IOException e) {
System.out.println(e);
}
客户代码
try {
allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnecting to the server...");
try (Socket socket = new Socket("127.0.0.1", 3211)) {
allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nConnected to the server!");
try (DataInputStream input = new DataInputStream(socket.getInputStream())) {
try (DataOutputStream output = new DataOutputStream(socket.getOutputStream())) {
output.writeUTF("sweet");
}
String data = input.readUTF();
System.out.println(String.format("data received from server '%s':\n", data));
allMessagesTextBox.setText(allMessagesTextBox.getText() + "\nSERVER: " + data);
}
}
} catch (IOException er) {
allMessagesTextBox.setText(allMessagesTextBox.getText() + "\n" + er);
}
服务器上的输出
Waiting for a connection...
The client has connected!
Received sweet
Awesome!
Socket closed
-----------------------------
客户端上的输出
data received from server 'Great!':
现在要解决代码中的问题。
查看您编写的DataInputStream input = new DataInputStream(System.in);
而不是DataInputStream input = new DataInputStream(socket.getInputStream())
的客户端代码
这导致无法从服务器接收消息
答案 1 :(得分:-1)
您的客户正在读取System.in
。它应该从套接字输入流中读取。
NB您只需要关闭套接字的另一个输出流即可。如有必要,将刷新它并关闭输入流和套接字。目前,您不仅完成了超过必要的交易,而且顺序错误,