0

I have the two programs and i want them to connect to my school server afs1.njit.edu but it never connects. That is where i have the files. should i run two ssh programs to each run the different programs? unsure how to test them. (these are a simple ezxample from online i want to test before i write my code) I compile them serperately and they never connect.

singleSocketserver.java

    public static void main(String[] args) {
try{
  socket1 = new ServerSocket(port);
  System.out.println("SingleSocketServer Initialized");
  int character;

  while (true) {
    connection = socket1.accept();

    BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
    InputStreamReader isr = new InputStreamReader(is);
    process = new StringBuffer();
    while((character = isr.read()) != 13) {
      process.append((char)character);
    }
    System.out.println(process);
    //need to wait 10 seconds for the app to update database
    try {
      Thread.sleep(10000);
    }
    catch (Exception e){}
    TimeStamp = new java.util.Date().toString();
    String returnCode = "SingleSocketServer repsonded at "+ TimeStamp + (char) 13;
    BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
    OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
    osw.write(returnCode);
    osw.flush();
 }
}
catch (IOException e) {}
  try {
    connection.close();
  }
  catch (IOException e) {}
  }
}

SocketClient.java

    public class SocketClient {

public static void main(String[] args) {
/** Define a host server */
String host = "afs1.njit.edu";
/** Define a port */
int port = 19999;

StringBuffer instr = new StringBuffer();
String TimeStamp;
System.out.println("SocketClient initialized");

try {
  /** Obtain an address object of the server */
  InetAddress address = InetAddress.getByName(host);
  /** Establish a socket connetion */
  Socket connection = new Socket(address, port);
  /** Instantiate a BufferedOutputStream object */
  BufferedOutputStream bos = new BufferedOutputStream(connection.
      getOutputStream());

  /** Instantiate an OutputStreamWriter object with the optional character
   * encoding.
   */
  OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");


  TimeStamp = new java.util.Date().toString();
  String process = "Calling the Socket Server on "+ host + " port " + port +
      " at " + TimeStamp +  (char) 13;

  /** Write across the socket connection and flush the buffer */
  osw.write(process);
  osw.flush();

   /** Instantiate a BufferedInputStream object for reading
  /** Instantiate a BufferedInputStream object for reading
   * incoming socket streams.
   */

  BufferedInputStream bis = new BufferedInputStream(connection.
      getInputStream());
  /**Instantiate an InputStreamReader with the optional
   * character encoding.
   */

  InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

  /**Read the socket's InputStream and append to a StringBuffer */
  int c;
  while ( (c = isr.read()) != 13)
    instr.append( (char) c);

  /** Close the socket connection. */
  connection.close();
  System.out.println(instr);
 }
catch (IOException f) {
  System.out.println("IOException: " + f);
}
catch (Exception g) {
  System.out.println("Exception: " + g);
}
}

}

John Siviano
  • 33
  • 1
  • 5

1 Answers1

1

if you're trying to connect from your home to the school's computer, then you will likely hit a firewall. Usually, though not always, connections initiated from the machine are allowed, but connections to the machine are only allowed on certain ports. You can set up your ssh to tunnel the packets but then you may as well run the 2 programs next to one another.

If you run both programs on the same machine, they should find one another, assuming that: 1: you are allowed to open sockets 2: the socket isn't already taken by anothe program 3: the firewall doesn't block those ports.

to run both on the school machine you can use 2 shells (ssh) but it isn't necessary. you can run the receiver in the background (put a & at the end of the command) and then run the sender. However it is easier to run 2 shells, especially if the program sends to sysout like yours does.

a few pointers, if you use System.out (or System.err) for debug / log output, when you consume an exception, I recommend e.printStackTrace(System.out) if you don't want to pull in a library for this. Most logging frameworks have a logger.error("message", ex) and commons.lang has an exception printer too.

How can I convert a stack trace to a string?

One thing you can do to test your logic, without the socket connection, is to use PipedInputStream and PipedOutputStream. http://docs.oracle.com/javase/7/docs/api/java/io/PipedInputStream.html but if you're sure of your logic and need to test sockets you'll have to run them side by side.

Community
  • 1
  • 1
Joeblade
  • 1,735
  • 14
  • 22