我正在尝试构建一个应用程序,其中服务器是银行,客户端是该银行的分支,因此它是经典的多线程服务器/客户端应用程序。第一步,我希望银行记录连接到它的每个分行。所以我想将布兰克作为对象发送到套接字的对象流中,以便银行可以提取并记录它。
这是我到目前为止所做的
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Banque {
private List<Succursale> listSucc = new ArrayList<Succursale>();
private int sommeTotale;
private int nbSuccInit = 4;
public void ajouteSucc(Succursale suc){
}
public Banque(){
initialiserServeur();
}
private void initialiserServeur() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(10118);
}
catch (IOException e)
{
System.err.println("On ne peut pas ecouter au port: 10118.");
System.exit(1);
}
System.out.println ("Le serveur est en marche, Attente de la connexion.....");
int i = 0;
while(i<5){
try {
UtilMultiTh mt = new UtilMultiTh(serverSocket.accept());
Thread t = new Thread(mt);
t.start();
listSucc.add(mt.getSuc());
System.out.println(listSucc.size());
for(int j =0; j<listSucc.size();j++){
System.out.println("La succursale "+(j+1)+" est:"+listSucc.get(j).getName());
}
i++;
System.out.println("FLAGPOSTban");
}
catch (IOException e)
{
System.err.println("Accept a echoue.");
System.exit(1);
}
}
System.out.println ("connexion reussie");
System.out.println ("Attente de l'entree.....");
}
public static void main (String [] args){
Banque banK = new Banque();
}
}
管理分支的多线程连接的 MultiTh 类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.*;
public class UtilMultiTh implements Runnable {
private Socket soc;
private Succursale suc;
public UtilMultiTh(Socket s){
System.out.println("FLAGconsmth");
this.soc = s;
}
public void run() {
System.out.println("FLAGPOSrun");
ObjectOutputStream oos;
ObjectInputStream ois;
try{
oos = new ObjectOutputStream(soc.getOutputStream());
ois = new ObjectInputStream(soc.getInputStream());
//System.out.println("La succ est");
try {
Object o = ois.readObject();
if(o!=null){
suc = (Succursale)o;
//System.out.println("La succ est"+suc.getName());
}
/*while(o!=null){
suc = (Succursale)o;
System.out.println("La succ est"+suc.getName());
}*/
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
oos.close();
ois.close();
soc.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public synchronized Succursale getSuc() {
return suc;
}
public void setSuc(Succursale suc) {
this.suc = suc;
}
}
这是分支的 Succursale 类
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class Succursale implements Serializable {
private String coordonnees;
private String name;
private int sommeDepart;
private int sommeRecue;
private int sommeEnvoyee;
private List<Succursale> listSuccAc = new ArrayList<Succursale>();
private GuiSuccursale succView;
public Succursale (){
succView = new GuiSuccursale(Constantes.sommeDepart,1);
this.sommeDepart=Constantes.sommeDepart;
this.name="Succursale: "+(1);
connexionBanque();
}
public void connexionBanque(){
String host = Constantes.adrBanque[0];
int port = Constantes.portBanque[0];
Socket echoSocket = null;
try {
echoSocket = new Socket(host, port);
ObjectOutputStream oos = new ObjectOutputStream(echoSocket.getOutputStream());
oos.writeObject(this);
System.out.println("FLAGPOSTSUcc");
} catch (UnknownHostException e) {
System.err.println("Hôte inconnu: " + host);
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.err.println("Ne pas se connecter au serveur: " + host);
System.exit(1);
}
}
public void connexionSuccursales(){
}
public void envoiManuel(){
}
public String getName() {
return name;
}
public void envoiPeriodique(){
}
public static void main (String[] args){
Succursale suc = new Succursale();
}
}
我有两个问题,我如何从 UtilMultuTh 返回一个 Succursale 到 Banque,在此之前为什么在 UtilMultiTh 类中 readObject 返回 null 而在建立连接之后我将类放入套接字中的 succursale 类?我必须在这里放一个无限循环吗?
编辑:我更改了代码,现在 multith 正确地从套接字获取 Succursale,现在的问题是线程没有同步,因为 UtilMultiTh 在 Banque 想要获取 Succursale 之后得到它,我不熟悉同步,我怎么能告诉 Banque 只有在 utilMultiTh 运行后才执行 getSuccursale ?