I am making a client to client chatting application in Java and I wanted to know how I could create Sockets on demand. More specifically, I wanted to know if there is any method which checks whether there is an incoming connection. Using that I could have two methods running simultaneously together with Threads with one method waiting for a connection while the other handles the server (messages being sent). Is this a fine strategy or should I use a different technique?
What if I used a Socket Array and added a new Socket to it with each connection?
Would the array however cause a problem when referencing to Sockets later on?
The if has a && with nothing as I want to add a method there which will help me check whether there is an incoming connection or not.
import java.util.*;
import java.io.*;
import java.net.*;
public class Server {
public static ServerSocket SSock;
public static Socket Sock;
public static DataInputStream dis;
public static DataOutputStream dos;
public static PrintWriter pw;
public static BufferedReader br;
public static Socket[] wow;
public int counter = 0;
public int port = 2500;
public Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException{
SSock = new ServerSocket();
Sock = SSock.accept();
dis = new DataInputStream(Sock.getInputStream());
dos = new DataOutputStream(Sock.getOutputStream());
pw = new PrintWriter(dos, true);
br = new BufferedReader(new InputStreamReader(dis));
Server s = new Server();
Thread t1 = new Thread(s.new connection());
Thread t2 = new Thread(s.new server());
t1.start();
t2.start();
}
public class connection implements Runnable {
public void run() {
try {
Thread.sleep(200);
} catch (Exception e) {
//NOTHING!! MWAH MWAH MWAH
//Sigh. I'll add something here later...
}
if ( && Sock.isConnected()) {
}
}
}
public class server implements Runnable{
public void run() {
}
}
}