0

for an exam i have to make a synchronous multiplayergame, to practice with the serverside, i decided to make a little sketch in processing where two clients exchange the mouse's positions.

The idea is when two client are connected to the server, the server spawn a thread with the two connections, in the thread client send his data to server and server turn the data to the client, so the server is a simple bridge for the clients.

This is the Java server code:

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ProvaServerGioco {
    public final static int PORT = 31946;
    public final static int MAXPARTITE = 50;
    private Socket player1;
    private Socket player2;
    public final static char separatore = '\n';
    public static final String INDIRIZZOHAMACHI = "25.166.111.50";
    public static final String INDIRIZZOLAN = "192.168.1.65";

    public void start() throws UnknownHostException {
        ExecutorService pool = Executors.newFixedThreadPool(MAXPARTITE);
        StringBuilder mouse=new StringBuilder();
        InetAddress addr = InetAddress.getByName(INDIRIZZOHAMACHI);
        try (ServerSocket server = new ServerSocket(PORT, 0, addr)) {
            while (true) {
                {
                    if (player1 == null) {
                        try {
                            player1 = server.accept();
                            System.out.println("Ryu");
                            Writer out = new OutputStreamWriter(
                                    player1.getOutputStream());
                            out.write("in attesa di giocatore 2" + separatore);
                            out.flush();
                        } catch (IOException ex) {
                            player1.close();
                            player1 = null;
                        }
                    }
                    if (player2 == null) {
                        try {
                            player2 = server.accept();
                            System.out.println("Ken");
                            Writer out = new OutputStreamWriter(
                                    player2.getOutputStream());
                            out.write("in attesa di giocatore 2" + separatore);
                            out.flush();
                        } catch (IOException ex) {
                            player2.close();
                            player2 = null;
                        }
                    }

                    if(!checkConnessione(player1))
                        player1=null;
                    if(!checkConnessione(player2))
                        player2=null;


                    if (player2 != null && player1 != null) {

                        pool.submit(new ThreadPartita(player1, player2));
                        player1 = null;
                        player2 = null;
                    }

                }

            }
        } catch (IOException ex) {
            System.err.println(ex);

        }

    }

    private boolean checkConnessione(Socket player) {
        if (player != null)
            try {
                Writer out = new OutputStreamWriter(player.getOutputStream());
                out.write(separatore);
                out.flush();
                return true;
            } catch (IOException ex) {
                try {
                    player.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return false;

            }
        return false;
    }

    public static void main(String[] args) {
        ProvaServerGioco server = new ProvaServerGioco();
        try {
            server.start();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

}

And this is the thread for the match:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.util.concurrent.Callable;


public class ThreadPartita implements Callable<Void>{

    private Socket player1;
    private Socket player2;
    private Writer player1Out;
    private Writer player2Out;
    private BufferedReader readerPlayer1;
    private BufferedReader readerPlayer2;
    private boolean finePartita=false;
    public ThreadPartita(Socket player1,Socket player2)
    {
        this.player1=player1;
        this.player2=player2;
    }
    @Override
    public Void call() throws Exception {
        notificaConnessione(player1);
        notificaConnessione(player2);
        player1Out=new OutputStreamWriter(
                player1.getOutputStream());
        player2Out=new OutputStreamWriter(
                player2.getOutputStream());
        readerPlayer1=new BufferedReader(new InputStreamReader(player1.getInputStream(), "ASCII"));
        readerPlayer2=new BufferedReader(new InputStreamReader(player2.getInputStream(), "ASCII"));
        String mousePlayer1="";
        String mousePlayer2="";

        while(true&&!finePartita)
        {
            mousePlayer2=readerPlayer2.readLine();
            mousePlayer1=readerPlayer1.readLine();
            player1Out.write(mousePlayer2+ProvaServerGioco.separatore);
            player1Out.flush();
            System.out.println(mousePlayer1);
            System.out.println(readerPlayer2.ready());


            player2Out.write(mousePlayer1+ProvaServerGioco.separatore);

            player2Out.flush();
        }


        return null;
    }


    private void inviaMessaggio(Socket player,String messaggio) 
    {
        try{
        Writer out = new OutputStreamWriter(
                player.getOutputStream());

        out.write(messaggio);
        out.flush();
        }
        catch(IOException ex)
        {
            System.out.println("partita annullata");
        }
    }


        private void notificaConnessione(Socket player)
        {
            inviaMessaggio(player,"Sei connesso"+ProvaServerGioco.separatore);
        }
}

The problem in this code is when i read the data from the player2 in ThreadPartita:

mousePlayer2=readerPlayer2.readLine();

In this line the program freezes, if i comment that line everything is working and player1 send his data to player2 correctly.

I know for sure that is a noob error, but i don't know how to fix it cause i'm a beginner with server socket (i'am studyng the book "java network programming" from o'Reilly in this days).

I don't know if can be useful, but this is the code in processing for the clientside:

import java.net.*;
import java.io.*;
import java.io.Writer;
StringBuilder mouse;
final static int PORTA=31946;
//final static int PORTA=13;
String mouseStringa;
String hamachiIP="127.0.0.1";
Socket socket;
Writer out;
BufferedReader reader;
void setup()
{

  size(800,800);
  try{
  socket = new Socket(hamachiIP, PORTA);
  socket.setSoTimeout(15000);
  out=new OutputStreamWriter(
        socket.getOutputStream());
  InputStream in = socket.getInputStream();
  mouse = new StringBuilder();
  reader = new BufferedReader(new InputStreamReader(in, "ASCII"));
  mouseStringa=reader.readLine();
  }
  catch (IOException ex) 
  {
      System.err.println(ex);
  }
  }

  void draw()
  {
    background(255);

println(mouseStringa);

try{
  mouse = new StringBuilder();
if(reader.ready())
{
  String appoggio=reader.readLine();
  if(appoggio.length()>0)
      mouseStringa=reader.readLine();
 }
  String posizioniMouse=""+mouseX+';'+mouseY+'\n';
  out.write(posizioniMouse);
  out.flush();
  }
  catch (IOException ex) 
  {
      System.err.println(ex);
  }  

    frame.setTitle("fps: "+frameRate);

    }

Thank you for your attention!

user207421
  • 305,947
  • 44
  • 307
  • 483
Jaeger
  • 331
  • 1
  • 2
  • 11
  • I am confident that player2 does not transmit its moseposition, so that `readLine` blocks forever becase there is nothing to read. But I cannot test this, because your client code is incomplete. Please update the client code, so I can test it. – Soana Aug 22 '14 at 09:39
  • The client code is complete, maybe you thin not cause is made in processing: https://www.processing.org/ – Jaeger Aug 22 '14 at 10:42

0 Answers0