Issue with MouseListener

Thread Solved

Join Date: Nov 2009
Posts: 12
Reputation: chandini.david is an unknown quantity at this point 
Solved Threads: 0
chandini.david chandini.david is offline Offline
Newbie Poster

Issue with MouseListener

 
0
  #1
24 Days Ago
Hey!!

I am trying to make a 2-player game; where one player plays after the next. I have used socket programming in the code; so that, when the client (player 1) clicks on the window, the co-ordinates are sent to the server (player 2) and vice-versa. (For now, both player screen are displayed on the same host.)

So, here's the issue: Suppose I click somewhere on the client's screen - lets call it click 1. The co-ordinates of click 1 are correctly sent to the server. Now suppose I click again on the client's screen (call it click 2) - ideally nothing should happen since it's not player 1's turn - and for now, it doesn't. So far, so good. Now, suppose I click on the server's screen (call it click 3) - click 3's co-ordinates are sent to the client correctly. However, now, without clicking anywhere at all, the client sends click 2's co-ordinates to the server!! I don't want the last part to happen - i.e. when it's not someone's turn to play, all the mouse clicks on that screen should be ignored.

I can't figure this out!! I am using removeMouseListener right after a click is done (provided its the right person's turn), and I am not calling the addMouseListener until after co-ordinates are received. So, why is this happening?!! I am not very good at jav coding, so even if it is a very dumb error, please be patient and let me know!! I've already spent hours trying to debug this, to no avail!!

Thanks,
Chand
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: chandini.david is an unknown quantity at this point 
Solved Threads: 0
chandini.david chandini.david is offline Offline
Newbie Poster

The Code

 
0
  #2
24 Days Ago
Here's the Code:

  1. package game;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. import javax.swing.*;
  9.  
  10. public class Clientms2 extends JFrame implements MouseListener
  11. {
  12. private static final long serialVersionUID = 1L;
  13.  
  14. JFrame container;
  15. static InetAddress address;
  16. static Socket s;
  17. static String host = "localhost";
  18. static BufferedOutputStream bos;
  19. static OutputStreamWriter osw;
  20. static BufferedInputStream bis;
  21. static InputStreamReader isr;
  22. static int port = 5656;
  23. static String message;
  24. static boolean ClientTurn_flag = true;
  25. //static MouseListener l;
  26.  
  27. static int tempx, tempy;
  28. static int mouseX, mouseY;
  29. static StringBuffer x, y;
  30.  
  31. public Clientms2()
  32. {
  33. super("Client 1: Player 1");
  34. setSize(800, 600);
  35. setDefaultCloseOperation(EXIT_ON_CLOSE);
  36. this.setResizable(false);
  37. addMouseListener(this);
  38. }
  39.  
  40. public static void main(String args[])
  41. {
  42. Clientms2 obj = new Clientms2();
  43. obj.setVisible(true);
  44. }
  45.  
  46. @Override
  47. public void mouseClicked(MouseEvent e)
  48. {
  49. if (ClientTurn_flag)
  50. {
  51. mouseX = e.getX();
  52. mouseY = e.getY();
  53. removeMouseListener(this);
  54.  
  55. try
  56. {
  57. address = InetAddress.getByName(host);
  58. s = new Socket(address, port);
  59.  
  60. } catch (IOException e1) { e1.printStackTrace(); }
  61.  
  62. System.out.println("CLIENT SENDS: (" + mouseX + ", " + mouseY + ")");
  63. send(mouseX, mouseY);
  64. receive();
  65.  
  66. addMouseListener(this);
  67. }
  68.  
  69. else
  70. {
  71. //do nothing: It is not the Client's turn to play
  72. }
  73.  
  74. //ClientTurn_flag = !ClientTurn_flag;
  75. }
  76.  
  77. public void receive()
  78. {
  79. if (ClientTurn_flag == false)
  80. {
  81. try
  82. {
  83. System.out.println("Client waiting to receive...");
  84.  
  85. do
  86. {
  87. bis = new BufferedInputStream(s.getInputStream());
  88. isr = new InputStreamReader(bis, "US-ASCII");
  89. } while(isr.ready() == true);
  90.  
  91. x = new StringBuffer();
  92. y = new StringBuffer();
  93.  
  94. while((tempx = isr.read()) != 13)
  95. x.append((char) tempx);
  96. String str = x.toString();
  97. mouseX = Integer.parseInt(str);
  98.  
  99. while((tempy = isr.read()) != 13)
  100. y.append((char) tempy);
  101. str = y.toString();
  102. mouseY = Integer.parseInt(str);
  103.  
  104. System.out.println("CLIENT CONSOLE, SERVER CLICKED: (" + mouseX + ", " + mouseY + ")");
  105.  
  106. } catch (IOException e) { }
  107.  
  108. ClientTurn_flag = true;
  109. System.out.println("Receive() -> C's Flag: " + ClientTurn_flag);
  110. System.out.println("Waiting for Client's Mouse Click...");
  111.  
  112. }
  113.  
  114. }
  115.  
  116. private void send(int xcor, int ycor)
  117. {
  118. try
  119. {
  120. bos = new BufferedOutputStream(s.getOutputStream());
  121. osw = new OutputStreamWriter(bos, "US-ASCII");
  122. message = (Integer.toString(xcor)) + (char) 13;
  123. osw.write(message);
  124. osw.flush();
  125. message = (Integer.toString(ycor)) + (char) 13;
  126. osw.write(message);
  127. osw.flush();
  128. } catch (IOException e) { e.printStackTrace(); }
  129.  
  130. ClientTurn_flag = false;
  131.  
  132. System.out.println("Send() -> C's Flag: " + ClientTurn_flag);
  133. }
  134.  
  135.  
  136. @Override
  137. public void mouseEntered(MouseEvent e) {
  138. // TODO Auto-generated method stub
  139.  
  140. }
  141.  
  142. @Override
  143. public void mouseExited(MouseEvent e) {
  144. // TODO Auto-generated method stub
  145.  
  146. }
  147.  
  148. @Override
  149. public void mousePressed(MouseEvent e) {
  150. // TODO Auto-generated method stub
  151.  
  152. }
  153.  
  154. @Override
  155. public void mouseReleased(MouseEvent e) {
  156. // TODO Auto-generated method stub
  157.  
  158. }
  159. }
Last edited by chandini.david; 24 Days Ago at 5:50 pm. Reason: Cleaned up the code
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #3
24 Days Ago
Originally Posted by chandini.david View Post
I can't figure this out!! I am using removeMouseListener right after a click is done (provided its the right person's turn)
I would say that this is a logical error. It might work if done properly, but in my opinion, the proper way to do this would be to keep both client and server aware of whose turn it is at all times. If the client clicks when it isn't their turn, don't tell the server about it. If the server clicks when it isn't their turn, don't tell the client about it. And on both client and server, if it is their turn and the other player sends them "click coordinates", then ignore it, but throw an Exception (or display an error message) because it is an error. (Example: if it's the server's turn but the client sends them click coordinates, then it's obviously an error). So what I'm saying is that you shouldn't remove the mouse listeners - instead, implement "talking about whose turn it is" between client and server, and do so such that they always know whose turn it is.

With that said, I'll try to help you debug the code you currently have, but I can't promise that I'll be able to fix whatever it is.
Last edited by BestJewSinceJC; 24 Days Ago at 9:06 pm.
Out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
1
  #4
24 Days Ago
I looked through your code, and I don't see a reason why you should be continuously adding and removing the mouse listener. If there is no reason to do so, don't do it. (I.e. it adds nothing to your program's logic, so what use does it have? Your ClientTurn_flag takes care of whose turn it is; you don't need to remove the listener). Other than that I changed the placement of one of your statements, it will likely do nothing though . . If you post the Server's code as well, I will run it and I'll help you debug it.

  1. package game;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. import javax.swing.*;
  9.  
  10. public class Clientms2 extends JFrame implements MouseListener
  11. {
  12. private static final long serialVersionUID = 1L;
  13.  
  14. JFrame container;
  15. static InetAddress address;
  16. static Socket s;
  17. static String host = "localhost";
  18. static BufferedOutputStream bos;
  19. static OutputStreamWriter osw;
  20. static BufferedInputStream bis;
  21. static InputStreamReader isr;
  22. static int port = 5656;
  23. static String message;
  24. static boolean ClientTurn_flag = true;
  25. //static MouseListener l;
  26.  
  27. static int tempx, tempy;
  28. static int mouseX, mouseY;
  29. static StringBuffer x, y;
  30.  
  31. public Clientms2()
  32. {
  33. super("Client 1: Player 1");
  34. setSize(800, 600);
  35. setDefaultCloseOperation(EXIT_ON_CLOSE);
  36. this.setResizable(false);
  37. addMouseListener(this);
  38. }
  39.  
  40. public static void main(String args[])
  41. {
  42. Clientms2 client = new Clientms2();
  43. client.setVisible(true);
  44. }
  45.  
  46. @Override
  47. public void mouseClicked(MouseEvent e)
  48. {
  49. if (ClientTurn_flag)
  50. {
  51. mouseX = e.getX();
  52. mouseY = e.getY();
  53. removeMouseListener(this);
  54.  
  55. try
  56. {
  57. address = InetAddress.getByName(host);
  58. s = new Socket(address, port);
  59.  
  60. } catch (IOException e1) { e1.printStackTrace(); }
  61.  
  62. System.out.println("CLIENT SENDS: (" + mouseX + ", " + mouseY + ")");
  63. send(mouseX, mouseY);
  64.  
  65. //try this
  66. ClientTurn_flag = false;
  67. receive();
  68.  
  69. addMouseListener(this);
  70. }
  71.  
  72. else
  73. {
  74. //do nothing: It is not the Client's turn to play
  75. }
  76.  
  77. //ClientTurn_flag = !ClientTurn_flag;
  78. }
  79.  
  80. public void receive()
  81. {
  82. if (ClientTurn_flag == false)
  83. {
  84. try
  85. {
  86. System.out.println("Client waiting to receive...");
  87.  
  88. do
  89. {
  90. bis = new BufferedInputStream(s.getInputStream());
  91. isr = new InputStreamReader(bis, "US-ASCII");
  92. } while(isr.ready() == true);
  93.  
  94. x = new StringBuffer();
  95. y = new StringBuffer();
  96.  
  97. while((tempx = isr.read()) != 13)
  98. x.append((char) tempx);
  99. String str = x.toString();
  100. mouseX = Integer.parseInt(str);
  101.  
  102. while((tempy = isr.read()) != 13)
  103. y.append((char) tempy);
  104. str = y.toString();
  105. mouseY = Integer.parseInt(str);
  106.  
  107. System.out.println("CLIENT CONSOLE, SERVER CLICKED: (" + mouseX + ", " + mouseY + ")");
  108.  
  109. } catch (IOException e) { }
  110.  
  111. ClientTurn_flag = true;
  112. System.out.println("Receive() -> C's Flag: " + ClientTurn_flag);
  113. System.out.println("Waiting for Client's Mouse Click...");
  114.  
  115. }
  116.  
  117. }
  118.  
  119. private void send(int xcor, int ycor)
  120. {
  121. try
  122. {
  123. bos = new BufferedOutputStream(s.getOutputStream());
  124. osw = new OutputStreamWriter(bos, "US-ASCII");
  125. message = (Integer.toString(xcor)) + (char) 13;
  126. osw.write(message);
  127. osw.flush();
  128. message = (Integer.toString(ycor)) + (char) 13;
  129. osw.write(message);
  130. osw.flush();
  131. } catch (IOException e) { e.printStackTrace(); }
  132.  
  133.  
  134. }
  135.  
  136.  
  137. @Override
  138. public void mouseEntered(MouseEvent e) {
  139. // TODO Auto-generated method stub
  140.  
  141. }
  142.  
  143. @Override
  144. public void mouseExited(MouseEvent e) {
  145. // TODO Auto-generated method stub
  146.  
  147. }
  148.  
  149. @Override
  150. public void mousePressed(MouseEvent e) {
  151. // TODO Auto-generated method stub
  152.  
  153. }
  154.  
  155. @Override
  156. public void mouseReleased(MouseEvent e) {
  157. // TODO Auto-generated method stub
  158.  
  159. }
  160. }
Out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: chandini.david is an unknown quantity at this point 
Solved Threads: 0
chandini.david chandini.david is offline Offline
Newbie Poster
 
0
  #5
24 Days Ago
Heyy! Thanks a lot for taking the time to go through it!!

Here's the server side code:

  1. package game;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.io.*;
  7. import java.net.*;
  8.  
  9. import javax.swing.*;
  10.  
  11. public class Serverms2 extends JFrame implements MouseListener
  12. {
  13. private static final long serialVersionUID = 1L;
  14.  
  15. JFrame container;
  16. static Socket s;
  17. static ServerSocket ss;
  18. static BufferedOutputStream bos;
  19. static OutputStreamWriter osw;
  20. static BufferedInputStream bis;
  21. static InputStreamReader isr;
  22. static StringBuffer x, y;
  23. static int port = 5656;
  24. static int ID, count = 0;
  25. static int tempx, tempy;
  26. static int mouseX, mouseY;
  27. static String message;
  28. static boolean ServerTurn_flag = false;
  29.  
  30. public Serverms2()
  31. {
  32. super("Server: Player 2");
  33. setSize(800, 600);
  34. setDefaultCloseOperation(EXIT_ON_CLOSE);
  35. this.setResizable(false);
  36. addMouseListener(this);
  37. }
  38.  
  39.  
  40. public static void main(String args[])
  41. {
  42. Serverms2 obj = new Serverms2();
  43. obj.setVisible(true);
  44.  
  45. try
  46. {
  47. ss = new ServerSocket(port);
  48. } catch (IOException e1) { e1.printStackTrace(); }
  49.  
  50. if(ServerTurn_flag == false)
  51. {
  52. try
  53. {
  54. s = ss.accept();
  55.  
  56. bis = new BufferedInputStream(s.getInputStream());
  57. isr = new InputStreamReader(bis, "US-ASCII");
  58.  
  59. x = new StringBuffer();
  60. y = new StringBuffer();
  61.  
  62. while((tempx = isr.read()) != 13)
  63. x.append((char) tempx);
  64.  
  65. String str = x.toString();
  66. mouseX = Integer.parseInt(str);
  67.  
  68. while((tempy = isr.read()) != 13)
  69. y.append((char) tempy);
  70. str = y.toString();
  71. mouseY = Integer.parseInt(str);
  72.  
  73. System.out.println("SERVER CONSOLE, CLIENT CLICKED: (" + mouseX + ", " + mouseY + ")");
  74. } catch (IOException e) { e.printStackTrace(); }
  75.  
  76. ServerTurn_flag = true;
  77. }
  78. }
  79.  
  80. @Override
  81. public void mouseClicked(MouseEvent e) {
  82.  
  83. if(ServerTurn_flag)
  84. {
  85. mouseX = e.getX();
  86. mouseY = e.getY();
  87. removeMouseListener(this);
  88. System.out.println("SERVER SENDS: (" + mouseX + ", " + mouseY + ")");
  89. send(mouseX, mouseY);
  90. receive();
  91. addMouseListener(this);
  92. }
  93.  
  94. else
  95. {
  96. //do nothing: It is not the Server's turn to play
  97. }
  98. }
  99.  
  100. public void receive() {
  101.  
  102. try
  103. {
  104. if (ServerTurn_flag == false)
  105. {
  106. System.out.println("Server waiting to receive...");
  107. s = ss.accept();
  108.  
  109. bis = new BufferedInputStream(s.getInputStream());
  110. isr = new InputStreamReader(bis, "US-ASCII");
  111.  
  112. x = new StringBuffer();
  113. y = new StringBuffer();
  114.  
  115. while((tempx = isr.read()) != 13)
  116. x.append((char) tempx);
  117.  
  118. String str = x.toString();
  119. mouseX = Integer.parseInt(str);
  120.  
  121. while((tempy = isr.read()) != 13)
  122. y.append((char) tempy);
  123. str = y.toString();
  124. mouseY = Integer.parseInt(str);
  125.  
  126. System.out.println("SERVER CONSOLE, CLIENT CLICKED: (" + mouseX + ", " + mouseY + ")");
  127. ServerTurn_flag = true;
  128. }
  129. } catch (IOException e) { }
  130.  
  131. System.out.println("Receive() -> Serv's Flag: " + ServerTurn_flag);
  132. System.out.println("Waiting for Server's Mouse Click...");
  133. }
  134.  
  135. private void send(int xcor, int ycor)
  136. {
  137. try
  138. {
  139. bos = new BufferedOutputStream(s.getOutputStream());
  140. osw = new OutputStreamWriter(bos, "US-ASCII");
  141. message = (Integer.toString(xcor)) + (char) 13;
  142. osw.write(message);
  143. osw.flush();
  144. //System.out.println(message);
  145. message = (Integer.toString(ycor)) + (char) 13;
  146. osw.write(message);
  147. osw.flush();
  148. bos.flush();
  149. //System.out.println(message);
  150.  
  151. } catch (IOException e) { e.printStackTrace(); }
  152.  
  153. ServerTurn_flag = false;
  154. System.out.println("Send() -> Serv's Flag: " + ServerTurn_flag);
  155. }
  156.  
  157. @Override
  158. public void mouseEntered(MouseEvent e) {
  159. // TODO Auto-generated method stub
  160.  
  161. }
  162.  
  163. @Override
  164. public void mouseExited(MouseEvent e) {
  165. // TODO Auto-generated method stub
  166.  
  167. }
  168.  
  169. @Override
  170. public void mousePressed(MouseEvent e) {
  171. // TODO Auto-generated method stub
  172.  
  173. }
  174.  
  175. @Override
  176. public void mouseReleased(MouseEvent e) {
  177. // TODO Auto-generated method stub
  178.  
  179. }
  180.  
  181. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #6
24 Days Ago
With the modification I made earlier, your program seems like it works. . so if you take the code I posted earlier and update the code on your machine with it, your program should run correctly.
Last edited by BestJewSinceJC; 24 Days Ago at 10:17 pm.
Out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: chandini.david is an unknown quantity at this point 
Solved Threads: 0
chandini.david chandini.david is offline Offline
Newbie Poster
 
0
  #7
24 Days Ago
Hey!! Thanks for trying, but it still doesn't work. It still reads and sends the co-ordinates of clicks that are performed when not that player's turn...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #8
24 Days Ago
I found the problem. Your code is way more complex than it needs to be. Essentially all you *need* in the client is the "clientTurnFlag", the send method, and the receive method. Initially in your main method you don't do anything except for opening the client's window and setting it visible. Your problem was basically setting the flags to false at the incorrect times. Here is your (much updated) code. I removed a lot of unnecessary code. There are logically only a few places where you should be setting your flags to true and false. Btw, you only need to accept the socket once - you shouldn't be accepting the same connection over and over again on your ServerSocket.

  1. package game;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. import javax.swing.*;
  9.  
  10. public class Clientms2 extends JFrame implements MouseListener
  11. {
  12. private static final long serialVersionUID = 1L;
  13.  
  14. JFrame container;
  15. static InetAddress address;
  16. static Socket s;
  17. static String host = "localhost";
  18. static BufferedOutputStream bos;
  19. static OutputStreamWriter osw;
  20. static BufferedInputStream bis;
  21. static InputStreamReader isr;
  22. static int port = 5656;
  23. static String message;
  24. static boolean ClientTurn_flag = true;
  25. //static MouseListener l;
  26.  
  27. static int tempx, tempy;
  28. static int mouseX, mouseY;
  29. static StringBuffer x, y;
  30.  
  31. public Clientms2()
  32. {
  33. super("Client 1: Player 1");
  34. setSize(400, 400);
  35. setDefaultCloseOperation(EXIT_ON_CLOSE);
  36. this.setResizable(false);
  37. addMouseListener(this);
  38. try {
  39. address = InetAddress.getByName(host);
  40. s = new Socket(address, port);
  41. } catch(Exception e){System.out.println("You fail at socket programs sucka!!!");}
  42. }
  43.  
  44. public static void main(String args[])
  45. {
  46. Clientms2 client = new Clientms2();
  47. client.setVisible(true);
  48.  
  49.  
  50. }
  51.  
  52. @Override
  53. public void mouseClicked(MouseEvent e)
  54. {
  55. if (ClientTurn_flag)
  56. {
  57. mouseX = e.getX();
  58. mouseY = e.getY();
  59. send(mouseX, mouseY);
  60. ClientTurn_flag = false;
  61. System.out.println("End of client's turn, client waiting to receive.");
  62. receive();
  63. }
  64.  
  65. }
  66.  
  67. public void receive()
  68. {
  69. try
  70. {
  71. do
  72. {
  73. bis = new BufferedInputStream(s.getInputStream());
  74. isr = new InputStreamReader(bis, "US-ASCII");
  75. } while(isr.ready() == true);
  76. x = new StringBuffer();
  77. y = new StringBuffer();
  78. while((tempx = isr.read()) != 13)
  79. x.append((char) tempx);
  80. String str = x.toString();
  81. mouseX = Integer.parseInt(str);
  82.  
  83. while((tempy = isr.read()) != 13)
  84. y.append((char) tempy);
  85. str = y.toString();
  86. mouseY = Integer.parseInt(str);
  87.  
  88. } catch (IOException e) { }
  89.  
  90. ClientTurn_flag = true;
  91.  
  92. }
  93.  
  94.  
  95. private void send(int xcor, int ycor)
  96. {
  97. try
  98. {
  99. bos = new BufferedOutputStream(s.getOutputStream());
  100. osw = new OutputStreamWriter(bos, "US-ASCII");
  101. message = (Integer.toString(xcor)) + (char) 13;
  102. osw.write(message);
  103. osw.flush();
  104. message = (Integer.toString(ycor)) + (char) 13;
  105. osw.write(message);
  106. osw.flush();
  107. } catch (IOException e) { e.printStackTrace(); }
  108.  
  109.  
  110. }
  111.  
  112.  
  113. @Override
  114. public void mouseEntered(MouseEvent e) {
  115. // TODO Auto-generated method stub
  116.  
  117. }
  118.  
  119. @Override
  120. public void mouseExited(MouseEvent e) {
  121. // TODO Auto-generated method stub
  122.  
  123. }
  124.  
  125. @Override
  126. public void mousePressed(MouseEvent e) {
  127. // TODO Auto-generated method stub
  128.  
  129. }
  130.  
  131. @Override
  132. public void mouseReleased(MouseEvent e) {
  133. // TODO Auto-generated method stub
  134.  
  135. }
  136. }


And server:
  1. package game;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import java.io.*;
  7. import java.net.*;
  8.  
  9. import javax.swing.*;
  10.  
  11. public class Serverms2 extends JFrame implements MouseListener
  12. {
  13. private static final long serialVersionUID = 1L;
  14.  
  15. JFrame container;
  16. static Socket s;
  17. static ServerSocket ss;
  18. static BufferedOutputStream bos;
  19. static OutputStreamWriter osw;
  20. static BufferedInputStream bis;
  21. static InputStreamReader isr;
  22. static StringBuffer x, y;
  23. static int port = 5656;
  24. static int ID, count = 0;
  25. static int tempx, tempy;
  26. static int mouseX, mouseY;
  27. static String message;
  28. static boolean ServerTurn_flag = false;
  29.  
  30. public Serverms2()
  31. {
  32. super("Server: Player 2");
  33. setSize(400, 400);
  34. setDefaultCloseOperation(EXIT_ON_CLOSE);
  35. this.setResizable(false);
  36. addMouseListener(this);
  37. try {
  38. ss = new ServerSocket(port);
  39. } catch (IOException e1) { e1.printStackTrace(); }
  40.  
  41. }
  42.  
  43.  
  44. public static void main(String args[])
  45. {
  46. Serverms2 server = new Serverms2();
  47. server.setVisible(true);
  48. try {
  49. s = ss.accept();
  50. } catch (IOException e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. }
  54.  
  55. server.receive();
  56.  
  57. }
  58.  
  59. @Override
  60. public void mouseClicked(MouseEvent e) {
  61.  
  62. if(ServerTurn_flag)
  63. {
  64. mouseX = e.getX();
  65. mouseY = e.getY();
  66. //System.out.println("SERVER SENDS: (" + mouseX + ", " + mouseY + ")");
  67. send(mouseX, mouseY);
  68. System.out.println("End of server's turn, server waiting to receive.");
  69. ServerTurn_flag = false;
  70. receive();
  71. }
  72.  
  73. }
  74.  
  75. public void receive() {
  76.  
  77. try
  78. {
  79. bis = new BufferedInputStream(s.getInputStream());
  80. isr = new InputStreamReader(bis, "US-ASCII");
  81.  
  82. x = new StringBuffer();
  83. y = new StringBuffer();
  84.  
  85. while((tempx = isr.read()) != 13)
  86. x.append((char) tempx);
  87.  
  88. String str = x.toString();
  89. mouseX = Integer.parseInt(str);
  90.  
  91. while((tempy = isr.read()) != 13)
  92. y.append((char) tempy);
  93. str = y.toString();
  94. mouseY = Integer.parseInt(str);
  95.  
  96. } catch (IOException e) { }
  97.  
  98. ServerTurn_flag = true;
  99.  
  100. }
  101.  
  102. private void send(int xcor, int ycor)
  103. {
  104. try
  105. {
  106. bos = new BufferedOutputStream(s.getOutputStream());
  107. osw = new OutputStreamWriter(bos, "US-ASCII");
  108. message = (Integer.toString(xcor)) + (char) 13;
  109. osw.write(message);
  110. osw.flush();
  111. //System.out.println(message);
  112. message = (Integer.toString(ycor)) + (char) 13;
  113. osw.write(message);
  114. osw.flush();
  115. bos.flush();
  116. //System.out.println(message);
  117.  
  118. } catch (IOException e) { e.printStackTrace(); }
  119.  
  120. }
  121.  
  122. @Override
  123. public void mouseEntered(MouseEvent e) {
  124. // TODO Auto-generated method stub
  125.  
  126. }
  127.  
  128. @Override
  129. public void mouseExited(MouseEvent e) {
  130. // TODO Auto-generated method stub
  131.  
  132. }
  133.  
  134. @Override
  135. public void mousePressed(MouseEvent e) {
  136. // TODO Auto-generated method stub
  137.  
  138. }
  139.  
  140. @Override
  141. public void mouseReleased(MouseEvent e) {
  142. // TODO Auto-generated method stub
  143.  
  144. }
  145.  
  146. }
Last edited by BestJewSinceJC; 24 Days Ago at 12:18 am.
Out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: chandini.david is an unknown quantity at this point 
Solved Threads: 0
chandini.david chandini.david is offline Offline
Newbie Poster
 
0
  #9
23 Days Ago
Hello!!

Thanks for trying to help, but i'm afraid it still doesn't work!

Here's what's happening: I clicked in the client window, and "End of client's turn, client waiting to receive." is printed on the client console.
Then, I click again a couple of times on the client window - nothing is printed. Okay, that's good.

After that, however, when I click on the server screen again, along with printing "End of server's turn, server waiting to receive." on the server console, it also prints "End of client's turn, client waiting to receive." on the client console!! I added some more printf's and the client is actually sending the co-ords of the location of the first of the series of clicks I did out of turn.

I've gone over the code many, many times now, and I just don't see why it still doesn't work!!

Thanks,
Chand
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #10
23 Days Ago
Actually you are correct, I did not test my modifications thoroughly enough. The problem seems to reside in the server class though, not the client class. I'll take a look again.
Last edited by BestJewSinceJC; 23 Days Ago at 2:36 pm.
Out.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC