R
rajatag
Hello,
Attached below is a code that runs one thread per client and each
client makes a separate connection to a server.
After sometime, even though the "write()" function of the client
thread writes data to System.out, the data does not seem to go to the
server.
Does this seem like a server issue or, something is wrong in the code
below?
Thanks,
///// InitClients.java ////
///////////////////////////
import java.io.*;
import java.util.*;
public class InitClients {
ClientThread clientThread[] = new ClientThread[20];
String serverIP;
Timer KeepConnectionOpenTimer = null;
Timer FlagTimer = null;
public InitClients() {
/// start as many clients as needed by calling the
clientThread[0] = new ClientThread("www.domain.com", 90000);
clientThread[1] = new ClientThread("www.domain.com", 90000);
clientThread[2] = new ClientThread("www.domain.com", 90000);
clientThread[3] = new ClientThread("www.domain.com", 90000);
// two timers. one at 5 minute intervals
// another at 30 second intervals
KeepConnectionOpenTimer = new Timer();
KeepConnectionOpenTimer.schedule(new ConnectionOpen(), 5 * 60 *
1000,
5 * 60 * 1000);
FlagTimer = new Timer();
FlagTimer.schedule(new FlagTask(), 30 * 1000, 1 * 1000);
}
class FlagTask extends TimerTask {
public void run() {
try {
for (int i = 0; i < clientThread.length; i++) {
if (clientThread != null)
clientThread.write("SENDFLAG"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ConnectionOpen extends TimerTask {
public void run() {
try {
for (int i = 0; i < clientThread.length; i++) {
if (clientThread != null)
clientThread.write("CONNECTOPEN"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] arguments) throws IOException {
new InitClients();
}
}
///// ClientThread.java ////
////////////////////////////
import java.io.*;
import java.net.Socket;
import java.util.*;
public class ClientThread implements Runnable {
Socket socket = null;
BufferedReader fromServer;
protected Thread thread;
BufferedWriter out;
public ClientThread(byte index, String server, int port) {
thread = new Thread(this);
try {
socket = new Socket(server, port);
socket.setKeepAlive(true);
fromServer = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private String readline() {
try {
return this.fromServer.readLine();
} catch (IOException e) {
return null;
}
}
public void run() {
try {
inputRun();
if (socket != null && !socket.isClosed()) {
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean write(String s) {
try {
out.write(s + "\r\n");
out.flush();
// DATA IS ALWAYS WRITTEN HERE!
// EVEN IF IT DOES NOT GO TO SERVER
System.out.println(System.currentTimeMillis() + ":OU: " + s);
return true;
} catch (Exception e) {
e.printStackTrace();
close();
return false;
}
}
public void close() {
try {
fromServer.close();
out.close();
socket.close();
} catch (Exception e) {
}
}
public void inputRun() {
String s = null;
while (((s = readline()) != null)) {
// process incoming here
}
}
}
Attached below is a code that runs one thread per client and each
client makes a separate connection to a server.
After sometime, even though the "write()" function of the client
thread writes data to System.out, the data does not seem to go to the
server.
Does this seem like a server issue or, something is wrong in the code
below?
Thanks,
///// InitClients.java ////
///////////////////////////
import java.io.*;
import java.util.*;
public class InitClients {
ClientThread clientThread[] = new ClientThread[20];
String serverIP;
Timer KeepConnectionOpenTimer = null;
Timer FlagTimer = null;
public InitClients() {
/// start as many clients as needed by calling the
clientThread[0] = new ClientThread("www.domain.com", 90000);
clientThread[1] = new ClientThread("www.domain.com", 90000);
clientThread[2] = new ClientThread("www.domain.com", 90000);
clientThread[3] = new ClientThread("www.domain.com", 90000);
// two timers. one at 5 minute intervals
// another at 30 second intervals
KeepConnectionOpenTimer = new Timer();
KeepConnectionOpenTimer.schedule(new ConnectionOpen(), 5 * 60 *
1000,
5 * 60 * 1000);
FlagTimer = new Timer();
FlagTimer.schedule(new FlagTask(), 30 * 1000, 1 * 1000);
}
class FlagTask extends TimerTask {
public void run() {
try {
for (int i = 0; i < clientThread.length; i++) {
if (clientThread != null)
clientThread.write("SENDFLAG"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ConnectionOpen extends TimerTask {
public void run() {
try {
for (int i = 0; i < clientThread.length; i++) {
if (clientThread != null)
clientThread.write("CONNECTOPEN"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] arguments) throws IOException {
new InitClients();
}
}
///// ClientThread.java ////
////////////////////////////
import java.io.*;
import java.net.Socket;
import java.util.*;
public class ClientThread implements Runnable {
Socket socket = null;
BufferedReader fromServer;
protected Thread thread;
BufferedWriter out;
public ClientThread(byte index, String server, int port) {
thread = new Thread(this);
try {
socket = new Socket(server, port);
socket.setKeepAlive(true);
fromServer = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private String readline() {
try {
return this.fromServer.readLine();
} catch (IOException e) {
return null;
}
}
public void run() {
try {
inputRun();
if (socket != null && !socket.isClosed()) {
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean write(String s) {
try {
out.write(s + "\r\n");
out.flush();
// DATA IS ALWAYS WRITTEN HERE!
// EVEN IF IT DOES NOT GO TO SERVER
System.out.println(System.currentTimeMillis() + ":OU: " + s);
return true;
} catch (Exception e) {
e.printStackTrace();
close();
return false;
}
}
public void close() {
try {
fromServer.close();
out.close();
socket.close();
} catch (Exception e) {
}
}
public void inputRun() {
String s = null;
while (((s = readline()) != null)) {
// process incoming here
}
}
}