I did a Dots and Boxes game in java with Netbeans 5.5 I have a doubt because the game is between each two players, it's multiclient with sessions of two players.

Client 1 send the message with sal.println to the server and the server to Client 2 but this client only follows the path of the client 1 but it doesn't paint the lines that Client 1 is making.

The same happens when Client 2 is playing.

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.*;
import javax.swing.JOptionPane;

public class Cliente extends javax.swing.JFrame implements Runnable{

    CLabel etiq;
    Socket sCliente=null;
    PrintWriter salida=null;
    BufferedReader entrada=null;
    int cliente;
    Thread tarea=null;
    String nombre="";
    String msjServidor="";
    String cad="";
    String msjCliente="";
    int n,k,j,x,y,p,q;
    Graphics2D g2;
    int x1,y1,x2,y2,a,b,c,d;
    JTextArea TA;
    boolean turno1,turno2;


    /** Creates new form Cliente */
    public Cliente() {
        initComponents();

        setBounds(0,0,700,700);
        etiq=new CLabel();
        jPanel1.add(etiq);
        etiq.setBounds(5,5,650,550);

        turno1=true;
        turno2=false;


         try{
            TAMsj.append("\nCliente:Intentando la conexión con el servidor...");
            sCliente=new Socket("127.0.0.1",6001);
            TAMsj.append("\nCliente: Conexión aceptada...");

            salida=new PrintWriter(sCliente.getOutputStream(),true);
            entrada= new BufferedReader(new InputStreamReader(sCliente.getInputStream()));
            cliente=sCliente.getInputStream().read();
            tarea=new Thread(this);
            tarea.start();
        }catch(UnknownHostException e){
            TAMsj.append("\nCliente: No se pudo encontrar el servidor...");
        }catch(IOException e){
            TAMsj.append("\nCliente: No se pudo efectuar la conexión...");
        }
        nombre=(String)JOptionPane.showInputDialog(this,"Escribe tu nombre","Identificación del cliente",JOptionPane.PLAIN_MESSAGE,null,null,"Anónimo");
         setTitle("Timbiriche " + "Cliente: " + cliente + " " + nombre);


    }


    public void run(){
        try{
            while((msjServidor=entrada.readLine())!=null){


                  if(msjServidor.substring(0,2).equals("c:")){ 
                      TAMsj.append("\n" + msjServidor);
                      recibe(msjServidor);

                 }else{
                    n=nombre.length();     
                    TAMsj.append("\n" + msjServidor.substring(0,n+2) + msjServidor.substring(n+4));                    
                 }

                if(msjServidor.equals("Hasta pronto..."))
                    break;
            }
        }catch(IOException e){
            TAMsj.append("\nCliente: No se pudo efectuar la conexión...");
        }
    }

    public void recibe(String nn){

    j=nn.indexOf(",");
    a=Integer.valueOf(nn.substring(2,5));
    j=nn.indexOf(",");
    b=Integer.valueOf(nn.substring(6,9));
    p=nn.indexOf("*");
    q=nn.indexOf("=");
    c=Integer.valueOf(nn.substring(10,13));
    d=Integer.valueOf(nn.substring(14));

    if(cliente==1){

            Graphics g=etiq.getGraphics();
            g2=(Graphics2D)g;
            g2.setPaint(Color.RED);
            g2.setStroke(new BasicStroke(4.0f));
            g2.draw(new Line2D.Double(a,b,c,d));
            repaint();

    }else if(cliente==2){

            Graphics g=etiq.getGraphics();
            g2=(Graphics2D)g;
            g2.setPaint(Color.BLUE);
            g2.setStroke(new BasicStroke(4.0f));
            g2.draw(new Line2D.Double(a,b,c,d));
            repaint();

    }  

    }





 private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {                                     
// TODO add your handling code here:

        if(k==0){
            x1=evt.getX();
            y1=evt.getY();
            k++;
            }

            if(k==1){
            x2=evt.getX();
            y2=evt.getY();

            k=0;

            }


            Graphics g=etiq.getGraphics();
            g2=(Graphics2D)g;
            if(cliente==1){
            g2.setPaint(Color.BLUE);
            g2.setStroke(new BasicStroke(4.0f));
            g2.draw(new Line2D.Double(x1,y1,x2,y2));




            msjCliente="c:"+ x1 +"," + y1 + "*" + x2 + "=" + y2;

            salida.println(msjCliente);


            }else if(cliente==2){
            g2.setPaint(Color.RED);
            g2.setStroke(new BasicStroke(4.0f));
            g2.draw(new Line2D.Double(x1,y1,x2,y2));


            msjCliente="c:"+ x1 +"," + y1 + "*" + x2 + "=" + y2;

            salida.println(msjCliente);


            }

      //      g2.drawRect(x1,y1,5,30);  





    }                               



 private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:

        cad=jTextField1.getText();
         msjCliente="t:" + cad;
        if(msjCliente!=null)
            salida.println(nombre+": " + msjCliente);



    }

I want make the conections between Client 1 and Client 2 but Client 1 comunicate with all the Clients 2 and Client 2 comunicates with all the Clients 1. I tried to put in SetSalida only the name of the salida of the other client but it didn't work in this way, the only way was putting a vector with all the clients 1 and a vector with all the clients 2.

import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
public class ServidorD extends javax.swing.JFrame implements Runnable {

    ServerSocket sServidor = null;
    Socket sCliente1=null, sCliente2=null, sCliente3=null;
    BufferedReader entrada1, entrada2, entrada3;
    ConexionD con1, con2, con3;
    Vector salidas,sale1;
    PrintWriter sal,sal1;
    int juego=1;

    //PrintWriter salida1, salida2;

    Thread tarea;
    boolean escuchando=true;
    int cont=0;

    public ServidorD() {
        initComponents();
        setBounds(0,0,420,320);
        try {
            TAMsj.append("Creando el socket de servidor en el puerto 6001...\n");
            sServidor = new ServerSocket(6001);
            TAMsj.append(new Date() + "   Socket listo para recibir clientes.\n");
            tarea=new Thread(this);
            tarea.start();
        } catch (IOException e) {
            TAMsj.append("No se puede crear la conexión con el puerto: 6001.\n");
            System.exit(-1);
        }

        salidas =new Vector();
        sale1=new Vector();

    }

    public void run(){
        try{
            while(true){
                if(cont==0){
                    sCliente1=sServidor.accept();
                    TAMsj.append("\nServidor: Conexión del primer cliente aceptada.");
                    TAMsj.append("\nDireccion IP del jugador 1: " + sCliente1.getInetAddress().getHostAddress()+"\n");
                    TAMsj.append("Juego No." + juego);
                    sal=new PrintWriter(sCliente1.getOutputStream(),true);
                    salidas.add(sal);
                    sCliente1.getOutputStream().write(1);
                    entrada1=new BufferedReader(new InputStreamReader(sCliente1.getInputStream()));
                    con1=new ConexionD(entrada1,TAMsj);
                    cont=1;
                } else if(cont==1){
                    sCliente2=sServidor.accept();
                    TAMsj.append("\nServidor: Conexión del segundo cliente aceptada.");
                    TAMsj.append("\nDireccion IP del jugador 1: " + sCliente1.getInetAddress().getHostAddress()+"\n");
                    TAMsj.append("Juego No." + juego);
                    sal1=new PrintWriter(sCliente2.getOutputStream(),true);
                    sale1.add(sal1);
                    sCliente2.getOutputStream().write(2);
                    entrada2=new BufferedReader(new InputStreamReader(sCliente2.getInputStream()));
                    con2=new ConexionD(entrada2,TAMsj);
                    //con1.setSalida(salida2);
                    //con2.setSalida(salida1);

                    con1.setSalidas(sale1);
                    con2.setSalidas(salidas);

                    con1.start();
                    con2.start();
                    juego++;
                    cont=0;
                }
            }
        }catch(IOException e){
            TAMsj.append("Error de E/S.");
        }

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.