However, when I try to run a second Client program, it does not sync with the Server. I tried first killing the first instance of the Client, then pressing "R" on the server (marked as IMPORTANT METHOD in the code) to try to set up another connection on the 100th port, but the second Client still could not connect. Am I doing something wrong?
I think the problem has something to do with my lack of multithreading, but I'm not sure (I don't exactly know how to do multithreading either).
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
public class MyClient extends JFrame implements MouseListener {
Socket s;
BufferedReader br;
BufferedWriter bw;
ArrayList<Point> points=new ArrayList<Point>();
Image i=null;
public static void main(String args[]) {
new MyClient();
}
public void run()
{
try{s.setSoTimeout(1);}catch(Exception e){}
i=createImage(400,400);
while (true)
{
try{
draw();
String s=br.readLine();
System.out.println(s);
if (s.charAt(0)=='C') {
points=new ArrayList<Point>();
}
if (s.charAt(0)=='Q') {
System.exit(0);
}
if (s!=null && !s.equals("")) {
StringTokenizer stk=new StringTokenizer(s);
points.add(new Point(Integer.parseInt(stk.nextToken()),Integer.parseInt(stk.nextToken())));
}
}catch (Exception e){}
}
}
public void draw() {
Graphics g=i.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,400,400);
g.setColor(Color.RED);
for (int pc=0;pc<points.size();pc++) {
Point p=points.get(pc);
g.fillRect((int)p.getX(),(int)p.getY(),3,3);
}
Graphics g2=this.getGraphics();
g2.drawImage(i,0,0,null);
}
public MyClient()
{
try{
s = new Socket("192.168.1.5",100);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
}catch(Exception e){}
this.setSize(400,400);
this.addMouseListener(this);
this.setVisible(true);
run();
}
public void write(String s) {
try {bw.write(s);
bw.newLine();
bw.flush();} catch(Exception e) {}
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
int x=e.getX(),y=e.getY();
System.out.println("MOUSE"+x+""+y);
points.add(new Point(x,y));
write(x+" "+y);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
public class MyServerApp extends JFrame implements MouseListener, KeyListener {
ServerSocket s;
Socket s1;
BufferedReader br;
BufferedWriter bw;
ArrayList<Point> points=new ArrayList<Point>();
Image i=null;
public void run()
{
try{s1.setSoTimeout(1);}catch(Exception e){}
i=createImage(400,400);
while (true)
{
try{
draw();
String s=br.readLine();
if (s!=null && !s.equals("")) {
StringTokenizer stk=new StringTokenizer(s);
points.add(new Point(Integer.parseInt(stk.nextToken()),Integer.parseInt(stk.nextToken())));
}
}catch (Exception e){}
}
}
public void draw() {
Graphics g=i.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,400,400);
g.setColor(Color.RED);
for (int pc=0;pc<points.size();pc++) {
Point p=points.get(pc);
g.fillRect((int)p.getX(),(int)p.getY(),3,3);
}
Graphics g2=this.getGraphics();
g2.drawImage(i,0,0,null);
}
public static void main(String arg[])
{
new MyServerApp();
}
public void write(String s) {
try {bw.write(s);
bw.newLine();
bw.flush();} catch(Exception e) {}
}
public MyServerApp()
{
makeConnection();
this.setSize(400,400);
this.addMouseListener(this);
this.addKeyListener(this);
this.setVisible(true);
run();
}
public void makeConnection() { // THE IMPORTANT METHOD
try{
s = new ServerSocket(100);
s1=s.accept();
br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
bw.write("Hello");bw.newLine();bw.flush();
}catch(Exception e){}
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
int x=e.getX(),y=e.getY();
points.add(new Point(x,y));
write(x+" "+y);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_SPACE) {
write("C");
points=new ArrayList<Point>();
}
if (e.getKeyCode()==KeyEvent.VK_Q) {
write("Q");
}
if (e.getKeyCode()==KeyEvent.VK_R) {
makeConnection();
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}