如何在被输入java阻止时对事件作出反应
我有一个套接字客户端应用程序。下面的代码应该允许客户端同时从服务器读取回复和读取输入。问题是-这段代码在eclipse中调试时“起作用”,就像我在调试的进程中接收到超出正常流程的消息一样,但如果我正常启动应用程序,它会完全忽略该进程吗?“IDE工作,现实生活不是”综合症最常见的原因是什么 全部文件: 服务器:如何在被输入java阻止时对事件作出反应,java,multithreading,sockets,stream,thread-safety,Java,Multithreading,Sockets,Stream,Thread Safety,我有一个套接字客户端应用程序。下面的代码应该允许客户端同时从服务器读取回复和读取输入。问题是-这段代码在eclipse中调试时“起作用”,就像我在调试的进程中接收到超出正常流程的消息一样,但如果我正常启动应用程序,它会完全忽略该进程吗?“IDE工作,现实生活不是”综合症最常见的原因是什么 全部文件: 服务器: public class Server implements Runnable { static ServerSocket serverSocket; Socket tempSocket
public class Server implements Runnable {
static ServerSocket serverSocket;
Socket tempSocket;
Socket tempSocket2;
static volatile List<User> usersList = new ArrayList<User>();
static boolean waitForNew = true;
PrintWriter tempOut;
volatile User[] tempUser;
volatile boolean isReadingN = false;
public Server(Socket _s, Socket _s2) {
tempSocket = _s;
tempSocket2 = _s2;
}
public Server(PrintWriter nOut, User[] user) {
tempOut = nOut;
tempUser = user;
isReadingN = true;
}
@Override
public void run() {
if (isReadingN) {
while (true) {
if (tempUser != null && tempUser.length > 0
&& tempUser[0] != null)
break;
}
User[] myUser = new User[1];
myUser[0] = tempUser[0];
// myUser[0]=usersList.
while (true) {
if (myUser[0].isCurrentlyLoggedIn() == false)
break;
String[] toSend = null;
if (myUser[0].isNotificable())
toSend = myUser[0].printNotifications().split("\n");
else
continue;
//tempOut.println("");
int sendL=toSend.length;
tempOut.println(String.valueOf(sendL));
for (int i = 0; i < toSend.length; i++)
tempOut.println(toSend[i]);
}
return;
}
Socket clientSocket = tempSocket;
System.out.println("Initiating conversation with the client");
String inputLine;
try {
System.out.print("creating server out...");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
Socket iClientSocket = tempSocket2;
ObjectOutputStream iout = new ObjectOutputStream(
iClientSocket.getOutputStream());
System.out.println("OK!");
System.out.print("creating server in...");
BufferedReader in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
System.out.println("OK!");
System.out.print("creating server image streams...");
System.out.println("OK!");
System.out.println("Server initiating conversation");
User[] currentUser = new User[1];
new Thread(new Server(out, currentUser)).start();
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
boolean[] downloadPicture = new boolean[1];
downloadPicture[0] = false;
String input = Command.call(inputLine, currentUser, usersList,
downloadPicture);
String[] toSend;
if (input != null) {
toSend = input.split("\n");
} else
toSend = new String[0];
out.println(String.valueOf(toSend.length));
for (int i = 0; i < toSend.length; i++)
out.println(toSend[i]);
if (downloadPicture[0]) {
System.out.println("File sent.");
iin.close();
} else{
out.println("1");
out.println("Error: File does not exit.");}
} else
//out.println(" ");
if (inputLine.equals("EXIT")) {
waitForNew = false;
break;
}
}
// End communication graciously
System.out.println("Closing sockets, closing streams");
out.close();
in.close();
clientSocket.close();
serverSocket.close();
} catch (IIOException e) {
System.out.println("Error: Could not find file");
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
System.out.println("Error");
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
// Create socket on port given in argument, localhost
if (args.length == 0) {
System.out
.println("Not enough arguments. Try Server <port number>");
System.exit(-1);
}
int port = 0;
try {
port = Integer.valueOf(args[0]);
System.out.println("Application start");
serverSocket = new ServerSocket(port);
System.out.println("Created socket on port " + port);
} catch (NumberFormatException c) {
System.out
.println("Incorrect port number. Try Server <port number>");
System.exit(-1);
} catch (IOException e) {
System.exit(-1);
}
// Waiting for client
System.out.println("Waiting for client...");
Socket clientSocket = null;
Socket iClientSocket = null;
while (waitForNew) {
try {
clientSocket = serverSocket.accept();
iClientSocket = serverSocket.accept();
new Thread(new Server(clientSocket, iClientSocket)).start();
} catch (IOException e) {
System.out.println("Accept failed: " + port);
System.exit(-1);
}
}
}
公共类服务器实现可运行{
静态服务器套接字服务器套接字;
插座温度插座;
插座温度插座2;
静态volatile List usersList=new ArrayList();
静态布尔waitForNew=true;
印刷作家tempOut;
易变用户[]临时用户;
易失性布尔值isReadingN=false;
公共服务器(套接字,套接字s2){
tempSocket=_s;
tempSocket2=_s2;
}
公共服务器(PrintWriter nOut,用户[]用户){
tempOut=nOut;
tempUser=user;
isReadingN=真;
}
@凌驾
公开募捐{
如果(正在阅读){
while(true){
如果(tempUser!=null&&tempUser.length>0
&&tempUser[0]!=null)
打破
}
用户[]myUser=新用户[1];
myUser[0]=临时用户[0];
//myUser[0]=用户列表。
while(true){
if(myUser[0].isCurrentlyLoggedIn()==false)
打破
字符串[]toSend=null;
如果(myUser[0].IsNotifiable())
toSend=myUser[0].printNotifications().split(“\n”);
其他的
继续;
//tempOut.println(“”);
int sendL=toSend.length;
println(String.valueOf(sendL));
for(int i=0;i
}
客户:
public class Client implements Runnable {
static Socket clientSocket = null;
static Socket iClientSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;
static InputStream iin = null;
public static void main(String[] args) {
int port = Integer.valueOf(args[1]);
String host = args[0];
try {
clientSocket = new Socket(host, port);
iClientSocket = new Socket(host, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
iin = iClientSocket.getInputStream();
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + host);
System.exit(-1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: "
+ host);
System.exit(-1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(
System.in));
String userInput;
try {
new Thread(new Client()).start();
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
}
System.out.println("Closing sockets, closing streams");
out.close();
in.close();
stdIn.close();
iClientSocket.close();
clientSocket.close();
} catch (IOException e) {
System.exit(-1);
}
}
@Override
public void run() {
String a = null;
try {
while (true) {
if((a = in.readLine()) == null)
continue;
int n;
try{
n = Integer.valueOf(a);
}catch(NumberFormatException e){
System.out.println(a);
n=1;
//continue;
}
a = "";
for (int i = 0; i < n; i++)
a += in.readLine() + "\n";
System.out.println(a);
// if(a.contains("POST"),)
if (a.compareToIgnoreCase("EXIT") == 0) {
System.out.println("Exiting");
break;
}
if (a.endsWith("Sending File\n")) {
System.out.println("Recieving image.");
(some unimportant for now code)
System.out.println("Image recieved");
}
}
} catch (IOException e) {
}
}
公共类客户端实现可运行{
静态套接字clientSocket=null;
静态套接字iClientSocket=null;
静态PrintWriter输出=空;
静态BufferedReader in=null;
静态输入流iin=null;
公共静态void main(字符串[]args){
int port=Integer.valueOf(args[1]);
字符串host=args[0];
试一试{
clientSocket=新套接字(主机、端口);
iClientSocket=新套接字(主机、端口);
out=新的PrintWriter(clientSocket.ge