Multiple Client Sockets, Single java program, connect to server socket, possible?
Hi all,
I have a strange problem. I started work on software expecting that it
was possible to create many client sockets in a single instance of a
java program that connect to a single server where ever that maybe.
In this case I have a thread running that determines actions, and
multiple threads are generated depending on the determined actions...
each thread requires a connection to the same server.
My issue is, when I call accept() and the server socket waits for a
connection... it only returns and services one socket per instance of
the program at a time. The client recieves no indication that accept()
didn't return a socket and proceeds to send data throught the socket
which is therefore lost.
Is it something I am doing wrong / not configuring in the socket or
server socket? Or is this not possible.
The sample code below demostrates the scenario... 2 clients are created
and begin sending text to the server... the server prints the text that
is received but only for the first client socket that connects... alll
other output is lost and only one request handler is spawed for the 2
requests.
Thanks for your help
Test code;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class socTest extends Thread {
public socTest() {
start();
synchronized(this) {
try {
wait(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
new reqSend("a bunch of flowers", 1).start();
new reqSend("a shiney new car", 2).start();
}
public static void main(String[] args) {
new socTest();
}
public void run() {
try {
ServerSocket s = new ServerSocket(1108);
s.setReuseAddress(false);
while (true) {
try {
Socket s1 = s.accept();
System.out.println("incoming request");
s1.setReuseAddress(false);
new reqRec(s1);
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private class reqRec {
public reqRec(Socket s) {
try {
System.out.println("req spawned");
DataInputStream dis = new
DataInputStream(s.getInputStream());
DataOutputStream dos = new
DataOutputStream(s.getOutputStream());
String str = "";
while (true) {
while (dis.available()==0) {
synchronized(this) {
notify();
wait(1);
}
}
str = dis.readUTF();
System.out.println(str);
}
} catch (Exception e) {
System.out.println("dead");
}
}
}
private class reqSend extends Thread {
String str;
int id;
public reqSend(String txt, int _id) {
str = txt;
id = _id;
}
public void run() {
try {
Socket s = new Socket("LocalHost", 1108);
s.setReuseAddress(false);
System.out.println("fetching streams");
DataInputStream dis = new
DataInputStream(s.getInputStream());
DataOutputStream dos = new
DataOutputStream(s.getOutputStream());
int i = 0;
while (i<100) {
synchronized(this) {
System.out.println(id + " - " + str + " send");
dos.writeUTF(str + i);
}
i++;
yield();
}
dos.flush();
s.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("dead");
}
}
}
}