-2

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() {

    }
}

}

  • 2
    I suggest you use `ServerSocket.accept()` I suggest you look up a tutorial on how to use this (or networking with Java in general) – Peter Lawrey Feb 11 '16 at 16:36
  • @PeterLawrey I have the first Socket which uses that method. How should I go on with the on-demand part of the code? I will upload my code in a few. –  Feb 11 '16 at 16:38
  • 1
    If you need more than one of these I suggest you use a loop. – Peter Lawrey Feb 11 '16 at 16:52
  • @Adit Kirtani: this question will be useful for you: http://stackoverflow.com/questions/33087890/multithreading-with-client-server-program/33088473#33088473 – Ravindra babu Feb 11 '16 at 17:05
  • None of these data members should be static, and some of them are declared in the wrong class. The DataInput/OutputStreams are pointless here, as are the sleeps and the `isConnected()` test. Never ignore an exception. You need to study the Custom Networking section of the Java Tutorial. Too broad. – user207421 Feb 11 '16 at 19:37
  • @EJP That's cause I haven't used them yet. As for the Exception, I've said I'll add something later on. Read the code. –  Feb 12 '16 at 13:51
  • So guys the answer actually is I use a method which returns a Socket –  May 15 '16 at 16:16

2 Answers2

1

I think you have somehow misunderstood the concept of TCP sockets. When you try to initiate a connection to remote server it does not “create demand;” the server has to listen on the socket before you initiate the request, otherwise you will only get a “connection refused” error.

In Java, ServerSocket.accept() handles all that for you: it listens for incoming connections and as soon as a connection has been established, it returns you the Socket that you use for all further communication on this particular connection.

Bombe
  • 81,643
  • 20
  • 123
  • 127
0

I figured it out I have to make a method which returns a socket.

public Socket s (Socket so) {  //When I add a Socket to an arraylist, I call this method
    //edit properties
    return so; 
}