0

I am trying to connect to the tcp client and I keep getting ioexception connection refused error. I don't know what I am doing wrong. I am trying to enable tcp notification. I am connecting to a rfid reader and trying to get the data back to my program to insert in a database.

Here is my code:

Socket tagServerSocket;
     while (true){
         try{
             String data;
               tagServerSocket = new Socket("localhost", this.notifyPort);
             DataOutputStream outToServer = new 
     DataOutputStream(tagServerSocket.getOutputStream());

             while (true) {

                    try {

                        Thread.sleep(20);
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                    }   

                    int cnt_rd=0;
                    while((cnt_rd < 10) && (TagBuffer.size() > 0))
                    {

                        synchronized (TagBuffer) {                      
                            if (TagBuffer.size() != 0) {
                                data = "";
                                TagInfo tag = TagBuffer.peek();
                                data += "ReaderIP:" + tag.ipaddr;
                                data += "|ID:" + tag.epc;
                                data += "|Antenna:" + tag.antennaPort;
                                data += "|Timestamp:" + tag.timestamp;
                                data += "|PC:" + tag.pc;
                                data += "|RSSI:" + tag.rssi + "\n";



                                TagBuffer.remove();
                                outToServer.writeBytes(data);


                                cnt_rd++;
                            }

                        }

                    }


             }
                    tagServerSocket.close();
                     tagServerSocket = null;
                     Thread.sleep(10);
                     startup=true;
            } catch (UnknownHostException e) {
                tagServerSocket = null;
                startup=false;
                System.out.println("Unable to connect to port " + 
   this.notifyPort);
        } catch (IOException e) {
                // tagServerSocket = null;
                startup=false;
                String data1 = String.format("00");
                System.out.println("Unable to send tag data to server" + 
data1);
                System.out.println("ioexception tcpclient: 
 "+e.getMessage());
        } catch (InterruptedException e) {
            startup=false;
            e.printStackTrace();
        }


     }
Roro
  • 427
  • 2
  • 8
  • 20
  • Which code line is the one where the error occurs? Is it `new Socket(...)`? – t0r0X Dec 13 '18 at 18:14
  • It doesn't say where the error occurs, it just jumps to the catch ioexception and prints out that connection is refused. – Roro Dec 13 '18 at 18:21
  • Basic troubleshooting, this.notifyPort has the right port number and that port is open and listening for connections? What happens when you telnet to that port? – Duston Dec 13 '18 at 19:13
  • Yes, as @duston mentioned, print out the `this.notifyPort` to make sure it is correct. Then do a `telnet localhost PORTNR` to verify "something" is listening at that port. PS With `e.printStackTrace()` instead of `System.out.println("ioexception tcpclient: "+e.getMessage())` you'll get the exact line numbers of the error location. – t0r0X Dec 13 '18 at 21:23
  • "*I am trying to connect to the tcp client*" - that will NEVER work, as you can't connect TO a client socket, only FROM a client socket. Now, if you meant to connect to a remote client *APPLICATION*, that is a different matter. The App needs to run its own listening server socket which you can then connect TO. Does the app in question do that? – Remy Lebeau Dec 14 '18 at 01:25

0 Answers0