0

I've got a server, a client and a JFrame, and I'm insecure about how to go around making the server obtain my Arraypoints, as well as making a client (preferably on another device), receive these arraypoints. I've been stuck on this issue for quite a while, so any thoughts or speculations are MUCH appreciated. I've failed to find any project similar to what I'm trying to do here on this forum, so I'm stuck.

Clientside:

public class TCPClient {

    public static void main(String argv[]) throws Exception {

        ConnectionFrame CF = new ConnectionFrame();

        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        Socket clientSocket = new Socket("localhost", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
        clientSocket.close();
    }
}

Serverside:

public class TCPServer {

    public static final int SERVERPORT = 6789;  

    public static void main(String argv[]) throws Exception {
        String clientSentence;
        String capitalizedSentence;
        ServerSocket welcomeSocket = new ServerSocket(SERVERPORT);

        CanvasFrame cf = new CanvasFrame();

        while(true) {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);        
        }
    }
}

My canvas(Jframe):

public class CanvasFrame extends JPanel {

    ArrayList<Point> location = new ArrayList<Point>(); 

    JTextArea consoleOutput = new JTextArea(1,20);

    public void addComponentToPane(Container pane) {
        consoleOutput.setEditable(false);
    }

    public CanvasFrame() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                location.clear();
                location.add(e.getPoint());
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                location.add(e.getPoint());
                repaint();
            }
        });
        setPreferredSize(new Dimension(800, 500));
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(location.isEmpty()){
            return;
        }

         Point p = location.get(0);
         for (int i = 1; i < location.size(); i++) {
             Point q = location.get(i);
             g.drawLine(p.x, p.y, q.x, q.y);
             p = q;
         }
    }

    public static void main(String[] args) throws Exception {

        InetAddress SERVERIP = InetAddress.getLocalHost();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {

                JFrame frame = new JFrame("Drawing with friends");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new CanvasFrame(), BorderLayout.CENTER);

                JTextArea IPadress = new JTextArea(1,20);
                IPadress.setEditable(false);
                IPadress.append("DEVICE IP: " + SERVERIP.getHostAddress());
                frame.add(IPadress, BorderLayout.SOUTH);

                frame.setSize(new Dimension(800,600));
                frame.setLocationRelativeTo(null);
                frame.setResizable(false);
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}
WONDERGG
  • 125
  • 1
  • 2
  • 10
  • Please have a look at my [previous posts](http://stackoverflow.com/questions/22732835/multiple-clients-access-the-server-concurrently/22736352#22736352) that will help you to design a client-server chat program. – Braj Apr 15 '16 at 10:50
  • Whenever you send something you have to `flush()` the streams before closing the connection, otherwise you can't be sure that the data has really been sent. – Robert Apr 15 '16 at 10:53
  • @Braj I've read through your post and I kind of get the idea, but I'll need a more thorough answer for my post. I've been looking at networking a lot the past few days, and as I continue to fail to understand it, it feels like I'm just staring blind at the moment. – WONDERGG Apr 15 '16 at 11:11
  • Divide and conquer ... 1. Try to successfully connect. 2. Try to send anything - like a byte simply, 3. Move on to more complex things to send ... Your understanding will get bigger bit by bit. Trying to swallow it all at once will suffocate you - and would probably most people. – Fildor Apr 15 '16 at 11:22

0 Answers0