I'm using the lightweight server provided in oracle java since java 1.6 and I'm using just to receive POST responses from another server.
Now for 2 or 3 responses it run well but when the load is increased I'm not receiving all the responses from the server, some of them are ignored.
I tried to increase the backlog to 1500 but it's the same thing.
here's how I create the server :
public class HttpServerZipWhip {
private final String SERVER_IP_ADDRESS = "X.X.X.X";
private final Integer SERVER_PORT = 9333;
private final short SERVER_BACKLOG = 1500;
private final String SERVER_CONTEXT = "/message/receive";
/**
* Constructor : Server initiation
* #param muc
* #param smshandle
*/
public HttpServerClass(Muc muc, SMSHandling_impl smshandle){
//Server Initialization
InetSocketAddress addr = new InetSocketAddress(SERVER_IP_ADDRESS, SERVER_PORT);
HttpServer server = null;
try {
server = HttpServer.create(addr, SERVER_BACKLOG);
} catch (IOException e) {
e.printStackTrace();
}
server.createContext(SERVER_CONTEXT, new MyHandler(muc, smshandle));
server.setExecutor(Executors.newCachedThreadPool());
server.start();
}
}
Related
I have a class that downloads files from FTP servers using the Apache FTP Client, and I want to test it. For doing so, I decided to use the FakeFtpServer class from MockFtpServer, but it always fails with the same error: Connection refused (Connection refused).
My test class:
public class FTPConnectionTest {
/** Mock FTP Server */
private static FakeFtpServer FAKE_FTP_SERVER;
/** Mock FTP Server address */
private static String ADDRESS = "localhost";
/** Mock FTP server user name */
private static String USERNAME = "ftpuser";
/** Mock FTP Server password */
private static String PASSWORD = "ftppasswd";
/** Mock FTP Server default root */
private static String ROOT;
/** Mock FTP Server port */
private static int PORT = 0;
/** Test directory in the mock server */
private static String DIRECTORY = "/directory";
/** Test file 1*/
private static String FILE1 = "/Human_satellite.txt";
/** Content of test file 1 */
private static String FILE1_CONTENT = "Ground control to Major Tom...";
/** Test file 1*/
private static String FILE2 = "/wall-e.txt";
/** Content of test file 1 */
private static String FILE2_CONTENT = "If lost in space, use fire extinguisher";
/** Test file 1*/
private static String FILE3 = "/buzz_lightyear.txt";
/** Content of test file 1 */
private static String FILE3_CONTENT = "To infinity, and beyond !";
/**
* Set up a mock FTP server before running the tests
*/
#BeforeClass
public static void setupMock() {
// Create Mock server
FAKE_FTP_SERVER = new FakeFtpServer();
FAKE_FTP_SERVER.setServerControlPort(0); // Automatically finds available port
PORT = FAKE_FTP_SERVER.getServerControlPort();
FAKE_FTP_SERVER.addUserAccount(new UserAccount(USERNAME, PASSWORD, ROOT));
// Get the path to the resources folder where to run the mock FTP
File mockDir = new File("src/test/resources/ftp/mock");
ROOT = mockDir.getAbsolutePath();
// Create mock files
FileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new DirectoryEntry(ROOT));
fileSystem.add(new FileEntry(ROOT + FILE1, FILE1_CONTENT));
fileSystem.add(new DirectoryEntry(ROOT + DIRECTORY));
fileSystem.add(new FileEntry(ROOT + DIRECTORY + FILE2, FILE2_CONTENT));
fileSystem.add(new FileEntry(ROOT + DIRECTORY + FILE3, FILE3_CONTENT));
FAKE_FTP_SERVER.setFileSystem(fileSystem);
FAKE_FTP_SERVER.start();
}
/**
* The the mock FTP Server once the tests are done
*/
#AfterClass
public static void stop() {
FAKE_FTP_SERVER.stop();
}
/**
* Test
*/
#Test
public void testFetchNewFiles () {
// Get output path
String outputPath = "src/test/resources/ftp/result";
File output = new File(outputPath);
outputPath = output.getAbsolutePath();
// Create the connection and get the files
FTPConnection conn = new FTPConnection(ADDRESS, PORT, USERNAME, PASSWORD, outputPath);
conn.fetchNewFiles();
// Check that the files have bin downloaded
File file1 = new File(outputPath + FILE1);
assertTrue(file1.exists());
}
}
And here is the part of the FTP class that fails:
public class FTPConnection {
/** Logger */
private Logger logger;
/** The FTP Client used to connect to the server */
private FTPClient client;
/** The address of the server */
private String server;
/** The port of the server (default 21) */
private int port;
/** The user name to use to connect to the server */
private String user;
/** The password to use to connect to the server */
private String password;
/** The directory where to save the downloaded files */
private String output;
/**
* Constructor
* #param server the address of the FTP server
* #param port the port of the FTP server
* #param user the user name to use to connect to the server
* #param password the password to use to connect to the server
* #param output the output directory where to download the files
*/
public FTPConnection (String server, int port, String user, String password, String output) {
this.logger = LoggerFactory.getLogger(FTPConnection.class);
this.server = server;
this.port = port;
this.user = user;
this.password = password;
this.output = output;
this.client = new FTPClient();
}
/**
* Constructor
* #param server the address of the FTP server
* #param user the user name to use to connect to the server
* #param password the password to use to connect to the server
* #param output the output directory where to download the files
*/
public FTPConnection (String server, String user, String password, String output) {
this(server, 21, user, password, output);
}
public void fetchNewFiles() {
// Connect to the server
try {
this.client.connect(server, port); // That's the line that fails
this.client.login(user, password);
} catch (IOException e) {
logger.error("Error while connecting to FTP server '" + this.server + "': " + e.getMessage());
e.printStackTrace();
return;
}
}
}
And finally an extract of what's going on in the console:
19:24:31.417 [Thread-1] INFO org.mockftpserver.fake.FakeFtpServer - Starting the server on port 0
19:24:31.419 [Thread-1] INFO org.mockftpserver.fake.FakeFtpServer - Actual server port is 34003
19:24:31.444 [main] ERROR com.my.project.importHandler.FTPConnection - Error while connecting to FTP server 'localhost': Connection refused (Connection refused)
*** STACK TRACE ***
19:24:31.457 [Thread-1] DEBUG org.mockftpserver.fake.FakeFtpServer - Cleaning up server...
19:24:31.457 [Thread-1] INFO org.mockftpserver.fake.FakeFtpServer - Server stopped.
Ok, I found it: FAKE_FTP_SERVER.setServerControlPort(0); sets the value of the server port to 0, and the automatic selection of an available port only happens during FAKE_FTP_SERVER.start();. I moved the line PORT = FAKE_FTP_SERVER.getServerControlPort(); after FAKE_FTP_SERVER.start(); and now it works.
I am trying to create a socket connection between a .Net server application and Java Client Application.
I am getting an error from the java client application:
Connection refused: connect
Notes:
Communicating with a .Net Client Application, works fine.
I have disables the windows firewall
Undoubtedly, I am running the server application in the background and then I am running the client application
Following are my server code (C#):
public class Server
{
public Server()
{
CreateListener();
}
public void CreateListener()
{
// Create an instance of the TcpListener class.
TcpListener tcpListener = null;
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
string output;
try
{
// Set the listener on the local IP address
// and specify the port.
tcpListener = new TcpListener(ipAddress, 13);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
}
}
and client application code (Java):
public class smtpClient {
public void Send() {
Socket smtpSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
try {
smtpSocket = new Socket("localhost", 13); // FAILURE
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new DataInputStream(smtpSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname");
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
It fails at the following line in the Java Client Application:
smtpSocket = new Socket("localhost", 13);
I can't tell what is the issue you are facing, but you need to start with a solid foundation to discover these issues.
As a rule of thumb, you should always write one piece (typically the server) first and verify connectivity (say using telnet) and then write the other piece (typically client) and verify its connectivity.
I always keep a Standard Client and Server handy to test whether its my code or its the environment/configuration.
Below is a sample code that works fine to test connectivity.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class ClientServer {
static void Main() {
new Thread(() => { StartServer("localhost", 5013); }).Start();
Thread.Sleep(100);
Console.WriteLine("\nPress enter to start the client...");
Console.ReadLine();
StartClient("localhost", 5013);
}
public static void StartServer(string serverInterface, int port) {
try {
IPHostEntry hostInfo = Dns.GetHostEntry(serverInterface);
string hostName = hostInfo.HostName;
IPAddress ipAddress = hostInfo.AddressList[0];
var server = new TcpListener(ipAddress, port);
server.Start();
Console.WriteLine($"Waiting for a connection at {server.LocalEndpoint}");
Console.WriteLine("Press ctrl+c to exit server...");
while (true) {
TcpClient client = server.AcceptTcpClient();
Console.WriteLine($"Server says - Client connected: {client.Client.RemoteEndPoint}");
ThreadPool.QueueUserWorkItem((state) => {
using (var _client = (TcpClient)state)
using (NetworkStream stream = _client.GetStream()) {
string msg = stream.ReadAsciiData();
if (msg == "Hello!") {
stream.WriteAsciiData($"Time:{DateTime.Now: yyyy/MM/dd HH:mm zzz}. Server name is {hostName}");
}
}
}, client);
}
} catch (Exception e) {
Console.WriteLine(e);
}
}
public static void StartClient(string serverInterface, int port) {
Console.WriteLine("Client started...");
try {
using (var client = new TcpClient(serverInterface, port))
using (NetworkStream stream = client.GetStream()) {
Console.WriteLine("Client says - Hello!");
stream.Write(Encoding.ASCII.GetBytes("Hello!"));
string msg = stream.ReadAsciiData();
Console.WriteLine($"Client says - Message from server: Server#{client.Client.RemoteEndPoint}: {msg}");
}
} catch (Exception e) {
Console.WriteLine(e);
}
Console.WriteLine("Client exited");
}
}
static class Utils {
public static void WriteAsciiData(this NetworkStream stream, string data) {
stream.Write(Encoding.ASCII.GetBytes(data));
}
public static string ReadAsciiData(this NetworkStream stream) {
var buffer = new byte[1024];
int read = stream.Read(buffer, 0, buffer.Length);
return Encoding.ASCII.GetString(buffer, 0, read);
}
public static void Write(this NetworkStream stream, byte[] data) {
stream.Write(data, 0, data.Length);
}
}
Now to your specific problem,
The choice of port 13, is not ideal for testing. Usually all ports below 1024 are considered privileged. i.e. a firewall or antivirus might block your attempt to listen on that port
Remember that IPV6 addresses plays a role. Your machine might have that enabled or disabled based on your configuration. You want to make sure that if your server is listening on a IPv6 interface, then your client also connects on the same
Which brings us to another related point: Irrespective of you are using IPv6 interface or not, the client needs to connect to the same interface the server is listening on. This might seem obvious, but is often missed. A typical machine
has at-least 2 interfaces: One for localhost (127...* called loopback interface) and another non local (typically 10...* or 192...*, but not restricted to it). It can so happen (especially when you pick the first available interface to bind your server without knowing which one it is) that server might be listening on non loopback interface like say 192.168.1.10 interface and the client might be connecting to 127.0.0.1, and you can see why the client will get "connection refused" errors
The sample code above works and you can test your code with it. You can us telnet for a client or just my sample code. You can play around changing the serverInterface values to some surprising discoveries which are accentuated by
ipAddress = hostInfo.AddressList[0] line
Hope this helps you with your debugging
I am working on JADE (Java) project that connects Matlab by a TCP connection with client-server sockets. Here, JADE creates a server socket and Matlab creates a client socket. I am retrieving some data from Matlab to Java (JADE). The following is my code where I am calling Matlab by JADE through Agent. (1) The issue is I cannot re-run it without re-starting the program again. I believe that I require a multithread java instance with multithread Matlab instance that could connect and synchronize each other. However, I found that Matlab is a single thread. The program throws binding error.
WARNING: Error adding ICP jade.imtp.leap.JICP.JICPPeer#1dbb27d[Cannot bind server socket to localhost port 1099].
jade.core.AgentContainerImpl joinPlatform
SEVERE: Communication failure while joining agent platform: No ICP active
jade.core.IMTPException: No ICP active
I want to run it multiple times without manually re-starting. Here is my JADE code (took help from https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html):
public class MatlabComAgent extends Agent
{
ServerSocket srvr = null;
Socket skt = null;
BufferedReader in;
PrintWriter out;
String ip = "localhost";
String filePath;
int port = 1234;
protected void setup()
{
// Get arguments
Object[] args = getArguments();
filePath = (String) args[0];
// Create the TCP connection
try
{
// Create server and socket
srvr = new ServerSocket(port);
skt = srvr.accept();
// Create writer and reader to send and receive data
out = new PrintWriter(skt.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
}
catch (IOException e)
{
e.printStackTrace();
}
// Send a message to the tester to say its can start sending requests
sendMessage("Tester","","start-now",ACLMessage.INFORM);
// Run behavior
CommWithMatlab commWithMatlab = new CommWithMatlab();
addBehaviour(commWithMatlab);
} // End setup
Code for Matlab connection:
% Create TCP/IP object 't'. Specify server machine and port number.
% Open the connection with the server
t = tcpip('localhost', 1234);
set(t, 'InputBufferSize', 30000);
set(t, 'OutputBufferSize', 30000);
pause(0.1)
fopen(t);
disp('Connection with JADE established')
I found interesting notes on "socket server which allows multiple connections via threads and Java" Creating a socket server which allows multiple connections via threads and Java page, however, I am not able to do it completely what is said here. May be I am missing something here. (2) I am confused should I edit my Matlab code and/or JADE code for multi-threading.
Here is my code that I tried:
protected void setup()
{
// Get arguments
Object[] args = getArguments();
filePath = (String) args[0];
// Create the TCP connection
try
{
srvr = new ServerSocket(port);
Runnable connectionHandler = new ConnectionHandler(skt);
new Thread(connectionHandler).start();
}
catch (IOException e)
{
e.printStackTrace();
}
Here is new ConnectionHandler class:
public class ConnectionHandler implements Runnable {
private Socket sk=null; //initialize in const'r
BufferedReader in;
PrintWriter out;
public ConnectionHandler(ServerSocket skt) throws IOException
{
sk = skt.accept();
out = new PrintWriter(sk.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
}
public void run() {
try
{
// Create writer and reader to send and receive data
out = new PrintWriter(sk.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
But I got some erorr "java.lang.NullPointerException". Can someone help me to properly code it, what I am missing. Also, (3) this run() in ConnectionHandler class will be invoked automatically? I was confused so I create writer and reader inside Connectionhandler class and its run(). Can I simply make my MatlabComAgent class as multithread without adding any new class. I can make my class as
public class MatlabComAgent extends Agent implements Runnable
{....
....
}
Should I also put the following inside ConnectionHandler class?
// Send a message to the tester to say its can start sending requests
sendMessage("Tester","","start-now",ACLMessage.INFORM);
// Run behavior
CommWithMatlab commWithMatlab = new CommWithMatlab();
addBehaviour(commWithMatlab);
Here, CommWithMatlab class extends SimpleBehavior containg required actions that further passes commands from Matlab to PowerWorld (using another connection). One example is like:
class CommWithMatlab extends SimpleBehaviour
{
private static final long serialVersionUID = 8966535884137111965L;
#Override
public void action()
{
// Wait for a message from another agent requesting something
ACLMessage msg = blockingReceive();
// If this is to open a case
if(msg.getConversationId().equals(OPEN_CASE))
{
openCase(msg.getContent());
}
}
I can simple pass arguments to addagent() and can call runJade(). The following are my JADE run functions using agents:
//Runs JADE and starts the initial agents
public static void runJade() throws ControllerException
{
// Launch JADE platform
Runtime rt = Runtime.instance();
Profile p;
p = new ProfileImpl();
cController = rt.createMainContainer(p);
rt.setCloseVM(true);
// Launch Powerworld interface agent
addAgent(PWRWORLD_NAME, PWRWORLD_CLASS, null);
addAgent(PWRWORLD_TESTER_NAME, PWRWORLD_TESTER_CLASS, null);
//addAgent(PWRWORLD_TESTER_NAME2, PWRWORLD_TESTER_CLASS2, null);
}
private static void addAgent(String name, String type, String arg) throws ControllerException
{
Object[] argsObj = {arg};
AgentController ac = cController.createNewAgent(name, type, argsObj);
ac.start();
}
(4) I have a different program that also creates the same connection. When I try to run one program when other is running, it again throws binding error. However, these programs are completely separate. One program uses port 1234 and other 1239. However, system always assign local port to 1099 to both programs, hence throw binding error in this case.
Any help is appreciable!
Unfortunately, it is not possible to use matlabcontrol over a distributed network. I checked.
I have configured one xbee pro as coordinator (API mode) and other as router (API mode). I trying to send data from coordinator to router using xbee java api, but in the router code i keep getting null, am I doing something wrong.
Below is the code for Sending data (coordinator):
public class MainApp {
private static final String PORT = "/dev/ttyUSB0";
private static final int BAUDRATE = 9600;
public static void main(String[] args)
{
String data = "Helloww";
XBeeDevice mycord = new XBeeDevice(PORT, BAUDRATE);
try {
mycord.open();
System.out.println("Port is opened\n");
System.out.println("remote device connection\n");
//mac of my router
RemoteXBeeDevice router = new RemoteXBeeDevice(mycord,
new XBee64BitAddress("0013A20040DD9BDD"));
System.out.println("Sending data\n");
mycord.sendData(router, data.getBytes());
} catch (XBeeException e) {
e.printStackTrace();
mycord.close();
System.exit(1);
}
}
}
code on router side
public class RecvApp {
private static final String PORT = "/dev/ttyUSB1";
private static final int BAUDRATE = 9600;
public static void main(String[] args)
{
XBeeDevice myrouter = new XBeeDevice(PORT, BAUDRATE);
try {
myrouter.open();
System.out.println("router port opened\n");
//mac of coordinator
RemoteXBeeDevice remotecord = new RemoteXBeeDevice(myrouter, new XBee64BitAddress("0013A20040D96FE5"));
XBeeMessage msg = myrouter.readDataFrom(remotecord);
System.out.print(msg);
} catch (XBeeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myrouter.close();
System.exit(1);
}
}
}
On the router you need to have a loop that checks for messages and prints them out. The API should have a method you can call to check for messages before calling readDataFrom() (or maybe you just ignore the null response). Sleep for a few milliseconds between each check. Right now, there isn't much opportunity for your message to come through before the program quits.
When debugging something like this, start by isolating your problem. Which side is failing, the coordinator or the router? Are you sure the XBee modules have joined to each other and are on the same network?
One test would be to run a simple terminal emulator on the serial port connected to the router, do you see any frames coming through? If you look at a hex dump of the bytes, do you see your "Helloww" message? If not, you need to get the coordinator working first before you debug your router.
Found the issue, I was not converting the message received in the correct format. Added the below lines
String content = HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData()));
System.out.println("Hex data" + "" + content + "\n");
String value = new String(xbeeMessage.getData());
System.out.print("Actual msg" + " " + value + "\n");
Works now :)
im using ZeroMQ to client / server application ,
now in my MT server i try to set timeout, i tried to set on the server :
socket.setReceiveTimeOut(2000);
socket.setSendTimeOut(2000);
with no luck , how do i set timeout on response .
this is my multi thread server code this is taken from zeromq examples for mt server :
/*
* Multithreaded Hello World server in Java
*
* #author Vadim Shalts
* #email vshalts#gmail.com
*
*/
import org.zeromq.ZMQ;
import org.zeromq.ZMQQueue;
class mtserver {
static void main(String[] args) {
final ZMQ.Context context = ZMQ.context(1);
ZMQ.Socket clients = context.socket(ZMQ.ROUTER);
clients.bind ("tcp://*:5555");
ZMQ.Socket workers = context.socket(ZMQ.DEALER);
workers.bind ("inproc://workers");
for(int thread_nbr = 0; thread_nbr < 5; thread_nbr++) {
Thread worker_routine = new Thread() {
#Override
public void run() {
ZMQ.Socket socket = context.socket(ZMQ.REP);
socket.connect ("inproc://workers");
while (true) {
// Wait for next request from client (C string)
byte[] request = socket.recv (0);
System.out.println ("Received request: ["+new String(request,0,request.length-1)+"]");
// Do some 'work'
try {
Thread.sleep (1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
// Send reply back to client (C string)
byte[] reply = "World ".getBytes();
reply[reply.length-1] = 0; //Sets the last byte of the reply to 0
socket.send(reply, 0);
}
}
};
worker_routine.start();
}
// Connect work threads to client threads via a queue
ZMQQueue zMQQueue = new ZMQQueue(context,clients, workers);
zMQQueue.run();
// We never get here but clean up anyhow
clients.close();
workers.close();
context.term();
}
}
Why do you need a timeout? The original examples don't use it.
Why do you need a timeout on the server?
A natural place to put a timeout in the client.
socket.setReceiveTimeOut(int milliseconds); works on a client side.