我一直在搞乱套接字,我试图创建一个具有多线程的服务器,以便从客户端获取请求。每个请求都有它的特殊功能,因此客户端可以使用服务器从另一个客户端获得响应。三种类型的客户端发送类型为A的套接字号为2000,类型B的套接字号为3000,类型为C,套接字号为4000.但是我遇到了无法解决的情况软件导致连接中止:套接字写入错误。尝试从具有套接字3000的客户端B向服务器发送请求然后将请求从服务器发送到具有套接字4000的客户端C时,发生此错误,客户端C收集必要的数据并将其发送回服务器以便最终到达其目的地客户B.使用 ObjectOutputStream 和 ObjectInputStream
交换客户端之间发送的所有数据请注意,建议使用服务器发送请求并从客户端获取响应的正确方法。
这是我的代码:
服务器代码:
public class Saca {
public static final int portAvion = 2000;
public static final int portRadar = 3000;
public static final int portControlleur = 4000;
public static ServerSocket serveurPortAvion;
public static ServerSocket serveurPortRadar;
public static ServerSocket serveurPortControlleur;
public static Socket socketAvion;
public static Socket socketRadar;
public static Socket socketControlleur;
public static void main(String []args) throws IOException, ClassNotFoundException{
new Saca().demarerServeur();
}
public void demarerServeur() throws IOException, ClassNotFoundException{
serveurPortAvion = new ServerSocket(portAvion);
serveurPortRadar = new ServerSocket(portRadar);
serveurPortControlleur = new ServerSocket(portControlleur);
System.out.println("Le serveur est lance");
while(true){
try {
socketAvion = serveurPortAvion.accept();
SacaThread sacaThreadAvion = new SacaThread(socketAvion);
sacaThreadAvion.start();
socketRadar = serveurPortRadar.accept();
SacaThread sacaThreadRadar = new SacaThread(socketRadar);
sacaThreadRadar.start();
socketControlleur = serveurPortControlleur.accept();
SacaThread sacaThreadControlleur = new SacaThread(socketControlleur);
sacaThreadControlleur.start();
} catch (Exception ex) {
System.out.println("Message d'erreur : " + ex.getMessage());
}
}
}
服务器多线程代码:
public class SacaThread extends Thread{
Socket socket = null;
SacaThread(Socket socket){
this.socket = socket;
}
public void run(){
try {
ObjetInter objetInter = null;
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
//ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
while ((objetInter = (ObjetInter)objectInputStream.readObject()) != null) {
switch (socket.getLocalPort()) {
case 2000:
//LES TACHES D'AVION
System.out.println(objetInter.getMessage());
if(objetInter.getAction().toLowerCase().equals("envoi_donnees") && Saca.socketRadar.isConnected()){
//ServerSocket socketServerRadar = new ServerSocket(portRadar);
//Socket socketRadar = socketServerRadar.accept();
ObjectOutputStream objectOutputStreamRadar = new ObjectOutputStream(Saca.socketRadar.getOutputStream());
objectOutputStreamRadar.writeObject(objetInter);
objectOutputStreamRadar.close();
}
break;
case 3000:
//LES TACHES DU RADAR
//System.out.println(objetInter.toString());
if(objetInter.getAction().toLowerCase().equals("avoir_liste") && Saca.socketControlleur.isConnected()){
ObjectOutputStream objectOutputStreamControllleur = new ObjectOutputStream(Saca.socketControlleur.getOutputStream());
objectOutputStreamControllleur.writeObject(objetInter);
objectOutputStreamControllleur.close();
}
break;
case 4000:
//LES TACHES DU CONTROLLEUR
//System.out.println(objetInter.toString());
if (objetInter.getAction().toLowerCase().equals("avoir_liste") && Saca.socketRadar.isConnected()) {
ObjectOutputStream objectOutputStreamRadar = new ObjectOutputStream(Saca.socketRadar.getOutputStream());
objectOutputStreamRadar.writeObject(objetInter);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
objectOutputStreamRadar.close();
}
break;
}
}
objectInputStream.close();
socket.close();
} catch (IOException ex) {
//System.out.println("Radar exception : " + ex.getMessage());
} catch (ClassNotFoundException ex) {
//System.out.println("Radar exception : " + ex.getMessage());
}
}
B类
public class Radar {
public int PAUSE = 1000;
public int tempsMiseAJour = 10000;
public int tempsPasse = 0;
private static ArrayList<String> listAvion = new ArrayList<String>();
Socket socket;
ObjectOutputStream objectOutputStream;
ObjectInputStream objectInputStream;
boolean ouvrir_communication() {
boolean conxOuverte = false;
try {
socket = new Socket("localhost", Saca.portRadar);
conxOuverte = true;
} catch (IOException ex) {
//System.out.println(ex.getStackTrace());
return conxOuverte;
}
return conxOuverte;
}
void fermer_communication() {
try {
if (socket.isConnected()) {
socket.close();
}
} catch (IOException ex) {
}
}
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
Radar r = new Radar();
r.lancerRadar();
}
void actionSurListe() throws ClassNotFoundException {
try {
ObjetInter objetInter = null;
objectInputStream = new ObjectInputStream(socket.getInputStream());
//objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
while ((objetInter = (ObjetInter) objectInputStream.readObject()) != null) {
switch (objetInter.getAction().toLowerCase()) {
case "envoi_donnees":
...
break;
case "avoir_liste":
envoie_listeAvion();
break;
}
}
objectInputStream.close();
} catch (IOException ex) {
}
}
void lancerRadar() throws ClassNotFoundException {
while (true) {
try {
ouvrir_communication();
Thread.sleep(PAUSE);
actionSurListe();
tempsPasse += PAUSE;
if (tempsPasse == tempsMiseAJour) {
afficherListAvion();
tempsPasse = 0;
}
fermer_communication();
} catch (InterruptedException ex) {
}
}
}
void envoie_listeAvion() throws IOException {
if (socket.isConnected()) {
try {
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(new ObjetInter("Radar", "envoi_liste", listAvion.toString()));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
objectOutputStream.flush();
objectOutputStream.close();
} catch (IOException ex) {
System.out.println("Exception " + ex.getMessage());
objectOutputStream.close();
}
} else {
System.out.println("Socket non connecte !");
}
}
C类
public class Controlleur implements Runnable {
static int selectionMenu = -1;
Socket socket;
ObjectOutputStream objectOutputStream;
ObjectInputStream objectInputStream;
private Avion avion;
static volatile boolean keepRunning = true;
static Controlleur test ;
static Thread t;
boolean ouvrir_communication() {
boolean conxOuverte = false;
try {
socket = new Socket("localhost", Saca.portControlleur);
conxOuverte = true;
} catch (IOException ex) {
return conxOuverte;
}
return conxOuverte;
}
void fermer_communication() {
try {
if (socket.isConnected()) {
socket.close();
}
} catch (IOException ex) {
}
}
void demandeListAvion(){
try {
ObjetInter objetInter = null;
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(new ObjetInter("Controlleur", "avoir_liste", ""));
objectOutputStream.flush();
} catch (IOException ex) {
}
}
void afficherListAvion() throws ClassNotFoundException, IOException{
try {
ObjetInter objetInter = null;
objectInputStream = new ObjectInputStream(socket.getInputStream());
while ((objetInter = (ObjetInter) objectInputStream.readObject()) != null) {
switch (objetInter.getAction().toLowerCase()) {
case "envoi_liste":
objetInter.getMessage().toString();
break;
}
}
//objectInputStream.close();
} catch (IOException ex) {
}
}
public void run() {
System.out.println("Starting to loop.");
while (keepRunning) {
ouvrir_communication();
System.out.println("Running loop...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
switch(selectionMenu){
case 0:
//System.out.println("0 est selectionné");
{
try {
Thread.sleep(3000);
demandeListAvion();
Thread.sleep(3000);
afficherListAvion();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Controlleur.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Controlleur.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
}
}
break;
}
}
fermer_communication();
}
afficherMenu();
System.out.println("Done looping.");
}
public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
test = new Controlleur();
afficherMenu();
}
static void afficherMenu(){
selectionMenu = -1;
keepRunning = true;
t = new Thread(test);
t.start();
System.out.println("Choisir du menu : ");
System.out.println("--------------------------------");
System.out.println("0 - Quitter Menu");
System.out.println("1 - Afficher liste des avions");
System.out.println("2 - Attacher pilote à une avion");
Scanner s = new Scanner(System.in);
while ((selectionMenu = s.nextInt()) != 0);
test.keepRunning = false;
t.interrupt(); // cancel current sleep.
}