自从两天以来,我一直在受这些代码的困扰。事实上,我正在开发一个具有服务器端和客户端的应用程序。服务器每秒接收来自客户端的请求,通过联系数据库处理请求,然后将结果发送回客户端。我这样做的方式是,如果客户端在服务器之前启动,它将继续尝试连接到给定端口和给定主机上的服务器。
1.这是服务器端:
try
{
Client client = new Client(jTable1,jLabel3);
Thread t = new Thread(client);
t.start();
}catch(IOException e){}
类 Client.java
public class Client implements Runnable{
private int svrPort = 0;
ServerSocket serverConnect = null;
static Socket clientSocket = null;
static ClientConnectThread t[] = new ClientConnectThread[1000];
JTable jtable;
JLabel jlabel;
public Client(JTable table, JLabel label) throws IOException {
this.svrPort = 9450;
this.jtable = table;
this.jlabel = label;
}
public void run(){
try{
serverConnect = new ServerSocket(this.svrPort);
}catch(IOException e){}
while(true){
try{
clientSocket = serverConnect.accept ();
for(int i=0; i<=1000; i++){ //I can accept up to 1000 clients
if(t[i]==null)
{
(t[i] = new ClientThread(client, t, jtable, jlabel)).start();
System.out.println ("Etat12. Apres bloc try");
break;
}
}
}catch(IOException e){}
}
}
}
类 ClientThread.java
public ClientThread(Socket socket, ClientThread t[], JTable table, JLabel label){
this._socket = socket;
this.jTable = table;
this.jlabel = label;
this.totalConnected = 0;
this.t = t;
}
public void run(){
int index = 0;
try{
this._output = new PrintWriter(this._socket.getOutputStream ());
this._input = new BufferedReader(new InputStreamReader(this._socket.getInputStream()));
while((clientMsg = this._input.readLine ()) != null){
if(clientMsg.equals ("@CONNECT")){ // If it is the first time the user is signig in, fill the table
jTable.setValueAt (this._socket.getInetAddress (), index, 0);
jTable.setValueAt (new Date(), index, 1);
jTable.setValueAt (new Date(), index, 2);
totalConnected++;
jlabel.setText ("");
jlabel.setText (totalConnected+"");
}else if(Integer.parseInt (clientMsg) == 1){
int p = Integer.parseInt (clientMsg);
this._output = new PrintWriter(this._socket.getOutputStream(), true);
if (this.getData.connect ())
{
if(this.getData.getDataByType (1).size () == 0){
}
_output.println (this.getData.getDataByPeriod (1));
}else{System.out.println("You are not connected to the database server");}
}else if(Integer.parseInt (clientMsg) == 2){
int p = Integer.parseInt (clientMsg);
this._output = new PrintWriter(this._socket.getOutputStream(), true);
if (this.getData.connect ())
{
if(this.getData.getDataByPeriod (2).size () == 0)System.out.println ("There is no data corresponding");
this._output.println (this.getData.getDataByPeriod (2));
}else{System.out.println("You are not connected to the database server");}
}else if(Integer.parseInt (clientMsg) == 3){
int p = Integer.parseInt (clientMsg);
this._output = new PrintWriter(this._socket.getOutputStream(), true);
if (this.getData.connect ())
{
if(this.getData.getDataByType (3).size () == 0)System.out.println ("There is no data corresponding");
this._output.println (this.getData.getDataByType (30));
}else{System.out.println("You are not connected to the database server");}
}else if(Integer.parseInt (clientMsg) == 4){
int p = Integer.parseInt (clientMsg);
this._output = new PrintWriter(this._socket.getOutputStream(), true);
if (this.getData.connect ())
{
if(this.getData.getDataByType (4).size () == 0)System.out.println ("There is no data corresponding");
this._output.println (this.getData.getDataByType (60));
}else{System.out.println("You are not connected to the database server");}
}else{
}
}
this._input.close ();
this._output.close ();
}catch(IOException e){}
}
这是使我的服务器运行的两个类。Client.java 类启动并等待接受连接。当客户端连接时,会创建一个 clientThread 实例并将其关联到客户端。直到这里,一切似乎都运行良好。
客户端
public class ServerConnect implements Runnable{
public static Socket clientSocket = null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static int port=9450;
public static String host = "127.0.0.1";
public static JLabel myLabel;
public static JButton button;
public static ResourceMap resourceMap;
private static String serverMsg = "";
public ServerConnect(JLabel jLabel, JButton b)
{
jLabel.setText ("Trying to contact the server");
myLabel = jLabel;
button = b;
port = Integer.parseInt("9450");
host = "127.0.0.1";
try{
clientSocket = new Socket(host, port);
}catch(IOException e){e.printStackTrace ();}
}
public void run()
{
while(true){
while(!this.connect ())
{myLabel.setText ("You are not connected to the server : "+host);
button.setEnabled (false);
try{
clientSocket = new Socket(host, port);
}catch(IOException e){}
}
myLabel.setText ("You are connected to the server : "+host);
button.setEnabled (true);
try{
out = new PrintWriter(clientSocket.getOutputStream(), true);
out.println("@CONNECT");
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while((serverMsg = in.readLine ()) != null){
System.out.println ("<=> :"+serverMsg);
}
}
catch(IOException e){e.printStackTrace ();}
}
}
private boolean connect()
{
try{
clientSocket = new Socket(host, port);
return true;
}catch(IOException e){}
return false;
}}
我的问题是,当双方启动时,客户端发送@CONNECT 的唯一内容,服务器接收它并全部停止在这里。如果客户端再次发送请求,服务器将不响应。
我希望有人逐步向我展示如何设置此应用程序
- 服务器端。接受带有 WHILE 循环的线程中的连接
- 客户端。在另一个线程中每次尝试联系服务器以建立连接
- 在另一个线程中,客户端再次向服务器发送请求
- 服务器是另一个线程从数据库中请求信息并发送回客户端。
我非常感谢你的帮助