UDP Socket: java.net.SocketException: socket closed - java

MessageCreator: Encapsulate and resolve ports and device unique identifiers.
public class MessageCreator {
private static final String HEADER_PORT = "to port:";
private static final String HEADER_SN = "My sn:";
public static String buildWithPort(int port) {
return HEADER_PORT + port;
}
public static int parsePort(String data) {
if (data.startsWith(HEADER_PORT)) {
return Integer.parseInt(data.substring(HEADER_PORT.length()));
}
return -1;
}
public static String buildWithSn(String sn) {
return HEADER_SN + sn;
}
public static String parseSn(String data) {
if (data.startsWith(HEADER_SN)) {
return data.substring(HEADER_SN.length());
}
return null;
}
}
UdpProvider: Loop to listen to a specific port, then parse the received data, determine whether the data conforms to the predetermined format, get the sender's response port from it, and respond with the uniquely identified UUID value to the UDP searcher.
public class UdpProvider {
public static void main(String[] args) throws IOException {
String sn = UUID.randomUUID().toString();
Provider provider = new Provider(sn);
provider.start();
// Warning: Result of 'InputStream.read()' is ignored
System.in.read();
provider.exit();
}
private static class Provider extends Thread {
private DatagramSocket ds = null;
private boolean done = false;
private final String sn;
public Provider(String sn) {
super();
this.sn = sn;
}
#Override
public void run() {
super.run();
try {
ds = new DatagramSocket(20000);
while (!done) {
final byte[] buf = new byte[512];
DatagramPacket receivePak = new DatagramPacket(buf, buf.length);
ds.receive(receivePak);
String ip = receivePak.getAddress().getHostAddress();
int port = receivePak.getPort();
byte[] receivePakData = receivePak.getData();
String receiveData = new String(receivePakData, 0, /*receivePakData.length*/receivePak.getLength());
System.out.println("received from -> ip: " + ip + ", port: " + port + ", data: " + receiveData);
int responsePort = MessageCreator.parsePort(receiveData.trim());
if (responsePort != -1) {
String responseData = MessageCreator.buildWithSn(sn);
byte[] bytes = responseData.getBytes(StandardCharsets.UTF_8);
DatagramPacket responsePak = new DatagramPacket(bytes, bytes.length,
/*InetAddress.getLocalHost()*/
receivePak.getAddress(),
responsePort);
ds.send(responsePak);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
}
public void exit() {
done = true;
close();
}
public void close() {
if (ds != null) {
ds.close();
ds = null;
}
}
}
}
UdpSearcher: Listening to a specific port and sending a LAN broadcast, sending a broadcast sets the listening port in the data, so you need to turn on listening first to finish before you can send a broadcast, and once you receive the response data, you can parse the device information
public class UdpSearcher {
private static final int LISTENER_PORT = 30000;
public static void main(String[] args) throws IOException, InterruptedException {
Listener listener = listen();
sendBroadcast();
// Warning: Result of 'InputStream.read()' is ignored
System.in.read();
List<Device> deviceList = listener.getDevicesAndClose();
for (Device device : deviceList) {
System.out.println(device);
}
}
public static void sendBroadcast() throws IOException {
DatagramSocket ds = new DatagramSocket();
String sendData = MessageCreator.buildWithPort(LISTENER_PORT);
byte[] sendDataBytes = sendData.getBytes(StandardCharsets.UTF_8);
DatagramPacket sendPak = new DatagramPacket(sendDataBytes, sendDataBytes.length);
sendPak.setAddress(InetAddress.getByName("255.255.255.255"));
sendPak.setPort(20000);
ds.send(sendPak);
ds.close();
}
public static Listener listen() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
Listener listener = new Listener(LISTENER_PORT, countDownLatch);
listener.start();
countDownLatch.await();
return listener;
}
private static class Listener extends Thread {
private final int listenPort;
private DatagramSocket ds = null;
private boolean done = false;
private final CountDownLatch countDownLatch;
private List<Device> devices = new ArrayList<>();
public Listener(int listenPort, CountDownLatch countDownLatch) {
super();
this.listenPort = listenPort;
this.countDownLatch = countDownLatch;
}
#Override
public void run() {
super.run();
countDownLatch.countDown();
try {
ds = new DatagramSocket(listenPort);
while (!done) {
final byte[] buf = new byte[512];
DatagramPacket receivePak = new DatagramPacket(buf, buf.length);
ds.receive(receivePak);
String ip = receivePak.getAddress().getHostAddress();
int port = receivePak.getPort();
byte[] data = receivePak.getData();
String receiveData = new String(data, 0, /*data.length*/receivePak.getLength());
String sn = MessageCreator.parseSn(receiveData);
System.out.println("received from -> ip: " + ip + ", port: " + port + ", data: " + receiveData);
if (sn != null) {
Device device = new Device(ip, port, sn);
devices.add(device);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
}
public void close() {
if (ds != null) {
ds.close();
ds = null;
}
}
public List<Device> getDevicesAndClose() {
done = true;
close();
return devices;
}
}
private static class Device {
private String ip;
private int port;
private String sn;
public Device(String ip, int port, String sn) {
this.ip = ip;
this.port = port;
this.sn = sn;
}
#Override
public String toString() {
return "Device{" +
"ip='" + ip + '\'' +
", port=" + port +
", sn='" + sn + '\'' +
'}';
}
}
}
UdpProvider and UdpSearcher worked fine and printed corrresponding data until I input a char sequence from keyboard follwed by pressing Enter key on each console window, both threw an exception on this line ds.receive(receivePak); :

Related

SMPP java server connect to SMSC or SMPP Client can receive SMS via SMPP?

i have 3rd party smpp connection.
so i created java smpp client and its working fine.
how i can create smpp server connect to 3rd party smpp connection and need to send sms via that created smpp server. my problem is i doesn't know how to connect smpp server to smsc/smpp 3rd party connection
please help to me connect this smpp server to smpp 3rd party server for send sms..
public class SMPPServerSimulator extends ServerResponseDeliveryAdapter implements Runnable, ServerMessageReceiverListener {
private static final Logger log = LoggerFactory.getLogger(SMPPServerSimulator.class);
private static final String QUERYSM_NOT_IMPLEMENTED = "query_sm not implemented";
private static final String CANCELSM_NOT_IMPLEMENTED = "cancel_sm not implemented";
private static final String DATASM_NOT_IMPLEMENTED = "data_sm not implemented";
private static final String REPLACESM_NOT_IMPLEMENTED = "replace_sm not implemented";
private static final String BROADCASTSM_NOT_IMPLEMENTED = "broadcast_sm not implemented";
private static final String CANCELBROADCASTSM_NOT_IMPLEMENTED = "cancel_broadcast_sm not implemented";
private static final String QUERYBROADCASTSM_NOT_IMPLEMENTED = "query_broadcast_sm not implemented";
private static final Integer DEFAULT_PORT = 2775;
private static final String DEFAULT_SYSID = "twtest";
private static final String DEFAULT_PASSWORD = "test123";
private static final String SMSC_SYSTEMID = "sys";
private final ExecutorService execService = Executors.newFixedThreadPool(5);
private final ExecutorService execServiceDelReceipt = Executors.newFixedThreadPool(100);
private final MessageIDGenerator messageIDGenerator = new RandomMessageIDGenerator();
private final boolean useSsl;
private final int port;
private final String systemId;
private final String password;
public SMPPServerSimulator(boolean useSsl, int port, String systemId, String password) {
this.useSsl = useSsl;
this.port = port;
this.systemId = systemId;
this.password = password;
}
#Override
public void run() {
boolean running = true;
/*
* for SSL use the SSLServerSocketConnectionFactory() or DefaultSSLServerSocketConnectionFactory()
*/
try (SMPPServerSessionListener sessionListener = useSsl ?
new SMPPServerSessionListener(port, new KeyStoreSSLServerSocketConnectionFactory())
: new SMPPServerSessionListener(port)) {
/*
* for SSL use the SSLServerSocketConnectionFactory() or DefaultSSLServerSocketConnectionFactory()
*/
log.info("Listening on port {}{}", port, useSsl ? " (SSL)" : "");
while (running) {
System.out.println("running");
SMPPServerSession serverSession = sessionListener.accept();
log.info("Accepted connection with session {}", serverSession.getSessionId());
serverSession.setMessageReceiverListener(this);
serverSession.setResponseDeliveryListener(this);
Future<Boolean> bindResult = execService.submit(new WaitBindTask(serverSession, 30000, systemId, password));
try {
boolean bound = bindResult.get();
System.out.println(bound+" sss");
if (bound) {
// Could start deliver_sm to ESME
log.info("The session is now in state {}", serverSession.getSessionState());
serverSession.deliverShortMessage("CMT",
TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.ISDN, "94712320529",
TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.ISDN, "TWTEST",
new ESMClass(0), (byte) 0x00, (byte) 0x00, new RegisteredDelivery(), DataCodings.ZERO,
"Hello Worldxxxxxxxxxxx".getBytes());
}
} catch (InterruptedException e){
log.info("Interrupted WaitBind task: {}", e.getMessage());
Thread.currentThread().interrupt();
running = false;
} catch (ExecutionException e){
log.info("Exception on execute WaitBind task: {}", e.getMessage());
running = false;
} catch (NegativeResponseException | ResponseTimeoutException | PDUException | InvalidResponseException e){
log.info("Could not send deliver_sm: {}", e.getMessage());
}
}
} catch (IOException e) {
log.error("IO error occurred", e);
}
}
#Override
public QuerySmResult onAcceptQuerySm(QuerySm querySm, SMPPServerSession source) throws ProcessRequestException {
log.info("QuerySm not implemented");
throw new ProcessRequestException(QUERYSM_NOT_IMPLEMENTED, SMPPConstant.STAT_ESME_RINVCMDID);
}
#Override
public SubmitSmResult onAcceptSubmitSm(SubmitSm submitSm,
SMPPServerSession source) throws ProcessRequestException {
MessageId messageId = messageIDGenerator.newMessageId();
log.info("Receiving submit_sm '{}', and return message id {}", new String(submitSm.getShortMessage()), messageId);
if (SMSCDeliveryReceipt.FAILURE.containedIn(submitSm.getRegisteredDelivery()) || SMSCDeliveryReceipt.SUCCESS_FAILURE.containedIn(submitSm.getRegisteredDelivery())) {
execServiceDelReceipt.execute(new DeliveryReceiptTask(source, submitSm, messageId));
}
/*
* SMPP 5.0 allows the following optional parameters (SMPP 5.0 paragraph 4.2.5):
* additional_status_info_text, delivery_failure_reason, dpf_result, network_error_code
* Add the congestionState for SMPP 5.0 connections.
*/
if (source.getInterfaceVersion().value() >= InterfaceVersion.IF_50.value()) {
final int congestionRatio = source.getCongestionRatio();
OptionalParameter.Congestion_state congestionState = new OptionalParameter.Congestion_state((byte) congestionRatio);
return new SubmitSmResult(messageId, new OptionalParameter[]{ congestionState });
}
return new SubmitSmResult(messageId, new OptionalParameter[0]);
}
#Override
public void onSubmitSmRespSent(SubmitSmResult submitSmResult,
SMPPServerSession source) {
log.debug("submit_sm_resp with message_id {} has been sent", submitSmResult.getMessageId());
}
#Override
public SubmitMultiResult onAcceptSubmitMulti(SubmitMulti submitMulti, SMPPServerSession source)
throws ProcessRequestException {
MessageId messageId = messageIDGenerator.newMessageId();
log.debug("Receiving submit_multi_sm '{}', and return message id {}",
new String(submitMulti.getShortMessage()), messageId);
if (SMSCDeliveryReceipt.FAILURE.containedIn(submitMulti.getRegisteredDelivery())
|| SMSCDeliveryReceipt.SUCCESS_FAILURE.containedIn(submitMulti.getRegisteredDelivery())) {
execServiceDelReceipt.execute(new DeliveryReceiptTask(source, submitMulti, messageId));
}
/*
* SMPP 5.0 allows the following optional parameters (SMPP 5.0 paragraph 4.2.5):
* additional_status_info_text, delivery_failure_reason, dpf_result, network_error_code
* Add the congestionState for SMPP 5.0 connections.
*/
if (source.getInterfaceVersion().value() >= InterfaceVersion.IF_50.value()) {
final int congestionRatio = source.getCongestionRatio();
OptionalParameter.Congestion_state congestionState = new OptionalParameter.Congestion_state((byte) congestionRatio);
return new SubmitMultiResult(messageId.getValue(), new UnsuccessDelivery[0], new OptionalParameter[]{ congestionState });
}
return new SubmitMultiResult(messageId.getValue(), new UnsuccessDelivery[0], new OptionalParameter[0]);
}
#Override
public DataSmResult onAcceptDataSm(DataSm dataSm, Session source)
throws ProcessRequestException {
log.info("Accepting data_sm, but not implemented");
throw new ProcessRequestException(DATASM_NOT_IMPLEMENTED, SMPPConstant.STAT_ESME_RSYSERR);
}
#Override
public void onAcceptCancelSm(CancelSm cancelSm, SMPPServerSession source)
throws ProcessRequestException {
log.info("Accepting cancel_sm, but not implemented");
throw new ProcessRequestException(CANCELSM_NOT_IMPLEMENTED, SMPPConstant.STAT_ESME_RCANCELFAIL);
}
#Override
public void onAcceptReplaceSm(ReplaceSm replaceSm, SMPPServerSession source)
throws ProcessRequestException {
log.info("Accepting replace_sm, but not implemented");
throw new ProcessRequestException(REPLACESM_NOT_IMPLEMENTED, SMPPConstant.STAT_ESME_RREPLACEFAIL);
}
#Override
public BroadcastSmResult onAcceptBroadcastSm(final BroadcastSm broadcastSm, final SMPPServerSession source)
throws ProcessRequestException {
MessageId messageId = messageIDGenerator.newMessageId();
log.debug("Receiving broadcast_sm '{}', and return message id {}",
new String(broadcastSm.getOptionalParameter(OptionalParameter.Tag.MESSAGE_PAYLOAD).serialize()), messageId);
return new BroadcastSmResult(messageId, new OptionalParameter[0]);
}
#Override
public void onAcceptCancelBroadcastSm(final CancelBroadcastSm cancelBroadcastSm, final SMPPServerSession source)
throws ProcessRequestException {
log.info("Accepting cancel_broadcast_sm, but not implemented");
throw new ProcessRequestException(CANCELBROADCASTSM_NOT_IMPLEMENTED, SMPPConstant.STAT_ESME_RBCASTCANCELFAIL);
}
#Override
public QueryBroadcastSmResult onAcceptQueryBroadcastSm(final QueryBroadcastSm queryBroadcastSm,
final SMPPServerSession source) throws ProcessRequestException {
log.info("Accepting query_broadcast_sm, but not implemented");
throw new ProcessRequestException(QUERYBROADCASTSM_NOT_IMPLEMENTED, SMPPConstant.STAT_ESME_RBCASTQUERYFAIL);
}
private static class WaitBindTask implements Callable<Boolean> {
private final SMPPServerSession serverSession;
private final long timeout;
private final String systemId;
private final String password;
public WaitBindTask(SMPPServerSession serverSession, long timeout, String systemId, String password) {
this.serverSession = serverSession;
this.timeout = timeout;
this.systemId = systemId;
this.password = password;
}
#Override
public Boolean call() {
try {
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
BindRequest bindRequest = serverSession.waitForBind(timeout);
try {
if (BindType.BIND_TRX.equals(bindRequest.getBindType())) {
if (systemId.equals(bindRequest.getSystemId())) {
if (password.equals(bindRequest.getPassword())) {
log.info("Accepting bind for session {}, interface version {}", serverSession.getSessionId(), bindRequest.getInterfaceVersion());
serverSession.setInterfaceVersion(InterfaceVersion.IF_50.min(bindRequest.getInterfaceVersion()));
// The systemId identifies the SMSC to the ESME.
bindRequest.accept(SMSC_SYSTEMID, InterfaceVersion.IF_50);
return true;
} else {
log.info("Rejecting bind for session {}, interface version {}, invalid password", serverSession.getSessionId(), bindRequest.getInterfaceVersion());
bindRequest.reject(SMPPConstant.STAT_ESME_RINVPASWD);
}
} else {
log.info("Rejecting bind for session {}, interface version {}, invalid system id", serverSession.getSessionId(), bindRequest.getInterfaceVersion());
bindRequest.reject(SMPPConstant.STAT_ESME_RINVSYSID);
}
} else {
log.info("Rejecting bind for session {}, interface version {}, only accept transceiver", serverSession.getSessionId(), bindRequest.getInterfaceVersion());
bindRequest.reject(SMPPConstant.STAT_ESME_RBINDFAIL);
}
} catch (PDUStringException e) {
log.error("Invalid system id: " + SMSC_SYSTEMID, e);
bindRequest.reject(SMPPConstant.STAT_ESME_RSYSERR);
}
} catch (IllegalStateException e) {
log.error("System error", e);
} catch (TimeoutException e) {
log.warn("Wait for bind has reach timeout", e);
} catch (IOException e) {
log.error("Failed accepting bind request for session {}", serverSession.getSessionId());
}
return false;
}
}
private static class DeliveryReceiptTask implements Runnable {
private final SMPPServerSession session;
private final MessageId messageId;
private final TypeOfNumber sourceAddrTon;
private final NumberingPlanIndicator sourceAddrNpi;
private final String sourceAddress;
private final TypeOfNumber destAddrTon;
private final NumberingPlanIndicator destAddrNpi;
private final String destAddress;
private final int totalSubmitted;
private final int totalDelivered;
private final byte[] shortMessage;
public DeliveryReceiptTask(SMPPServerSession session,
SubmitSm submitSm, MessageId messageId) {
this.session = session;
this.messageId = messageId;
// reversing destination to source
sourceAddrTon = TypeOfNumber.valueOf(submitSm.getDestAddrTon());
sourceAddrNpi = NumberingPlanIndicator.valueOf(submitSm.getDestAddrNpi());
sourceAddress = submitSm.getDestAddress();
// reversing source to destination
destAddrTon = TypeOfNumber.valueOf(submitSm.getSourceAddrTon());
destAddrNpi = NumberingPlanIndicator.valueOf(submitSm.getSourceAddrNpi());
destAddress = submitSm.getSourceAddr();
totalSubmitted = totalDelivered = 1;
shortMessage = submitSm.getShortMessage();
}
public DeliveryReceiptTask(SMPPServerSession session,
SubmitMulti submitMulti, MessageId messageId) {
this.session = session;
this.messageId = messageId;
// set to unknown and null, since it was submit_multi
sourceAddrTon = TypeOfNumber.UNKNOWN;
sourceAddrNpi = NumberingPlanIndicator.UNKNOWN;
sourceAddress = null;
// reversing source to destination
destAddrTon = TypeOfNumber.valueOf(submitMulti.getSourceAddrTon());
destAddrNpi = NumberingPlanIndicator.valueOf(submitMulti.getSourceAddrNpi());
destAddress = submitMulti.getSourceAddr();
// distribution list assumed only contains single address
totalSubmitted = totalDelivered = submitMulti.getDestAddresses().length;
shortMessage = submitMulti.getShortMessage();
}
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error("Interrupted", e);
//re-interrupt the current thread
Thread.currentThread().interrupt();
}
SessionState state = session.getSessionState();
if (!state.isReceivable()) {
log.debug("Not sending delivery receipt for message id {} since session state is {}", messageId, state);
return;
}
String stringValue = Integer.valueOf(messageId.getValue(), 16).toString();
try {
DeliveryReceipt delRec = new DeliveryReceipt(stringValue, totalSubmitted, totalDelivered, new Date(), new Date(), DeliveryReceiptState.DELIVRD, "000", new String(shortMessage));
session.deliverShortMessage(
"mc",
sourceAddrTon, sourceAddrNpi, sourceAddress,
destAddrTon, destAddrNpi, destAddress,
new ESMClass(MessageMode.DEFAULT, MessageType.SMSC_DEL_RECEIPT, GSMSpecificFeature.DEFAULT),
(byte)0,
(byte)0,
new RegisteredDelivery(0),
DataCodings.ZERO,
delRec.toString().getBytes());
log.debug("Sending delivery receipt for message id {}: {}", messageId, stringValue);
} catch (Exception e) {
log.error("Failed sending delivery_receipt for message id " + messageId + ":" + stringValue, e);
}
}
}
public static void main(String[] args) {
// System.setProperty("javax.net.debug", "ssl");
/*
* To use SSL, add -Djsmpp.simulator.ssl=true
* To debug SSL, add -Djavax.net.debug=ssl
*/
String systemId = System.getProperty("jsmpp.client.systemId", DEFAULT_SYSID);
String password = System.getProperty("jsmpp.client.password", DEFAULT_PASSWORD);
int port;
try {
port = Integer.parseInt(System.getProperty("jsmpp.simulator.port", DEFAULT_PORT.toString()));
} catch (NumberFormatException e) {
port = DEFAULT_PORT;
}
boolean useSsl = Boolean.parseBoolean(System.getProperty("jsmpp.simulator.ssl", "false"));
SMPPServerSimulator smppServerSim = new SMPPServerSimulator(useSsl, port, systemId, password);
smppServerSim.run();
}
}

failed to open stream: HTTP request failed using phpjavabridge

i have problem when i use php java bridge i got error like this
Warning: require_once(http://localhost:8080/JavaBridge/java/Java.inc): failed to open stream: HTTP request failed! in /Library/WebServer/Documents/test2.php on line 3
Fatal error: require_once(): Failed opening required 'http://localhost:8080/JavaBridge/java/Java.inc' (include_path='.:') in /Library/WebServer/Documents/test2.php on line 3
my tomcat has running at port 8080. i use force to use port 8080 but i still got error
here my standalone class for java bridge
package php.java.bridge;
import php.java.bridge.util.Thread;
import java.lang.reflect.Method;
import php.java.bridge.http.JavaBridgeRunner;
import php.java.bridge.util.Logger;
import java.io.File;
import javax.swing.Icon;
import java.awt.Component;
import javax.swing.JOptionPane;
import java.net.ServerSocket;
import java.io.IOException;
import php.java.bridge.http.TCPServerSocket;
import php.java.bridge.http.ISocketFactory;
public class Standalone
{
public static final int HTTP_PORT_BASE = 8080;
public static final int HTTPS_PORT_BASE = 8443;
public static ISocketFactory bind(final int logLevel, final String sockname) throws IOException {
ISocketFactory socket = null;
socket = TCPServerSocket.create(sockname, 20);
if (null == socket) {
throw new IOException("Could not create socket: " + sockname);
}
return socket;
}
protected static void disclaimer() {
System.err.println("Copyright (C) 2003, 2006 Jost Boekemeier and others.");
System.err.println("This is free software; see the source for copying conditions. There is NO");
System.err.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
}
protected void javaUsage() {
System.err.println("PHP/Java Bridge version " + Util.VERSION);
disclaimer();
System.err.println("Usage: java -jar JavaBridge.jar [SOCKETNAME LOGLEVEL LOGFILE]");
System.err.println("SOCKETNAME is one of INET_LOCAL, INET, HTTP_LOCAL, HTTP, HTTPS_LOCAL, HTTPS");
System.err.println("");
System.err.println("Example 1: java -jar JavaBridge.jar");
System.err.println("Example 2: java -jar JavaBridge.jar HTTP_LOCAL:8080 3 JavaBridge.log");
System.err.println("Example 3: java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=YOURPASSWD -jar JavaBridge.jar HTTPS:8443 3 JavaBridge.log");
System.err.println("The certificate for example 3 can be created with e.g.: jdk1.6.0/bin/keytool -keystore mySrvKeystore -genkey -keyalg RSA");
System.err.println("");
System.err.println("Influential system properties: threads, daemon, php_exec, default_log_file, default_log_level, base.");
System.err.println("Example: java -Djava.awt.headless=\"true\" -Dphp.java.bridge.threads=50 -Dphp.java.bridge.base=/usr/lib/php/modules -Dphp.java.bridge.php_exec=/usr/local/bin/php-cgi -Dphp.java.bridge.default_log_file= -Dphp.java.bridge.default_log_level=5 -jar JavaBridge.jar");
System.err.println("Example: java -Dphp.java.bridge.daemon=\"true\" -jar JavaBridge.jar");
}
protected void usage() {
this.javaUsage();
System.exit(1);
}
protected void checkOption(final String[] s) {
if ("--version".equals(s[0])) {
System.out.println(Util.VERSION);
System.exit(0);
}
this.usage();
}
private static boolean testPort(final int port) {
try {
final ServerSocket sock = new ServerSocket(port);
sock.close();
return true;
}
catch (IOException ex) {
return false;
}
}
private static int findFreePort(final int start) {
for (int port = start; port < start + 100; ++port) {
if (testPort(port)) {
return port;
}
}
return start;
}
public void init(final String[] s) {
String sockname = null;
int logLevel = -1;
// final String tcpSocketName = "9267";
final String tcpSocketName = "8080";
if (s.length > 3) {
this.checkOption(s);
}
try {
if (s.length > 0) {
sockname = s[0];
if (sockname.startsWith("-")) {
this.checkOption(s);
}
}
try {
if (s.length > 1) {
logLevel = Integer.parseInt(s[1]);
}
}
catch (NumberFormatException e2) {
this.usage();
}
catch (Throwable t) {
t.printStackTrace();
}
if (s.length == 0) {
try {
/* final int tcpSocket = Integer.parseInt(tcpSocketName);
final int freeJavaPort = findFreePort(tcpSocket);
final int freeHttpPort = findFreePort(8080);
final int freeHttpsPort = findFreePort(8443);
final Object result = JOptionPane.showInputDialog(null, "Start a socket listener on port", "Starting the PHP/Java Bridge ...", 3, null, new String[] { "INET_LOCAL:" + freeJavaPort, "INET:" + freeJavaPort, "HTTP_LOCAL:" + freeHttpPort, "HTTP:" + freeHttpPort, "HTTPS_LOCAL:" + freeHttpsPort, "HTTPS:" + freeHttpsPort }, "HTTP_LOCAL:" + freeHttpPort);
//final Object result = 8080;
if (result == null) {
System.exit(0);
}*/
//sockname = result.toString();
sockname = "8080";
}
catch (Throwable t2) {}
}
if (s.length == 0) {
TCPServerSocket.TCP_PORT_BASE = Integer.parseInt(tcpSocketName);
}
if (checkServlet(logLevel, sockname, s)) {
return;
}
final ISocketFactory socket = bind(logLevel, sockname);
if ("true".equals(System.getProperty("php.java.bridge.test.startup"))) {
System.exit(0);
}
JavaBridge.initLog(String.valueOf(socket), logLevel, s);
JavaBridge.init(socket, logLevel, s);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public static File getCanonicalWindowsFile(final String path) {
try {
return new File(path).getCanonicalFile();
}
catch (IOException e) {
return new File(path);
}
}
private static boolean checkServlet(final int logLevel, String sockname, final String[] s) throws InterruptedException, IOException {
if (sockname == null) {
return false;
}
if (sockname.startsWith("SERVLET_LOCAL:") || sockname.startsWith("HTTP_LOCAL:") || sockname.startsWith("HTTPS_LOCAL:")) {
Util.JAVABRIDGE_PROMISCUOUS = false;
System.setProperty("php.java.bridge.promiscuous", "false");
}
else {
if (!sockname.startsWith("SERVLET:") && !sockname.startsWith("HTTP:") && !sockname.startsWith("HTTPS:")) {
return false;
}
Util.JAVABRIDGE_PROMISCUOUS = true;
System.setProperty("php.java.bridge.promiscuous", "true");
}
final boolean isSecure = sockname.startsWith("HTTPS");
JavaBridge.initLog(sockname, logLevel, s);
sockname = sockname.substring(sockname.indexOf(58) + 1);
final String serverPort = (Util.JAVABRIDGE_PROMISCUOUS ? "INET:" : "INET_LOCAL:") + sockname;
Logger.logMessage("JavaBridgeRunner started on port " + serverPort);
Class runner = JavaBridgeRunner.class;
JavaBridgeRunner r;
try {
runner = Util.classForName("php.java.script.JavaBridgeScriptRunner");
final Method m = runner.getMethod("getRequiredInstance", String.class, Boolean.TYPE);
r = (JavaBridgeRunner)m.invoke(runner, serverPort, new Boolean(isSecure));
}
catch (Throwable e) {
r = JavaBridgeRunner.getRequiredInstance(serverPort, isSecure);
}
r.waitFor();
r.destroy();
return true;
}
private static final boolean checkGNUVM() {
try {
return "libgcj".equals(System.getProperty("gnu.classpath.vm.shortname"));
}
catch (Throwable t) {
return false;
}
}
public static void main(final String[] s) {
if (!System.getProperty("php.java.bridge.daemon", "false").equals("false")) {
final String[] args = new String[s.length + 8];
args[0] = System.getProperty("php.java.bridge.daemon");
if ("true".equals(args[0])) {
args[0] = "java";
}
args[1] = "-Djava.library.path=" + System.getProperty("java.library.path", ".");
args[2] = "-Djava.ext.dirs=" + System.getProperty("java.ext.dirs", ".");
args[3] = "-Djava.awt.headless=" + System.getProperty("java.awt.headless", "true");
args[4] = "-Dphp.java.bridge.asDaemon=true";
args[5] = "-classpath";
args[6] = System.getProperty("java.class.path", ".");
args[7] = "php.java.bridge.Standalone";
for (int j = 0; j < s.length; ++j) {
args[j + 8] = s[j];
}
try {
System.in.close();
System.out.close();
System.err.close();
}
catch (IOException e) {
System.exit(12);
}
new Thread(new Runnable() {
#Override
public void run() {
try {
Runtime.getRuntime().exec(args);
}
catch (IOException e) {
System.exit(13);
}
}
}).start();
try {
java.lang.Thread.sleep(20000L);
}
catch (Throwable t2) {}
System.exit(0);
}
try {
System.loadLibrary("natcJavaBridge");
}
catch (Throwable t3) {}
try {
final String cp = System.getProperty("java.class.path", ".");
File jbFile = null;
final boolean isExecutableJavaBridgeJar = cp.indexOf(File.pathSeparatorChar) == -1 && cp.endsWith("JavaBridge.jar") && (jbFile = new File(cp)).isAbsolute();
final File wd = getCanonicalWindowsFile(isExecutableJavaBridgeJar ? jbFile.getParent() : "");
final boolean sunJavaInstalled = new File("/usr/java/default/bin/java").exists();
final String javaExec = sunJavaInstalled ? "/usr/java/default/bin/java" : "java";
if (s.length == 0 && System.getProperty("php.java.bridge.exec_sun_vm", "true").equals("true") && ((sunJavaInstalled && checkGNUVM()) || isExecutableJavaBridgeJar)) {
final Process p = Runtime.getRuntime().exec(new String[] { javaExec, "-Dphp.java.bridge.exec_sun_vm=false", "-classpath", cp, "php.java.bridge.Standalone" }, null, wd);
if (p != null) {
System.exit(p.waitFor());
}
}
}
catch (Throwable t4) {}
try {
new Standalone().init(s);
}
catch (Throwable t) {
t.printStackTrace();
System.exit(9);
}
}
}
in this source code i force manual choice 8080 using this code
/* final int tcpSocket = Integer.parseInt(tcpSocketName);
final int freeJavaPort = findFreePort(tcpSocket);
final int freeHttpPort = findFreePort(8080);
final int freeHttpsPort = findFreePort(8443);
final Object result = JOptionPane.showInputDialog(null, "Start a socket listener on port", "Starting the PHP/Java Bridge ...", 3, null, new String[] { "INET_LOCAL:" + freeJavaPort, "INET:" + freeJavaPort, "HTTP_LOCAL:" + freeHttpPort, "HTTP:" + freeHttpPort, "HTTPS_LOCAL:" + freeHttpsPort, "HTTPS:" + freeHttpsPort }, "HTTP_LOCAL:" + freeHttpPort);
//final Object result = 8080;
if (result == null) {
System.exit(0);
}*/
//sockname = result.toString();
sockname = "8080";
}
and at tcpServer i force to choice that port use this code
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.InetAddress;
import java.io.IOException;
import java.net.ServerSocket;
public class TCPServerSocket implements ISocketFactory
{
public static int TCP_PORT_BASE;
private ServerSocket sock;
private int port;
boolean local;
public static ISocketFactory create(String name, final int backlog) throws IOException {
boolean local = false;
if (name == null) {
throw new NullPointerException("name");
}
if (name.startsWith("INET:")) {
name = name.substring(5);
}
else if (name.startsWith("INET_LOCAL:")) {
local = true;
name = name.substring(11);
}
final int p = Integer.parseInt(name);
final TCPServerSocket s = new TCPServerSocket(p, backlog, local);
return s;
}
private ServerSocket newServerSocket(final int port, final int backlog) throws IOException {
try {
if (this.local) {
return new ServerSocket(port, backlog, InetAddress.getByName("127.0.0.1"));
}
}
catch (UnknownHostException ex) {}
return new ServerSocket(port, backlog);
}
private void findFreePort(final int start, final int backlog) {
int port = start;
while (port < start + 100) {
try {
this.sock = this.newServerSocket(port, backlog);
this.port = port;
return;
}
catch (IOException e) {
++port;
continue;
}
}
}
private TCPServerSocket(final int port, final int backlog, final boolean local) throws IOException {
this.local = local;
if (port == 0) {
this.findFreePort(TCPServerSocket.TCP_PORT_BASE, backlog);
}
else {
this.sock = this.newServerSocket(port, backlog);
this.port = port;
}
}
#Override
public void close() throws IOException {
this.sock.close();
}
#Override
public Socket accept() throws IOException {
final Socket s = this.sock.accept();
s.setTcpNoDelay(true);
return s;
}
#Override
public String getSocketName() {
return String.valueOf(this.port);
}
#Override
public String toString() {
return (this.local ? "INET_LOCAL:" : "INET:") + this.getSocketName();
}
static {
// TCPServerSocket.TCP_PORT_BASE = 9267;
TCPServerSocket.TCP_PORT_BASE = 8080;
}
}
but my javabridge cannot open stream at that port why like that ? any idea for choice only port 8080 not choice the other ?
this problem close after i change this code
try { /*
final int tcpSocket = Integer.parseInt(tcpSocketName);
final int freeJavaPort = findFreePort(tcpSocket);
final int freeHttpPort = findFreePort(8080);
final int freeHttpsPort = findFreePort(8443);
final Object result = JOptionPane.showInputDialog(null, "Start a socket listener on port", "Starting the PHP/Java Bridge ...", 3, null, new String[] { "INET_LOCAL:" + freeJavaPort, "INET:" + freeJavaPort, "HTTP_LOCAL:" + freeHttpPort, "HTTP:" + freeHttpPort, "HTTPS_LOCAL:" + freeHttpsPort, "HTTPS:" + freeHttpsPort }, "HTTP_LOCAL:" + freeHttpPort);
//final Object result = 8080;
if (result == null) {
System.exit(0);
}*/
//sockname = result.toString();
sockname = "HTTP_LOCAL:8080";
System.out.println("sockname"+sockname);
}
so sockname use "HTTP_LOCAL:8080" not "8080"

Have problem with understanding threads and changing variable in multiple threads

I am trying to restrict the amount of clients on my server,so i just sending message from my server when it is full and print it in my client by ending process for him.But as i use multiple threads i cant stop my writeMessage thread
Tried AtomicBoolean,but i didnt work
public class Client {
private static final Logger LOGGER = LogManager.getLogger();
private BufferedReader consoleReader;
private String name;
private String address;
private int port;
private Thread writeMessage, readMessage;
private ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
private LocalTime dTime = zonedDateTime.toLocalTime();
private Net net;
private AtomicBoolean running = new AtomicBoolean(true);
public Client(String address, int port) {
this.address = address;
this.port = port;
net = new Net(address, port);
consoleReader = new BufferedReader(new InputStreamReader(System.in));
printName();
readMessage();
writeMessage();
}
private void printName() {
System.out.println("Print your name:");
try {
name = consoleReader.readLine();
while (NameValidator.nameIsValid(name)) {
System.out.println("Name should contain more than 1 letter");
name = consoleReader.readLine();
}
net.send(name + "\n");
} catch (IOException e) {
LOGGER.error(e);
}
}
private void readMessage() {
readMessage = new Thread(() -> {
String str = net.receive();
while (net.isConnected()) {
if ("full".equals(str)) {
System.out.println("server is full");
running.set(false);
break;
} else {
System.out.println(str);
str = net.receive();
}
}
net.offService();
}, "readMessage");
readMessage.start();
}
private void writeMessage() {
writeMessage = new Thread(() -> {
while (running.get()) {
String userWord;
try {
String time = dTime.getHour() + ":" + dTime.getMinute() + ":" + dTime.getSecond();
userWord = consoleReader.readLine();
if (userWord.equals("quit")) {
net.send(userWord + "\n");
ClientAmountGenerator.decrement();
running.set(false);
} else {
net.send("(" + time + ") " + name + " says : " + userWord + "\n");
}
} catch (IOException e) {
LOGGER.error(e);
}
}
net.offService();
}, "writeMessage");
writeMessage.start();
}
}
I want to change running to "false",so the writeMessage thread wont work if it gets message "full" from the server

Read rcon command response

I want to send rcon command to server using java, to do this I'm using the following library https://github.com/Kronos666/rkon-core
When i run command like this
Rcon rcon = new Rcon("127.0.0.1", 27015, "mypassword".getBytes());
// Example: On a minecraft server this will list the connected players
String result = rcon.command("list");
// Display the result in the console
System.out.println(result);
My server show response in console Gc connection established from... and so on
but in java app i have the empty result, it's not null, it's just empty
String result = rcon.command("list");
How can i take response from server using rcon protocol?
Try this:
try {
Rcon rcon = new Rcon("127.0.0.1", 27015, "mypassword".getBytes());
String result = rcon.command("list");
System.out.println(result);
} catch (AuthenticationException e) {
String result = "Authentication failed";
}
Finally I write my own implementation:
public final class RconClient implements AutoCloseable {
private static final int MAX_SIZE = 4096;
private final Socket socket;
private final RconData data;
private static final Logger LOG = LoggerFactory.getLogger(RconClient.class);
#SuppressWarnings("ConstructorShouldNotThrowExceptions")
public RconClient(final String host,
final int port,
final byte[] password) throws IOException {
this.socket = new Socket(host, port);
final RconData requst = request(new RconData(RconData.AUTH, password));
if (requst.getId() == -1) {
LOG.error("Wrong password or ip to connect to rcon");
throw new LoginException(host, port);
}
this.data = read();
}
public String command(String command) throws IOException {
command = "get5_status";
final RconData response = request(new RconData(command.getBytes()));
return new String(response.getPayload(), Charset.forName("UTF-8"));
}
public RconData request(RconData packet) throws IOException {
try {
write(packet);
return read();
} catch (final SocketException exception) {
socket.close();
throw exception;
}
}
private void write(RconData packet) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(packet.getPayload().length + 14);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(packet.getPayload().length + 10);
buffer.putInt(packet.getId());
buffer.putInt(packet.getType());
buffer.put(packet.getPayload());
buffer.put((byte)0);
buffer.put((byte)0);
socket.getOutputStream().write(buffer.array());
socket.getOutputStream().flush();
}
private RconData read() throws IOException {
byte[] packet = new byte[MAX_SIZE];
int packetSize = this.socket.getInputStream().read(packet);
ByteBuffer buffer = ByteBuffer.wrap(packet, 0, packetSize);
buffer.order(ByteOrder.LITTLE_ENDIAN);
if (buffer.remaining() < 4) {
throw new WrongPacketException();
}
int size = buffer.getInt();
if (buffer.remaining() < size) {
throw new WrongPacketException();
}
int id = buffer.getInt();
int type = buffer.getInt();
byte[] payload = new byte[size - 10];
buffer.get(payload);
buffer.get(new byte[2]);
return new RconData(id, type, payload);
}
#Override
public void close() throws IOException {
this.socket.close();
}
}
Where RconData it's simple POJO with byte[] password property,

java client and C# server working with unicode

I'm trying to build a tcp client and a server that will work with unicode. the server is in C# and looks like that:
static void Main(string[] args)
{
// tcp setup
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
// waiting for client to connect
clientSocket = serverSocket.AcceptTcpClient();
// client comunication
string resAscii = Recv(clientSocket);
Send(clientSocket, "got from you: " + resAscii);
string resUnicode = Recv(clientSocket);
Send(clientSocket, "קיבלתי ממך: " + resUnicode);
Console.ReadKey();
}
public static void Send(TcpClient client, string msg)
{
Byte[] sendtBytes = Encoding.Unicode.GetBytes(msg + "$");
client.GetStream().Write(sendtBytes, 0, sendtBytes.Length);
client.GetStream().Flush();
}
public static string Recv(TcpClient client)
{
byte[] recvBytes = new byte[65537];
client.GetStream().Read(recvBytes, 0, (int)client.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.Unicode.GetString(recvBytes);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf('$'));
return dataFromClient;
}
and the client is in java (android):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
#Override
public void run() {
try {
Socket s = new Socket("192.168.0.102", 8888);
Send(s, "this is ascii");
String asciiString = Recv(s);
Send(s, "זה יוניקוד");
String unicodeString = Recv(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private String Recv(Socket s) throws IOException {
byte[] b = new byte[10080];
int read = s.getInputStream().read(b, 0, 1000);
String ret = new String(b, "UTF16");
ret = ret.substring(0, ret.indexOf('$'));
return ret;
}
private void Send(Socket s, String msg) throws IOException {
s.getOutputStream().write((msg + "$").getBytes("UTF16"));
}
When I'm sending from the client to the server I get the message. But when the server sending to the client I get only Chinese letters:
client (android) error
how can I fix it?
As #flakes said that was the answer.
change this line in the Recv function:
String ret = new String(b, "UTF16");
with this line:
String ret = new String(b, StandardCharsets.UTF_16LE);
and
s.getOutputStream().write((msg + "$").getBytes("UTF16"));
with this line in the Send file:
s.getOutputStream().write((msg + "$").getBytes(StandardCharsets.UTF_16LE));

Categories