I am creating a client-server application in java that will allow several people using a client-side swing application (notepad) to connect to the server. After connecting, each client will have to request control over the notebook so that he can edit it, and then give up control by sending his result to display on the panel all other clients.
The main problem that I encountered is multi-threaded server logic using an instance of the main server and several threads, each of which handles communication with the client.
Iโm not sure if the structure I selected will work or if there will be some problem related to a lack of understanding of how streams work, which leads to data corruption or any other problem related to the stream.
In any case, this is the server code, I was wondering, can someone tell me if this system will work without errors? Of course, add more logic, for example, a cap on the number of connections, a waiting list for blocking, etc., but I'm mainly connected with the connection between threads.
I am also wondering how to access the server instance methods from within the stream, as I am not sure. -number, this has been clarified, I use a common โlockโ object, which has a list of thread instances, and each thread instance has a lock instance, so they can call methods on eachother.
Thank you very much.
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() { 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"); } } }