我的客户端不处理从服务器发送的命令,但我的服务器可以处理客户端发送的命令。 如果我在调试中运行我的客户端而不是线程“in”正在等待输入字符串和命令继续。
我减少了我的代码以便于阅读。
服务器
public class Server extends Thread{
BufferedReader in;
PrintWriter out;
ServerSocket server;
Socket client = null;
Frame frame;
Server() throws IOException{
frame = new Frame();
try {
server = new ServerSocket(4444);
frame.textAreaForServer.append("Welcome to server side!\n");
} catch (IOException e) {
frame.textAreaForServer.append("Couldn't listen to port 4444\n");
System.exit(-1);
}
addListenerOnTextField();
this.start();
}
private void clearVector(){
frame.textAreaForServer.append("-clr\n");
try {
//send message for client
out.println("-clr");
frame.textAreaForServer.append("Vector is cleared\n");
} catch (NullPointerException e1) {
frame.textAreaForServer.append("It's impossible to perform!\n");
}
}
public void run(){
while (true) {
try {
//connection to client
if (client == null) {
frame.textAreaForServer.append("Waiting for client connection...\n");
client = server.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
frame.textAreaForServer.append("Client connected\n");
}
} catch (IOException e) {
frame.textAreaForServer.append("Can't accept\n");
System.exit(-1);
}
}
}
客户端:
public class Client extends Thread{
Socket fromserver = null;
BufferedReader in = null;
PrintWriter out;
ControlPanel controlPanel;
Client(ControlPanel controlPanel1) throws IOException {
controlPanel = controlPanel1;
addListenerOnTextField();
this.start();
}
//connection to server
private void connect(ControlPanel controlPanel) {
controlPanel.textAreaForClient.append("-ct\n");
if (fromserver == null) {
controlPanel.textAreaForClient.append("Connecting to localhost...\n");
try {
fromserver = new Socket("localhost", 4444);
in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
out = new PrintWriter(fromserver.getOutputStream(), true);
} catch (IOException e1) {
controlPanel.textAreaForClient.append("Server is not available!\n");
}
} else {
controlPanel.textAreaForClient.append("Already connected\n");
}
}
public void run() {
while (true) {
//here the -clr command sent from server should be proceeded, but it doesn't
try {
switch (in.readLine()) {
case "-clr": {
VectorOfThreads.getInstance().getVectorOfThreads().removeAllElements();
controlPanel.textAreaForClient.append("Vector is cleared\n");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
在客户端类中,您似乎忘记在读取任何输入流之前连接到服务器。此外,在服务器线程中创建输出流后,您没有调用ClearVector。
我已根据您的代码实现了一个非常基本的服务器/客户端程序。它工作正常:
服务器类:
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;
import jdk.internal.org.objectweb.asm.tree.analysis.Frame;
/*
* 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 Emad
*/
public class Server extends Thread {
BufferedReader in;
PrintWriter out;
ServerSocket server;
Socket client = null;
Frame frame;
Server() throws IOException {
try {
server = new ServerSocket(4444);
System.out.println("Welcome to server side. listening on port 4444");
} catch (IOException e) {
System.out.println("Couldn't listen to port 4444\\n");
System.exit(-1);
}
this.start();
}
private void clearVector() {
System.out.println(" -clr command send\n");
try {
//send message for client
out.println("-clr");
System.out.println("Vector is cleared\n");
} catch (NullPointerException e1) {
System.out.println("It's impossible to perform\n");
}
}
public void run() {
while (true) {
try {
//connection to client
if (client == null) {
System.out.println("Waiting for client connection...\\n");
client = server.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
clearVector();
System.out.println("Client Connected!");
}
} catch (IOException e) {
System.out.println("Can't accept.\n");
System.exit(-1);
}
}
}
public static void main(String args[])
{
try {
Server serverInstance = new Server();
} catch (IOException ex) {
System.out.println("Exception");
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
客户类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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 Emad
*/
public class Client extends Thread {
Socket fromserver = null;
BufferedReader in = null;
PrintWriter out;
Client() throws IOException {
this.start();
}
//connection to server
private void connect() {
System.out.println("-ct\n");
if (fromserver == null) {
System.out.println("Connecting to localhost...\n");
try {
fromserver = new Socket("localhost", 4444);
in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
out = new PrintWriter(fromserver.getOutputStream(), true);
} catch (IOException e1) {
System.out.println("Server is not available!\n");
}
} else {
System.out.println("Already connected\n");
}
}
public void run() {
connect();
while (true) {
//here the -clr command sent from server should be proceeded, but it doesn't
try {
switch (in.readLine()) {
case "-clr": {
System.out.println("Vector is cleared\n");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[])
{
try {
Client clientInstance = new Client();
} catch (IOException ex) {
System.out.println("Error in instanciating client");
}
}
}