Please find below code, which runs fine on Windows 10 System and is able to discover remote devices across the network. On Linux we are creating a jar out of the code and running it using java -jar test.jar
package main;
import java.util.ArrayList;
import java.util.List;
import com.serotonin.bacnet4j.LocalDevice;
import com.serotonin.bacnet4j.RemoteDevice;
import com.serotonin.bacnet4j.RemoteObject;
import com.serotonin.bacnet4j.ServiceFuture;
import com.serotonin.bacnet4j.event.DeviceEventAdapter;
import com.serotonin.bacnet4j.exception.BACnetException;
import com.serotonin.bacnet4j.exception.ErrorAPDUException;
import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
import com.serotonin.bacnet4j.npdu.ip.IpNetworkBuilder;
import com.serotonin.bacnet4j.service.acknowledgement.ReadPropertyAck;
import com.serotonin.bacnet4j.service.acknowledgement.ReadPropertyMultipleAck;
import com.serotonin.bacnet4j.service.confirmed.*;
import com.serotonin.bacnet4j.service.unconfirmed.WhoIsRequest;
import com.serotonin.bacnet4j.transport.DefaultTransport;
import com.serotonin.bacnet4j.transport.Transport;
import com.serotonin.bacnet4j.type.constructed.ReadAccessResult;
import com.serotonin.bacnet4j.type.constructed.ReadAccessSpecification;
import com.serotonin.bacnet4j.type.constructed.SequenceOf;
import com.serotonin.bacnet4j.type.enumerated.ObjectType;
import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
import com.serotonin.bacnet4j.type.enumerated.Segmentation;
import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
import com.serotonin.bacnet4j.type.primitive.Real;
import com.serotonin.bacnet4j.util.DiscoveryUtils;
public class test {
public static void main(String[] args) throws Exception {
IpNetwork network = new IpNetworkBuilder().broadcastIp("192.168.1.255").localBindAddress("192.168.1.164").port(47808).build();
Transport transport = new DefaultTransport(network);
transport.setTimeout(500000);
transport.setSegTimeout(15000);
LocalDevice localDevice = new LocalDevice(21312, transport);
localDevice.getEventHandler().addListener(new DeviceEventAdapter() {
#Override
public void iAmReceived(RemoteDevice device) {
System.out.println("Discovered device " + device);
System.out.println("device Address" + device.getAddress().getMacAddress().getDescription());
localDevice.addRemoteDevice(device);
final RemoteDevice remoteDevice = localDevice.getRemoteDevice(device.getAddress());
remoteDevice.setSegmentationSupported(Segmentation.segmentedBoth);
new Thread(new Runnable() {
#Override
public void run() {
try {
try {
DiscoveryUtils.getExtendedDeviceInformation(localDevice, remoteDevice);
} catch (BACnetException e) {
e.printStackTrace();
}
System.out.println(remoteDevice.getName() + " " + remoteDevice.getVendorName() + " " + remoteDevice.getModelName() + " " + remoteDevice.getAddress() + " " + remoteDevice.getProtocolRevision() + " " + remoteDevice.getProtocolVersion());
ReadPropertyAck ack = localDevice.send(remoteDevice, new ReadPropertyRequest(remoteDevice.getObjectIdentifier(), PropertyIdentifier.objectList)).get();
SequenceOf<ObjectIdentifier> value = ack.getValue();
for (ObjectIdentifier id : value) {
List<ReadAccessSpecification> specs = new ArrayList<ReadAccessSpecification>();
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.presentValue));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.units));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.objectName));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.description));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.objectType));
ReadPropertyMultipleRequest multipleRequest = new ReadPropertyMultipleRequest(new SequenceOf<ReadAccessSpecification>(specs));
ReadPropertyMultipleAck send = localDevice.send(remoteDevice, multipleRequest).get();
SequenceOf<ReadAccessResult> readAccessResults = send.getListOfReadAccessResults();
System.out.print(id.getInstanceNumber() + " " + id.getObjectType() + ", ");
for (ReadAccessResult result : readAccessResults) {
for (ReadAccessResult.Result r : result.getListOfResults()) {
System.out.print(r.getReadResult() + ", ");
}
}
System.out.println();
}
ObjectIdentifier mode = new ObjectIdentifier(ObjectType.analogValue, 11);
ServiceFuture send = localDevice.send(remoteDevice, new WritePropertyRequest(mode, PropertyIdentifier.presentValue, null, new Real(2), null));
System.out.println(send.getClass());
} catch (ErrorAPDUException e) {
System.out.println("Could not read value " + e.getApdu().getError() + " " + e);
} catch (BACnetException e) {
e.printStackTrace();
}
}
}).start();
}
#Override
public void iHaveReceived(RemoteDevice device, RemoteObject object) {
System.out.println("Value reported " + device + " " + object);
}
});
localDevice.initialize();
localDevice.sendGlobalBroadcast(new WhoIsRequest());
System.in.read();
localDevice.terminate();
}
}
On Linux we are unable to discover the devices that are running on Windows OS but the application that is running on Windows is able to discover Linux BacNet localDevice. Code is same but still unable to discover BacNet devices on Linux.
Please help us find any solution.
Issues like this (partial visibility) are often caused my a 'mismatch' of IP parameters. I notice you have hardcoded "192.168.1.255". Is this really the broadcast IP address from the PI's perspective?
Resolved the problem by removing the broadcast and LocalBindAddress.
Related
How can I create a dead letter queue and test it out? I have a producer and consumer. I also have producer configs like acks, retries.
Producer.java
package org.timothy.producer;
import org.apache.kafka.clients.producer.*;
import org.timothy.producer.common.AppConfigs;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.timothy.producer.common.PropConfigs;
import java.util.concurrent.ExecutionException;
public class Producer{
private static final Logger logger = LogManager.getLogger(Producer.class);
public static void main(String[] args) {
logger.info("Creating Kafka Producer...");
KafkaProducer<Integer, String> producer = PropConfigs.prodProps();
logger.info("Start sending messages...");
for (int i = 1; i <= AppConfigs.numEvents; i++) {
ProducerRecord<Integer, String> record = new ProducerRecord<>(AppConfigs.topicName, "This is Message: " + i);
try {
RecordMetadata metadata = producer.send(record).get();
System.out.println("Record sent with key " + i + " to partition " + metadata.partition()
+ " with offset " + metadata.offset());
}
catch (ExecutionException e) {
System.out.println("Error in sending record");
//System.out.println(e);
e.printStackTrace();
}
catch (InterruptedException e) {
System.out.println("Error in sending record");
//System.out.println(e);
}
}
producer.flush();
producer.close();
logger.info("Finished - Closing Kafka Producer.");
}
}
Consumer.java
package org.timothy.producer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.timothy.producer.common.AppConfigs;
import org.timothy.producer.common.PropConfigs;
import java.time.Duration;
import java.util.Collections;
public class Consumer{
private static final Logger logger = LogManager.getLogger(Consumer.class);
public static void main(String[] args) {
KafkaConsumer<Integer, String> consumer = PropConfigs.consProps();
consumer.subscribe(Collections.singleton(AppConfigs.topicName));
int noMessageFound = 0;
while(true){
ConsumerRecords<Integer,String> records = consumer.poll(Duration.ofMillis(1000));
if (records.count() == 0) {
noMessageFound++;
if (noMessageFound > AppConfigs.MAX_NO_MESSAGE_FOUND_COUNT)
break;
else
continue;
}
records.forEach(record -> logger.info("Received new record: " +
" Key: " + record.key() +
", Value: " + record.value() +
", Topic: " + record.topic() +
", Partition: " + record.partition() +
", Offset: " + record.offset() + "\n"
));
consumer.commitAsync();
}
}
}
How do I implement DLQ in my code? How to test the retries, when I run my program I don't encounter errors that's why I don't know if the program retries or not.
when I run my program I don't encounter errors
Then introduce them. Create a counter, when it hits a certain value, throw a RuntimeException. In the catch block, use your producer instance to send an event to a new topic.
If you want to test retries, shutdown the broker or introduce some other network exception
I am trying to work with bacnet4j. but unable to find any tutorial or guide. I am using Bacnet-stack simulator for testing.
I tried this code to check i am functionality and I have also tried localdevice but both the cases i am getting error of no class definition error in this package com.serotonin.bacnet4j.npdu.Network.
package bacnet_4_j_test;
import com.serotonin.bacnet4j.LocalDevice;
import com.serotonin.bacnet4j.RemoteDevice;
import com.serotonin.bacnet4j.RemoteObject;
import com.serotonin.bacnet4j.ServiceFuture;
import com.serotonin.bacnet4j.event.DeviceEventAdapter;
import com.serotonin.bacnet4j.exception.BACnetException;
import com.serotonin.bacnet4j.exception.ErrorAPDUException;
import com.serotonin.bacnet4j.npdu.ip.IpNetwork;
import com.serotonin.bacnet4j.service.acknowledgement.ReadPropertyAck;
import
com.serotonin.bacnet4j.service.acknowledgement.ReadPropertyMultipleAck;
import com.serotonin.bacnet4j.service.confirmed.*;
import com.serotonin.bacnet4j.service.unconfirmed.WhoIsRequest;
import com.serotonin.bacnet4j.transport.DefaultTransport;
import com.serotonin.bacnet4j.transport.Transport;
import com.serotonin.bacnet4j.type.constructed.ReadAccessResult;
import com.serotonin.bacnet4j.type.constructed.ReadAccessSpecification;
import com.serotonin.bacnet4j.type.constructed.SequenceOf;
import com.serotonin.bacnet4j.type.enumerated.ObjectType;
import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
import com.serotonin.bacnet4j.type.enumerated.Segmentation;
import com.serotonin.bacnet4j.type.primitive.ObjectIdentifier;
import com.serotonin.bacnet4j.type.primitive.Real;
import com.serotonin.bacnet4j.util.DiscoveryUtils;
import java.util.ArrayList;
import java.util.List;
/**
*
* #author kaushikdas
*/
public class Bacnet_4_j_test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
IpNetwork network = new IpNetwork("192.168.2.255", IpNetwork.DEFAULT_PORT);
Transport transport = new DefaultTransport(network);
transport.setTimeout(500000);
transport.setSegTimeout(15000);
final LocalDevice localDevice = new LocalDevice(1338, transport);
localDevice.getEventHandler().addListener(new DeviceEventAdapter() {
public void iAmReceived(RemoteDevice device) {
System.out.println("Discovered device " + device);
localDevice.addRemoteDevice(device);
final RemoteDevice remoteDevice = localDevice.getRemoteDevice(device.getAddress());
remoteDevice.setSegmentationSupported(Segmentation.segmentedBoth);
new Thread(new Runnable() {
#Override
public void run() {
try {
try {
DiscoveryUtils.getExtendedDeviceInformation(localDevice, remoteDevice);
} catch (BACnetException e) {
e.printStackTrace();
}
System.out.println(remoteDevice.getName() + " " + remoteDevice.getVendorName() + " " + remoteDevice.getModelName() + " " + remoteDevice.getAddress() + " " + remoteDevice.getProtocolRevision() + " " + remoteDevice.getProtocolVersion());
ReadPropertyAck ack = localDevice.send(remoteDevice, new ReadPropertyRequest(remoteDevice.getObjectIdentifier(), PropertyIdentifier.objectList)).get();
SequenceOf<ObjectIdentifier> value = ack.getValue();
for (ObjectIdentifier id : value) {
List<ReadAccessSpecification> specs = new ArrayList<ReadAccessSpecification>();
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.presentValue));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.units));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.objectName));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.description));
specs.add(new ReadAccessSpecification(id, PropertyIdentifier.objectType));
ReadPropertyMultipleRequest multipleRequest = new ReadPropertyMultipleRequest(new SequenceOf<ReadAccessSpecification>(specs));
ReadPropertyMultipleAck send = localDevice.send(remoteDevice, multipleRequest).get();
SequenceOf<ReadAccessResult> readAccessResults = send.getListOfReadAccessResults();
System.out.print(id.getInstanceNumber() + " " + id.getObjectType() + ", ");
for (ReadAccessResult result : readAccessResults) {
for (ReadAccessResult.Result r : result.getListOfResults()) {
System.out.print(r.getReadResult() + ", ");
}
}
System.out.println();
}
ObjectIdentifier mode = new ObjectIdentifier(ObjectType.analogValue, 11);
ServiceFuture send = localDevice.send(remoteDevice, new WritePropertyRequest(mode, PropertyIdentifier.presentValue, null, new Real(2), null));
System.out.println(send.getClass());
System.out.println(send.get().getClass());
} catch (ErrorAPDUException e) {
System.out.println("Could not read value " + e.getApdu().getError() + " " + e);
} catch (BACnetException e) {
e.printStackTrace();
}
}
}).start();
}
#Override
public void iHaveReceived(RemoteDevice device, RemoteObject object) {
System.out.println("Value reported " + device + " " + object);
}
});
localDevice.initialize();
localDevice.sendGlobalBroadcast(new WhoIsRequest());
List<RemoteDevice> remoteDevices = localDevice.getRemoteDevices();
for (RemoteDevice device : remoteDevices) {
System.out.println("Remote dev " + device);
}
System.in.read();
localDevice.terminate();
}
}
But i am getting this error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.serotonin.bacnet4j.npdu.Network.<clinit>(Network.java:43)
at bacnet_4_j_test.Bacnet_4_j_test.main(Bacnet_4_j_test.java:43)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
If anybody has any tutorial please provide.
You are missing slf4j dependency. Add slf4j to your class path and it should work.
I am trying to send message from java to GSM Modem like wavecom,
and I found this post : Adding a progress bar on SMSlib sendmessage using Swing (netbeans)
When I run sendMessage class I am getting this exception:
java.io.IOException: write error
at com.sun.comm.Win32SerialPort.write(Win32SerialPort.java:677)
at com.sun.comm.Win32SerialOutputStream.write(Win32SerialOutputStream.java:38)
at org.smslib.modem.SerialModemDriver.write(SerialModemDriver.java:166)
at org.smslib.modem.AModemDriver.write(AModemDriver.java:302)
at org.smslib.modem.athandler.ATHandler_Wavecom.done(ATHandler_Wavecom.java:52)
at org.smslib.modem.ModemGateway.stopGateway(ModemGateway.java:201)
at org.smslib.Service.stopService(Service.java:355)
at org.smslib.Service.startService(Service.java:234)
at org.smslib.Service.startService(Service.java:196)
at consoledmt.SendMessage.sendMessage(SendMessage.java:42)
at consoledmt.SendMessage.main(SendMessage.java:86)
Here is the code sir:
import java.net.URL;
import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.OutboundWapSIMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;
public class SendMessage {
public void sendMessage() throws Exception {
OutboundNotification outboundNotification = new OutboundNotification();
System.out.println("Sample of Send message from a serial gsm modem.");
System.out.println(Library.getLibraryDescription());
System.out.println("Version: " + Library.getLibraryVersion());
SerialModemGateway gateway = new SerialModemGateway("modem.com16", "COM16", 115200, "Wavecom", "M1206B");
gateway.setInbound(false);
gateway.setOutbound(true);
// gateway.setSimPin("");
Service.getInstance().setOutboundMessageNotification(
outboundNotification);
Service.getInstance().addGateway(gateway);
Service.getInstance().startService();
System.out.println();
System.out.println("Modem Information:");
System.out.println(" Manufacturer: " + gateway.getManufacturer());
System.out.println(" Model: " + gateway.getModel());
System.out.println(" Serial No: " + gateway.getSerialNo());
System.out.println(" SIM IMSI: " + gateway.getImsi());
System.out.println(" Signal Level: " + gateway.getSignalLevel()+ " dBm");
System.out.println(" Battery Level: " + gateway.getBatteryLevel()+ "%");
// Send a message synchronously.
OutboundMessage msg = new OutboundMessage("+6283806120618",
"SMS test: sample message from StackOverflow");
Service srvice = Service.getInstance();
// Service.getInstance().sendMessage(msg);
System.out.println(msg);
// Or, send out a WAP SI message.
OutboundWapSIMessage wapMsg = new OutboundWapSIMessage("+6283806120618",
new URL("https://stackoverflow.com/"),
"WAP test: sample message from StackOverflow!");
// gateway.setFrom("chandpriyankara");
// wapMsg.setFrom("chandpriyankara");
srvice.queueMessage(wapMsg);
Service.getInstance().stopService();
}
/**
* Outbound Message informations handler
*
* #author chandpriyankara
*
*/
public class OutboundNotification implements IOutboundMessageNotification {
public void process(AGateway gateway, OutboundMessage msg) {
System.out.println("Outbound handler called from Gateway: "+ gateway.getGatewayId());
System.out.println(msg);
}
}
public static void main(String args[]) {
SendMessage app = new SendMessage();
try {
app.sendMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am planning to do a java onvif application. I have created a new project and generated sources from devicemgmt.wsdl.Also generated the classes from remote discovery.wsdl.
How can I discover a device in a network using theses generated classes?
Thanks for any help.
devicemgmt.wsdl is not related to discovery process, the ONVIF discovery process is based on http://specs.xmlsoap.org/ws/2005/04/discovery it use SOAP over UDP.
If you are using apache-cxf, this can be achieve using
org.apache.cxf.ws.discovery.WSDiscoveryClient
A simple sample code could be :
import java.util.List;
import javax.xml.ws.EndpointReference;
import org.apache.cxf.ws.discovery.WSDiscoveryClient;
public class Main
{
public static void main(String[] args)
{
WSDiscoveryClient client = new WSDiscoveryClient();
client.setVersion10(); // use WS-discovery 1.0
client.setDefaultProbeTimeout(1000); // timeout 1s
System.out.println("Probe:" + client.getAddress());
List<EndpointReference> references = client.probe();
System.out.println("Nb answsers:" + references.size());
for (EndpointReference ref : references)
{
System.out.println(ref.toString());
}
}
}
I had the same problem, CXF is simply to big, please check my approach: JavaWsDiscovery at https://github.com/thhart/javaWsDiscovery.
It uses a simple network probe as suggested by Onvif standards to be able to identify any devices on your local network, following line will return you all available devices:
final Collection urls = DeviceDiscovery.discoverWsDevicesAsUrls("^http$", ".onvif.");
Simple and complete example pure Java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
/**
*
* #author Ronald
*/
public class TestDiscoveryPureJava {
public static void main(String cor[]) throws SocketException{
discoverWsDevices();
}
public static void discoverWsDevices() throws SocketException {
final int WS_DISCOVERY_PORT = 3702;
final String WS_DISCOVERY_ADDRESS_IPv4 = "239.255.255.250";
Thread thread = new Thread() {
#Override
public void run() {
final String probe = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">\n" +
" <s:Header>\n" +
" <a:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</a:Action>\n" +
" <a:MessageID>uuid:f0ded492-301a-4891-882b-cb2d7cac2e45</a:MessageID>\n" +
" <a:ReplyTo>\n" +
" <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>\n" +
" </a:ReplyTo>\n" +
" <a:To s:mustUnderstand=\"1\">urn:schemas-xmlsoap-org:ws:2005:04:discovery</a:To>\n" +
" </s:Header>\n" +
" <s:Body>\n" +
" <Probe xmlns=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\">\n" +
" <d:Types xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:dp0=\"http://www.onvif.org/ver10/network/wsdl\">dp0:Device</d:Types>\n" +
" </Probe>\n" +
" </s:Body>\n" +
"</s:Envelope>";
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket();
datagramSocket.setBroadcast(true);
datagramSocket.setSoTimeout(9000);
} catch (SocketException e) {
System.out.println( "In discoverWsDevices datagram socket exception" + datagramSocket);
e.printStackTrace();
}
byte[] soapMessageByteArray = probe.getBytes();
DatagramPacket datagramPacketSend = null;
try {
datagramPacketSend = new DatagramPacket(
soapMessageByteArray,
soapMessageByteArray.length,
InetAddress.getByName(WS_DISCOVERY_ADDRESS_IPv4),
WS_DISCOVERY_PORT);
} catch (UnknownHostException e) {
System.out.println("Unknown host in send packet");
e.printStackTrace();
}
try {
System.out.println("Send package");
datagramSocket.send(datagramPacketSend);
System.out.println("package sent");
} catch (IOException e) {
System.out.println("In discoverWsDevices datagram socket IOException send " + datagramSocket);
e.printStackTrace();
}
System.out.println("Sending data");
System.out.println(datagramPacketSend.getAddress().getHostName()+":"+WS_DISCOVERY_PORT);
List<ByteArrayInputStream> probeMatches = new ArrayList<>();
while (true) {
byte[] responseMessageByteArray = new byte[9000];
DatagramPacket datagramPacketRecieve = new DatagramPacket(responseMessageByteArray,responseMessageByteArray.length);
try {
System.out.println("Waiting response...");
datagramSocket.receive(datagramPacketRecieve);
} catch (SocketTimeoutException e) {
datagramSocket.close();
System.out.println("In discoverWsDevices datagram socket timeout exception");
break;
} catch (IOException e) {
System.out.println("In discoverWsDevices datagram socket ioexception");
e.printStackTrace();
break;
}
probeMatches.add(new ByteArrayInputStream(datagramPacketRecieve.getData(), 0, datagramPacketRecieve.getLength()));
}
for (ByteArrayInputStream input : probeMatches) {
byte[] bytes = new byte[input.available()];
input.read(bytes, 0, input.available());
String stream = new String(bytes);
System.out.println("stream" + stream);
}
}
};
thread.start();
}
}
How to implement SCTP protocol between a gateway and a server with java ?
If your target is Java 7 never try to implement it. As Andrew and Tom stated its already implemented as a core feature.
No need, just use sctp:
https://github.com/RestComm/sctp
Handles most needs, works great.
Windows cannot support SCTP. if want to does with windows plz install sctp driver.Other wise use linux am adding simple client server example
Client.java
package mai;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import com.sun.nio.sctp.MessageInfo;
import com.sun.nio.sctp.SctpChannel;
public class Client
{
public static void main(String[] args)
{
try {
//SocketAddress socketAddress = new InetSocketAddress( 6050);
InetSocketAddress socketAddress = new InetSocketAddress("192.9.200.193", 4444);
System.out.println("open connection for socket [" + socketAddress + "]");
SctpChannel sctpChannel = SctpChannel.open(socketAddress, 1,1); //(socketAddress, 1 ,1 );
sctpChannel.bind(new InetSocketAddress(4444)); //6060
sctpChannel.connect(socketAddress, 1 ,1);
System.out.println("sctpChannel.getRemoteAddresses() = " + sctpChannel.getRemoteAddresses());
System.out.println("sctpChannel.getAllLocalAddresses() = " + sctpChannel.getAllLocalAddresses());
System.out.println("sctpChannel.isConnectionPending() = " + sctpChannel.isConnectionPending());
System.out.println("sctpChannel.isOpen() = " + sctpChannel.isOpen());
System.out.println("sctpChannel.isRegistered() = " + sctpChannel.isRegistered());
System.out.println("sctpChannel.provider() = " + sctpChannel.provider());
System.out.println("sctpChannel.association() = " + sctpChannel.association());
System.out.println("send bytes");
final ByteBuffer byteBuffer = ByteBuffer.allocate(64000);
//Simple M3ua ASP_Up message
byte [] message = new byte []{1,0,3,1,0,0,0,24,0,17,0,8,0,0,0,1,0,4,0,8,84,101,115,116};
final MessageInfo messageInfo = MessageInfo.createOutgoing(null, 0);
System.out.println("messageInfo = " + messageInfo);
System.out.println("messageInfo.streamNumber() = " + messageInfo.streamNumber());
byteBuffer.put(message);
byteBuffer.flip();
sctpChannel.send(byteBuffer, messageInfo);
System.out.println("close connection");
sctpChannel.close();
System.in.read();
} catch (Exception e) {
e.printStackTrace();
try {
System.in.read();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
Server.java
package mai;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;
import com.sun.nio.sctp.MessageInfo;
import com.sun.nio.sctp.SctpChannel;
import com.sun.nio.sctp.SctpServerChannel;
public class Server {
static ByteBuffer rxBuffer;
public static void main(String[] args)throws Exception {
rxBuffer = ByteBuffer.allocateDirect(64000);
rxBuffer.clear();
rxBuffer.rewind();
rxBuffer.flip();
// SctpChannel xx=SctpChannel.open();
com.sun.nio.sctp.SctpChannel sc = com.sun.nio.sctp.SctpChannel.open();
SocketAddress serverSocketAddress = new InetSocketAddress(4444);
System.out.println("create and bind for sctp address");
SctpServerChannel sctpServerChannel = SctpServerChannel.open().bind(serverSocketAddress);
System.out.println("address bind process finished successfully");
SctpChannel sctpChannel;
while ((sctpChannel = sctpServerChannel.accept()) != null) {
System.out.println("client connection received");
System.out.println("sctpChannel.getRemoteAddresses() = " + sctpChannel.getRemoteAddresses());
System.out.println("sctpChannel.association() = " + sctpChannel.association());
MessageInfo messageInfo = sctpChannel.receive(rxBuffer=ByteBuffer.allocateDirect(64000) , null, null);
int len= messageInfo.bytes();
System.out.println("Server... Total bytes recived "+len);
System.out.println("Server... "+messageInfo);
rxBuffer.flip();
byte[] data = new byte[len];
rxBuffer.get(data);
rxBuffer.clear();
System.out.println("Server..... data "+Arrays.toString(data));
System.out.println("Server..... close connection");
}
}
}