Open TCP & UDP - Java

The Ewon gateway can publish its tag values through Modbus TCP or SNMP protocols natively.

The Ewon gateway acts as a ModbusTCP / SNMP / OPCUA server in this case.

However, when you must publish the tag values through another protocol, you can use the BASIC script to develop your own client or server protocol based on TCP or UDP protocols.

The JAVA class javax.microedition.io.Connector provides all the functions and objects needed to develop a custom TCP/UDP client or server.

Here is an example of a small Telnet server that echoes all incoming commands:

try
	{
		// Open a server socket at the port specified
		serverSocket = (ServerSocketConnection) Connector.open("socket://:" + port);
                       
		while(true)
		{
			SocketConnection socket = (SocketConnection) serverSocket.acceptAndOpen();
			InputStream input = socket.openInputStream();
            OutputStream output = socket.openOutputStream();
            String request = "";
                               
            while(true)
            {     
				byte[] buffer = new byte[1024];
                int length;                                 
                if((length = input.read(buffer)) > 0 )
                {
                    request += new String(buffer,0,length);
                    if (request.indexOf('\r')> -1)
                    {
                        if (request.startsWith("exit"))
                        {
							break;
						}
                        output.write(request.getBytes());
                        output.flush();  
                        request = "";
                    }
                }                                    
            }                                
            input.close();
            output.close();
            socket.close();			
		}		
	}
	catch(Exception e)
	{System.err.println(e.getMessage());}

For more details, please refer to the JAVA section.