我正在使用Java创建一个客户端服务器应用程序,允许多个人使用客户端swing应用程序(即记事本)连接到服务器。一旦连接上,每个客户端都必须请求控制记事本以便编辑它,并且在完成后放弃控制权,将其结果发送到其他客户端的记事本上显示。
我面临的主要问题是多线程服务器逻辑,使用主服务器实例和几个线程,每个线程处理与客户端的通信。
我不确定我选择的结构是否可以工作,或者是否会有一些涉及线程运作的问题导致数据损坏或其他线程相关问题。
无论如何,这是服务器代码,我想知道这个系统是否能够正常工作?当然还有更多的逻辑需要添加,例如限制连接数量、等待锁定列表等,但我主要关心线程之间的通信。
我还想知道如何从线程内部访问服务器实例方法,因为我不确定。 -注意,这已经得到解决,我使用了共享的“锁”对象,其中包含线程实例的列表,每个线程实例都有锁实例,因此它们可以相互调用方法。
非常感谢。
我面临的主要问题是多线程服务器逻辑,使用主服务器实例和几个线程,每个线程处理与客户端的通信。
我不确定我选择的结构是否可以工作,或者是否会有一些涉及线程运作的问题导致数据损坏或其他线程相关问题。
无论如何,这是服务器代码,我想知道这个系统是否能够正常工作?当然还有更多的逻辑需要添加,例如限制连接数量、等待锁定列表等,但我主要关心线程之间的通信。
我还想知道如何从线程内部访问服务器实例方法,因为我不确定。 -注意,这已经得到解决,我使用了共享的“锁”对象,其中包含线程实例的列表,每个线程实例都有锁实例,因此它们可以相互调用方法。
非常感谢。
import java.net.*;
import java.io.*;
import java.util.ArrayList;
public class server {
private ArrayList<ClientServiceThread> SocketList;
private int lock = 0;
private ServerSocket myServerSocket;
private Socket mySocket;
public static void main(String[] args)
{
server myserver = new server();
}
public server()
{
/**
* This will (when finished) accept only a certain number of connections,
* and will then finish the constructor by breaking the while loop. It will
* then sit here waiting for the synchronised methods to be called by its worker
* threads.
*/
try{
myServerSocket = new ServerSocket(8080);
}catch(Exception e)
{
System.out.println("Could not create serversocket "+e);
}
int id = 1;
while(true)
{
try{
mySocket = myServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(mySocket, id);
SocketList.add(cliThread);
id++;
cliThread.start();
}catch(Exception e)
{
System.out.println("Problem with accepting connections");
}
}
}//end constructor
public synchronized boolean acquireLock(int id)
{
/**
* Here any spawned thread can try to acquire the lock,
* so it can be the one to send the data (synchronised to prevent data corruption)
*/
if(this.lock == 0){
this.lock = id;
return true;
}
else
{
return false;
}
}
public synchronized void releaseLock(int id)
{
/**
* Any thread can call this, releasing the lock. of course, the lock will only be
* released if the thread calling it actually owns the lock.
*/
if(id == this.lock)
{
this.lock = 0;
}
else
{
//do nothing
}
}
public synchronized void publish(String toSend)
{
/**
* When a thread in control of the lock wants to publish to all other threads, it
* invokes this method, which then calls another synchronised method on each thread
* in the list, telling it to write to it's client with the new data.
*/
for(int i = 0; i<this.SocketList.size(); i++)
{
if(i != this.lock)
{
this.SocketList.get(i).sendData(toSend);
}
}
}
}
class ClientServiceThread extends Thread{
Socket mySocket;
int id;
boolean hasControl = false;
public ClientServiceThread(Socket mySocket, int id)
{
/**
* this constructor gives it the ID and the socket for communication, it will
* then be run
*/
this.mySocket = mySocket;
this.id = id;
}
@Override
public void run()
{
//listen, it will be either a request, or some data
//based on whether the client is the one in control or not (hasControl)
try{
//create buffered reader
if(!this.hasControl)
{
//it has control, so wait for the lines
}
else
{
//read in one line and then call acquire lock because we know
//that it has sent a request for control
// how do i access the original class for acquireLock();?
}
}catch(IOException e)
{
System.out.println("Problem reading from the socket");
}
}
public synchronized void sendData(String toSend)
{
//create writer and send to my client, saying "true" or some other message
//the client will recognise as the go-ahead to edit the data.
}
}