-1

so i'm trying to create a chess server for a chess application i wrote in java. The two classes i'm including are the main class that starts my TCPServerThreads and this class itselve.

I am able to connect two clients and for example echo their input back to them, but i have no idea, how to exchange information between these two threads. I am trying to forward Server inputs from one client towards the main class, or directly to the other client, so i can update the chess field on the client.

It's pretty much my first time working with servers, so thanks in advance for you patience.

This is my main class:

package Main;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import TCP.TCPServerThread;

public class Main {

    public static final String StopCode = "STOP";
    public static final int PORT = 8888;
    public static int count = 0;
    
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        
        
        //create Server Socket
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("serverSocket created");
        
        
        //accept client Sockets
        while (count < 2) {
            try {
                socket = serverSocket.accept();
                count ++;
                System.out.println("socket Nr " + count + " accepted");
                
            } catch (IOException e) {
                System.out.println("I/O error: " + e);
            }
            // new thread for a client
            new TCPServerThread(socket).start();
        }
    }
}

And this is the TCPServerThread:

package TCP;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Timestamp;

import Main.Main;

public class TCPServerThread extends Thread{
    
    
    
    Timestamp ts;
    private int port = 0;
    
    Socket socket;
    
    
    public TCPServerThread(Socket clientSocket) {
        this.socket = clientSocket;
    }
    
    
    
    public void run() {
        InputStream is = null;
        BufferedReader br = null;
        DataOutputStream os = null;
        
        try {
            is = socket.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            os = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            return;
        }
        String line;
        while (true) {
            try {
                line = br.readLine();
                
                if ((line == null) || line.equalsIgnoreCase("QUIT")) {
                    socket.close();
                    return;
                } else {
                    
                    if(line.equalsIgnoreCase("sp") && this.activeCount() == 3) {
                        os.writeBytes("1" + "\n\r"); 
                        os.flush();
                    }
                    
                    
                    os.writeBytes("Echo reply: " + line + "\n\r");
                    os.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }
    }
}


Maihoo
  • 31
  • 6
  • 1
    You don't communicate "between threads". You communicate between objects. Side issue: why are you even extending Thread? You almost never want to do to this. – Hovercraft Full Of Eels Jul 19 '20 at 01:46
  • So in what way should i change up this thing? Normaly i would pass the main class into the constructor, but of course it's static, so that won't work. I'm extending thread because i don't know how else to control the inStream and the outStream. Thanks for the advice though. EDIT: I'll be trying to implement runnable – Maihoo Jul 19 '20 at 03:05
  • You don't need to communicate between either threads or objects. You just need to write directly to the various `Sockets, with suitable synchronization. – user207421 Jul 19 '20 at 05:29
  • 1
    @MarquisofLorne: that seems an over-simplification, especially if the client is running a GUI. Usually, the data sent by the client to the server is done on the client's main thread, but reading from the server will require the use of a background thread and communication between objects since the client will comprise multiple objects (even if one is an anonymous inner class type object). – Hovercraft Full Of Eels Jul 19 '20 at 15:09
  • @Maihoo: your question is one of how to wire up multiple clients and a server while respecting threading, and there are many good examples of just such a situation on this and other sites, for example [this question](https://stackoverflow.com/questions/22732835/multiple-clients-access-the-server-concurrently?answertab=votes#tab-top) as well as [these questions](https://www.google.com/search?q=java+client+server+threading+game+site:stackoverflow.com). Also, not mentioned in my comment to Marquis is that the server will use helper objects to represent each client and they will run in threads – Hovercraft Full Of Eels Jul 19 '20 at 15:16
  • Thank you very much! I made the tcp threads implement runnable instead of extend thread so that already helped. Then i added a ConnectionManager between the main and the TCPThreads which isn't static. This way i can put the manager into the TCPThreads constructor and communicate between its objects. – Maihoo Jul 19 '20 at 20:34

1 Answers1

0

Thank you very much! I made the tcp threads implement runnable instead of extend thread. Then i added a ConnectionManager between the main and the TCPThreads which isn't static. This way i can put the manager into the TCPThreads constructor and communicate between its objects.

Maihoo
  • 31
  • 6