Skip to main content
Home
for DLMS smart meters

Main navigation

  • Home
  • Products
  • About us
  • Open Source
  • Community
  • Forum
  • Downloads
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Issue With Socket Connection After Serial Port Communication and IP Retrieval

Issue with Socket Connection After Serial Port Communication and IP Retrieval

By Tharun, 4 February, 2025
Forums
Gurux DLMS for Java

Hello everyone,

I am currently working on a project where I need to retrieve information from a meter using both serial and TCP connections. The process involves reading a meter’s UID via the serial port, then obtaining the meter’s IP address, and finally, querying the meter for its details via a TCP connection.

Here’s the sequence of operations I’m following:

1. I read the UID from the meter using the serial port.
2. I close the serial port.
3. After a delay of 500 milliseconds, I attempt to retrieve the IP of the meter using the GXSerial port.
4. Once I have the IP, I attempt to establish a TCP connection to the meter to read its data (such as serial number, profile read, clock).

However, I’m running into the following issues when trying to establish the TCP connection:

Error 1: java.net.SocketException: Connection reset
Error 2: java.net.SocketException: Socket operation on non-socket

Additionally, sometimes the code does not exit from the settings.media.open(); method, and it hangs indefinitely.

Troubleshooting Steps Taken:

I’ve verified that the serial communication and the retrieval of the UID work as expected.
Close the media below code i have used

finally {
if (reader != null) {
try {
reader.close(); // diconnect request
settings.media.close()
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

Any advice or insights into solving this issue would be greatly appreciated.

Thank you in advance for your help!

Profile picture for user Kurumi

Kurumi

11 months 1 week ago

Hi, SocketException is…

Hi,

SocketException is coming from Java and it usually means that your TCP/IP address or port is invalid. Check those at the start.

BR,
Mikko

Tharun

11 months 1 week ago

Hi Mikko, Thanks for the…

Hi Mikko, Thanks for the suggestion. The IP and port are correct, but in this case, I'm continuously reading data. 

It seems that when I try to connect to the internet (Mobile network), It might be encountering below issue.

java.net.SocketException: Socket operation on nonsocket: socket at java.base/java.net.Socket.createImpl(Socket.java:533) at java.base/java.net.Socket.connect(Socket.java:632) at gurux.net.GXNet.open(GXNet.java:536) at gurux.dlms.client.tcp.TCPConnection.main(TCPConnection.java:70) at gurux.dlms.client.tcp.craeteSerialAndTCP.lambda$1(craeteSerialAndTCP.java:53) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:842) 
java.net.SocketException: Socket operation on nonsocket: no further information
	at java.base/sun.nio.ch.IOUtil.configureBlocking(Native Method)
	at java.base/sun.nio.ch.NioSocketImpl.configureNonBlocking(NioSocketImpl.java:224)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:590)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
	at java.base/java.net.Socket.connect(Socket.java:633)
	at gurux.net.GXNet.open(GXNet.java:536)
	at gurux.dlms.client.tcp.TCPConnection.main(TCPConnection.java:70)
	at gurux.dlms.client.tcp.craeteSerialAndTCP.lambda$1(craeteSerialAndTCP.java:53)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:842)

Could this be related to a network fluctuation or a timeout when switching connections or related to meter connectivity? 

Or, here socket connection is happening with GXNet jar file, Can i create my own socket logic to communicate with meter. If its is possible let me know and explain with simple program to read serial number through TCP. 

BR,

Tharun

Profile picture for user Kurumi

Kurumi

11 months 1 week ago

Hi, Is your meter using a…

Hi,

Is your meter using a static or dynamic IP address? Read this at first.

https://gurux.fi/Gurux.DLMS.DynamicIP

It will help you to get started.

BR,
Mikko

Tharun

11 months 1 week ago

Hi Mikko, Our meter is using…

Hi Mikko, 

Our meter is using a static IP, so in this case, the meter can initiate the connection to the server. However, when I continuously try to read data, I am getting a socket exception. The IP we are using is obtained via the GXSerial HDLC port. 

The same IP is being used to read the meter information. In this case, I am able to successfully read the data the first time, but on the second attempt, I can retrieve the IP through the port, but when I try to read using the IP, I encounter an exception in below line. 

settings.media.open(); 
Exception is: java.net.SocketException: Socket operation on nonsocket: connect

I observed that once I get the IP, I am trying to ping using the code below. 

 public static boolean pingIPv6(String ipv6Address, int port) {
       try (Socket socket = new Socket()) {
           Inet6Address inetAddress = (Inet6Address) Inet6Address.getByName(ipv6Address);
           InetSocketAddress socketAddress = new InetSocketAddress(inetAddress, port);
           socket.connect(socketAddress, 5000); // Timeout of 5 seconds
           return true; // Connection was successful
       } catch (UnknownHostException e) {
           throw new RuntimeException("Unknown Host: " + ipv6Address);
       } catch (IOException e) {
           throw new RuntimeException("Error connecting to " + ipv6Address + " on port " + port + ": " + e.getMessage());
       }
   }

However, I encountered the following exception. 

1. Error connecting to ip on port no: Socket operation on nonsocket: no further information.
2. Error connecting to ip on port no: Socket operation on nonsocket: connection time out

Based on this, it seems that I am unable to create a socket connection. Is there any issue related to the meter?

BR, 

Tharun

Profile picture for user Kurumi

Kurumi

11 months ago

Hi, There is something wrong…

Hi,

There is something wrong with TCP/IP connection.

The reason is usually the wrong IP address or port number. Sometimes there is a firewall that blocks the connection.

Check those at first.

BR,
Mikko

Tharun

11 months ago

Hi,I have checked firewall…

Hi,

I have checked firewall settings, Everything is fine only, I have made TCP connection using below code

          settings.client = new GXDLMSSecureClient2(true);
           GXNet net = null;
           settings.media = new GXNet();
           net = (GXNet) settings.media;
           net.setHostName(args[0]);//enter port name
           net.setPort(1234);
           settings.client.setInterfaceType(InterfaceType.WRAPPER);
           settings.client.setClientAddress(32);
           settings.client.setAuthentication(Authentication.LOW);
           settings.client.setPassword(args[1].getBytes());
           settings.client.getCiphering().getSecurityPolicy().clear();
           settings.client.getCiphering().setSecurity(Security.ENCRYPTION);
           settings.client.getCiphering().setSystemTitle(GXCommon.hexToBytes(args[2]));
         settings.client.getCiphering().setAuthenticationKey(GXCommon.hexToBytes(args[3]));
           settings.client.getCiphering().setBlockCipherKey(GXCommon.hexToBytes(args[4]));
           settings.invocationCounter = args[5];// 0.0.43.1.2.255
           settings.client.setStandard(Standard.INDIA);
           settings.client.setUseUtc2NormalTime(true);
           settings.trace = TraceLevel.VERBOSE;

Here I am connected mobile internet for to create connection between meter and pc.

Once I read data through ip i have close the resource which is followed by gurux sample client code.

    finally {
           try {
               System.out.println("********************** RESOURCE IS CLOSED*********************************");
               if (reader != null) {
                   reader.close();
               }
           } catch (Exception e) {
               System.out.println("Exception in process command: " + e.getMessage());
           }
       }

Now my problem second time when I try to read data through ip, I have got socket exception.

 Note: Here ip I have got through the GXDLMS HDLC port so it can't be wrong.

So kindly check the TCP connection code is there any issue let me know sir.

BR,

Tharun

Profile picture for user Kurumi

Kurumi

11 months ago

Hi, I'm sorry, but I can't…

Hi,

I'm sorry, but I can't help you with this. This is Java connection problem and the reason can be anything. For some reason, the TCP/IP connection fails and I have proposed a few reasons.

BR,
Mikko

Tharun

11 months ago

Hi, Could you please let me…

Hi,

Could you please let me know which Java version is supported for this connection?
In my project I am using java 21. This will help in troubleshooting the issue more effectively.
Please reply ASAP.

BR,
Tharun

Profile picture for user Kurumi

Kurumi

11 months ago

Hi, All Java versions…

Hi,

All Java versions support this. The socket connection established fails for some reason. I have told you the possible reasons why the connection might fail.

BR,
Mikko

  • Create new account
  • Reset your password

Hire Us!

Latest Releases

  • Wed, 01/14/2026 - 14:29
    Gurux.DLMS.Python 1.0.196
  • Mon, 12/29/2025 - 10:38
    Gurux.Serial.Android 3.0.5
  • Mon, 12/15/2025 - 08:11
    Gurux.DLMS.Net 4.0.87
  • Fri, 12/12/2025 - 08:38
    Gurux.DLMS.Python 1.0.195
  • Thu, 12/11/2025 - 13:22
    Gurux.DLMS.Python 1.0.194

New forum topics

  • 6-digit field values from meter not fully received (only 5 digits shown)
  • Gurux DLMS Android App
  • AMR support for bidirectional meters.
  • addition of new object in object list, meter not working
  • old meter take to much time(l&t)
More
RSS feed
Privacy FAQ GXDN Issues Contact
Follow Gurux on Twitter Follow Gurux on Linkedin