我正在尝试实现客户端服务器套接字连接,其中我通过GUI传递ls / pwd等命令,并使用url(localhost)在端口上建立服务器连接。虽然我能够与客户端建立连接,但代码不会超出客户端连接接受状态。即它不会读取客户端通过套接字发送的服务器端的输入。下面是我的三个类,Mainserver,ClientHandler(它处理服务器的线程连接)和客户端。
这是执行客户端操作按钮的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEventevt) {
command = jTextField1.getText();
String url = jTextField3.getText();
try {
System.out.println("Before socket connection");
Socket socket = new Socket(url, 9002);
System.out.println("After socket connection");
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("After Buffered readers");
System.out.println("After getting streams");
if (socket != null) {
try {
int x = Integer.parseInt(command);
flag = 1;
} catch (Exception e) {
flag = 0;
}
if (flag == 0) {
String[] cmd = {"/bin/sh", "-c", command};
System.out.println("the value of command in GUI class is " + Arrays.toString(cmd));
try {
String commd = Arrays.toString(cmd);
System.out.println(commd);
out.write(commd);
input = in.readLine();
}
catch (IOException ex1)
{
Logger.getLogger(TestGUI.class.getName()).log(Level.SEVERE, null, ex1);
}
jTextField2.setText(input.toString());
}
}
}//try end of the first one
catch (IOException ex) {
Logger.getLogger(TestGUI.class.getName()).log(Level.SEVERE, null, ex);
}
服务器类:
public class ServerMain {
public static void main(String[] args) throws IOException, InterruptedException {
int number, temp;
try {
ServerSocket serverSocket = new ServerSocket(9002);
System.out.println("server has been started in the server");
System.out.println("Server is waiting connection at" + InetAddress.getLocalHost().getCanonicalHostName() + "port" + serverSocket.getLocalPort());
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client Connection Accepted");
//pass on handling on this client to a thread
(new ClientHandler(socket)).start();
}
} catch (Exception e) {
System.err.println("Server already in use");
System.exit(-1);
}
}
}
服务器的客户端处理程序:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ameerah
*/
public class ClientHandler extends Thread {
private static int BUFSIZE = 1024;
private StringBuffer result;
ServerSocket serverSocket;
String serverText;
StringBuffer output = new StringBuffer();
private Object serversocket;
public Socket getSock() {
return sock;
}
public void setSock(Socket sock) {
this.sock = sock;
}
Socket sock;
public ClientHandler(Socket sock) {
this.sock = sock;
}
@Override
public void run() {
PrintWriter outWriter = null;
try {
BufferedReader myInput = new BufferedReader(new InputStreamReader(sock.getInputStream()));
outWriter = new PrintWriter(sock.getOutputStream());
System.out.println(
"before accepting the command in server");
String inputLine;
while ((inputLine = myInput.readLine()) != null) //String command = myInput.readLine();
{
System.out.println(inputLine);
String result = "";
try {
result = executeCommand(inputLine);
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(result);
outWriter.write(result);
}
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} finally {
outWriter.close();
}
}
public String executeCommand(String cmd)
throws IOException, InterruptedException {
try {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader
= new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("Inside the execute method");
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
我已经使用了一段时间,并尝试使用不同的流,如ObjectInputStream,ObjectOutputStream,但代码每次都会挂起。我不知道在这一点上我出错了:(我在几个论坛上搜索过但我仍然不知道我在哪里出错。应该感谢任何帮助。 最诚挚的问候
答案 0 :(得分:1)
最后期待'\ n'的是readLine()。因此,一旦我在末尾附加'\ n'并添加了out.flush()它就能够读取并且不会一直等待更多输入,现在应用程序正在运行。 非常感谢你的有用建议。事实证明out.flush()的建议非常有用。
答案 1 :(得分:0)
几个提示可以解决问题。
检查命令的值并捕获异常堆栈跟踪。
out.write(commd);
之后:再添加一行out.flush();
刷新后,服务器将从客户端获取数据。 outWriter的情况也是如此。在写完数据后,应在 outWriter 上调用flush()
。
答案 2 :(得分:0)
您正在寻找结束输入循环的行尾,但您正在使用write。
更改发送数据语句以使用println。
客户端:
out.println(commd);
服务器:
outWriter.println(result);