Getting two different classes/programs interacting

Posted on 2006-11-09
Last Modified: 2010-03-31
For an AI program I am making I need to get two programs interacting - I am doing this by using sockets and a threads.

This program is a smaller version of th AI program. There are two of these programs - opposites i.e. where one says female the other says male and so on.

Ok this is what should happen:

-Program starts and background threads starts with it (always reading input).

-Command is issued by the other program ("mate").

-This triggers the connect() method which sends a command to the other program (action triggered) therefore they have interacted.

Please give me any hints as to why this does not work.


import java.util.Scanner;
import java.util.Vector;
import java.util.Random;
//set path="C:\Program Files\Java\jdk1.5.0_08\bin"
public class AIPetTest
      public Socket socket = null;
      public String command = "";
      public static Vector<String> commands = null;
      public String line = null;
      public String start()
            Scanner command = new Scanner(;
            String commandIssued = command.nextLine();
                  catch(Exception e)
                        System.err.println("Caught Exception: " + e.getMessage());
            return null;
      public void connect(String update) throws Exception
                  System.out.println("Connection made");
                socket = new Socket("localhost", 1977); // throws Exception below, if no connection is possible
          catch(ConnectException ce)
                System.out.println("Connection lost");
                socket = new ServerSocket(1977).accept();
                // no connection was possible, so make app server
                //socket = new ServerSocket(1977).accept();
                // be aware that accept is blocking!
            command = commands.remove(0);  // Take the 1st object out
            System.out.println("Command: " + command);
            if(command.equals("updateMale")) // incoming
    Thread backgroundThread = new Thread(new Runnable() // reads all incoming messages and stores them in a vector
          public void run()
                  System.out.println("Thread running");
                  commands = new Vector<String>();
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                               line = in.readLine();
                               if(line == null)
                                     break; // out of the while
                               commands.add(new String(line + '\n'));
                  catch(IOException ioe)
                        System.err.println("Caught Exception: " + ioe.getMessage());
      public static void main(String[] args)
            AIPetTest pet = new AIPetTest();
Question by:john8932
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 13

Expert Comment

ID: 17905970

OutputStream os = sock.getOutputStream();
PrintWriter pw = new PrintWriter(os,true);

the true inside the PrintWriter constructor will autoflush the output.


Author Comment

ID: 17906030
What does the autoflush do?

Do you suggest I replace the code you mentioned?

I forgot to say that when I run this I always get this exception although the  program carries on and allows me to enter the mate command which then gives another exception:

Thread running
Exception in thread "Thread-0" java.lang.NullPointerException
        at AIPetTest2$

Connection made
Caught Exception: Array index out of range: 0

LVL 13

Accepted Solution

marchent earned 250 total points
ID: 17906228
i think this is a client side program, did u implemented a serverside program for this code? otherwise u have to implement the serverside code.

>>BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
socket.getInputStream() returns null, so null pointer exception.

Author Comment

ID: 17906270
I think I will simplify this so that one program is the server - always listening for a command, and one is the client. It runs second. It connects then it sends a command.

If you can get the other way working i.e. connect both ways alternatively then please do as this would be better.

This is a client side program.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding multiple JVM environments to RedHat 6 7 70
Cisco ASA: Java web start no go, asdm launcher no go 3 58
batch vs regular insert in spring DAO 2 25
JDBC performance 8 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question