27

A server software my client communicates with regularly sends transaction messages on port 4000. I need to print those messages to the console line by line. (Eventually I will have to write those values to a table, but I’m saving that for later.)

I tried this code but it doesn’t output anything:

package merchanttransaction;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MerchantTransaction {
    public static void main(String[] args) {
        try {
            InetAddress host = InetAddress.getLocalHost();
            Socket socket = new Socket("192.168.1.104", 4000);
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String message = (String) ois.readObject();
            System.out.println("Message: " + message);

            ois.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

By the way, I need to be able to monitor that port until the program terminates. I’m not sure if the code above will be able to do that because I don’t see any iteration to the code.

I’m using Java version 1.6.0_24, SE Runtime Environment (build 1.6.0_24-b07) running on Ubuntu.

2
  • You are reading an Object but perhaps server is sending plain text, you have to know exactly what is sent to read it as same datatype, good luck Jun 9, 2011 at 20:03
  • Yes, I believe the messages to capture is in plain text. Jun 9, 2011 at 20:13

3 Answers 3

28

You need to use a ServerSocket. You can find an explanation here.

17

What do you actually want to achieve? What your code does is it tries to connect to a server located at 192.168.1.104:4000. Is this the address of a server that sends the messages (because this looks like a client-side code)? If I run fake server locally:

$ nc -l 4000

...and change socket address to localhost:4000, it will work and try to read something from nc-created server.

What you probably want is to create a ServerSocket and listen on it:

ServerSocket serverSocket = new ServerSocket(4000);
Socket socket = serverSocket.accept();

The second line will block until some other piece of software connects to your machine on port 4000. Then you can read from the returned socket. Look at this tutorial, this is actually a very broad topic (threading, protocols...)

3
  • Thanks Tomasz. I'm not really sure what terminologies to use. I'm really new at this. But in layman's terms, what I need is a piece of code that will listen to a port where another pre-made program is sending message to. It doesn't need to send anything. It just needs to listen to it. Jun 9, 2011 at 20:10
  • Listening is a traditionally a role of a server so you need a ServerSocket as I suggested. The client needs a client socket to connect to this server in a similar way you did that in your example (however in your code you should send rather than receive messages; your receiving code goes to ServerSocket side). So basically you have all the pieces (your and mine code) to complete. Jun 9, 2011 at 20:19
  • +1 Ah, I had it the other way around. This is really enlightening, Tomasz. I'll try to cook up something and get back for more questions (I hope not). Thank you very much! Jun 9, 2011 at 20:27
4

Try this piece of code, rather than ObjectInputStream.

BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
while (true)
{
    String cominginText = "";
    try
    {
        cominginText = in.readLine ();
        System.out.println (cominginText);
    }
    catch (IOException e)
    {
        //error ("System: " + "Connection to server lost!");
        System.exit (1);
        break;
    }
}
4
  • "Cannot find symbol. Symbol: class BufferedReader" Sorry. Total I'm a total noob to Java :( Jun 9, 2011 at 20:05
  • You have to import java.io.*;
    – Pangolin
    Jun 10, 2011 at 12:44
  • No problem! Does this solution work? Maybe use this together with what the other said about using a ServerSocket
    – Pangolin
    Jun 10, 2011 at 15:11
  • I'm not getting any response from the client. I'll check further Jun 12, 2011 at 13:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.