Reputation: 19
Till now, my server-client application was working perfectly. Then suddenly when I decided to run it(first the server then the client), the server window closed after a second of opening the client window. Now, only the client window was left open. However, the javaw.exe processes (there are 2 of them, server and client) are not stopped. They are hooked to the port number I used in the game. Therefore, I get the JVM_Bind exception when run again.
I terminated both process and ran the application(s) again, but the same problem arises. I even changed the port numbers (on both the projects of course) but it gave me the same result.
The server and the client keep sending and receiving things automatically like in any multiplayer game.
Client side :
private void tick() {
String info = handler.getPlayer(0).getX() + " " + handler.getPlayer(0).getY() + " " + handler.hb2.width + " " + handler.getPlayer(0).h1.x + " " + handler.getPlayer(0).h1.y + " " + handler.getPlayer(0).h2.x + " " + handler.getPlayer(0).h2.y + handler.getPlayer(0).h1.punch + " " + handler.getPlayer(0).h2.punch;
sendMessage(info);
String infoR = "";
try {
infoR = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Scanner s = new Scanner(infoR);
int a = 0;
while(s.hasNext()) {
String poop = s.next();
a++;
try {
switch(a) {
case 1:
handler.getPlayer(1).x = Integer.parseInt(poop);
break;
case 2:
handler.getPlayer(1).y = Integer.parseInt(poop);
break;
case 3:
handler.hb1.width = Integer.parseInt(poop);
break;
case 4:
handler.getPlayer(1).h1.x = Integer.parseInt(poop);
break;
case 5:
handler.getPlayer(1).h1.y = Integer.parseInt(poop);
break;
case 6:
handler.getPlayer(1).h2.x = Integer.parseInt(poop);
break;
case 7:
handler.getPlayer(1).h2.y = Integer.parseInt(poop);
break;
case 8:
handler.getPlayer(1).h1.punch = Boolean.getBoolean(poop);
break;
case 9:
handler.getPlayer(1).h2.punch = Boolean.getBoolean(poop);
break;
}
} catch(NumberFormatException e) {
frame.setVisible(false);
poop += " " + s.next();
if(poop.equals("you lose")) new ResultDisplay("red");
handler.hb1.resultDisplayed = true;
stop();
}
}
handler.tick();
}
public void sendMessage(String message) {
pw.println(message);
pw.flush();
}
private void init() {
try {
sock = new Socket("127.0.127.1", 1111);
ostream = sock.getOutputStream();
pw = new PrintWriter(ostream);
istream = sock.getInputStream();;
br = new BufferedReader(new InputStreamReader(istream));
} catch (IOException e) {
e.printStackTrace();
}
// more game initialization code follows
init()
is called first and then the tick()
method is called 60 times a second(ideally).
Server side:
private void tick() {
String info = handler.getPlayer(1).getX() + " " + handler.getPlayer(1).getY() + " " + handler.hb1.width + " " + handler.getPlayer(1).h1.x + " " + handler.getPlayer(1).h1.y + " " + handler.getPlayer(1).h2.x + " " + handler.getPlayer(1).h2.y + " " + handler.getPlayer(1).h1.punch + " " + handler.getPlayer(1).h2.punch;
String infoR = "";
try {
infoR = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Scanner s = new Scanner(infoR);
int a = 0;
while(s.hasNext()) {
a++;
String poop = s.next();
try {
switch(a) {
case 1:
handler.getPlayer(0).x = Integer.parseInt(poop);
break;
case 2:
handler.getPlayer(0).y = Integer.parseInt(poop);
break;
case 3:
handler.hb2.width = Integer.parseInt(poop);
break;
case 4:
handler.getPlayer(0).h1.x = Integer.parseInt(poop);
break;
case 5:
handler.getPlayer(0).h1.y = Integer.parseInt(poop);
break;
case 6:
handler.getPlayer(0).h2.x = Integer.parseInt(poop);
break;
case 7:
handler.getPlayer(0).h2.y = Integer.parseInt(poop);
break;
case 8:
handler.getPlayer(0).h1.punch = Boolean.getBoolean(poop);
break;
case 9:
handler.getPlayer(0).h2.punch = Boolean.getBoolean(poop);
break;
}
}catch(NumberFormatException e) {
frame.setVisible(false);
poop += " " + s.next();
if(poop.equals("you lose")) new ResultDisplay("green");
handler.hb2.resultDisplayed = true;
stop();
}
}
sendMessage(info);
handler.tick();
}
public void sendMessage(String message) {
pw.println(message);
pw.flush();
}
private void init() {
try {
sersock = new ServerSocket(1111);
sock = sersock.accept();
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
pw = new PrintWriter(sock.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
// more game initialization code follows
The main game thread calls both the init()
and tick()
are called by the main game threads in both the projects
Upvotes: 1
Views: 78
Reputation: 129
In your try and catch around the switch on your server you handle the exception by actually setting the visibility of your frame to false
and you don't print out the exception, thus explains why you don't get an exception.
}catch(NumberFormatException e) {
frame.setVisible(false); //<-- This bit
poop += " " + s.next();
if(poop.equals("you lose")) new ResultDisplay("green");
handler.hb2.resultDisplayed = true;
stop();
}
Of course you catch it for a reason, but maybe you should print out the exception it throws and not hide the frame? It could be throwing something you don't want it to throw.
Upvotes: 1