How to setup Apache Thrift communication between a Java client and a PHP server?
I have PHP codes on server side:
$header('Content-Type', 'application/x-thrift');
$handler = new MyApplicationHandler();
$processor = new \tutorial\MyApplicationProcessor($handler);
$transport = new TFramedTransport(
new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TBinaryProtocol($transport, true, true);
$transport->open();
$processor->process($protocol, $protocol);
$transport->close();
And Java codes on the client side:
THttpClient httpClient =
new THttpClient("http://my.application.com/PhpServer.php");
TTransport transport = new TFramedTransport(httpClient);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
MyApplication.Client client = new MyApplication.Client(protocol);
Boolean result = client.someApi(someData); // <-- will crash here
transport.close();
The client will crash when executing this line:
client.someApi(someData);
Is there something wrong in my codes?
Related
So basically I'm trying to communicate between a Java client and a NodeJS server. Java sends a message via a socket, Node receives it using an event listener, then Node tries to send a response to Java. Something like "OK" because the message was received.
Java (in Main.java)
try {
Socket s = new Socket("localhost", 6666);
// Send a message to the server
OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8);
out.write("test");
out.flush();
// Receive a message from the server
InputStream input = s.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
System.out.println(line);
} catch (Exception e) {
System.out.println(e);
}
NodeJS
var net = require('net');
var server = net.createServer(function (connection) {
console.log("Client connected");
connection.on('data', function (data) {
console.log('Request from', connection.remoteAddress, 'port', connection.remotePort);
console.log(data.toString())
connection.write("OK");
});
})
server.listen(6666, () => console.log(`Server is listening...`));
The server displays:
Server is listening...
Client connected
Request from ::ffff:127.0.0.1 port 65025
test
but the client is empty, it doesn't get server's response.
I can't figue out where the problem is. I found countless examples for two-way socket communications, but most of them between a client/server written both for Java/NodeJS. Their code was still similar with what I wrote, but doesn't work. Thanks a lot!
I am calling an api to send sms with a java apache common client, and it looks like setting a time out for more than 0 is returning a "java.net.SocketException: Connection reset" error.
Here is a the code sample
// creating the http client
HttpClient client = new HttpClient();
Setting a connection time out of 10 sec (I receive a successful response
as soon as I remove this part of the code)
client.getHttpConnectionManager().
getParams().setConnectionTimeout(10000);
Rest of the code
//creating the request method
GetMethod method = new GetMethod(smsUrl);
// setting its params
method.setQueryString(new NameValuePair[] {
new NameValuePair("username", user),
new NameValuePair("password", pass),
new NameValuePair("action", "sendsms"),
new NameValuePair("from", "Woosh"),
new NameValuePair("to", toMobile),
new NameValuePair("text", textBody)
});
//calling the method
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
int statusCode = client.executeMethod(method);
The issue was sorted out, it was an issue caused by the server of the api I was calling. Not sure what was it though
I'm trying to send an message from Java client to C++ server using ProtoBuf over socket. My program hangs while i am trying to create InputStream. Thanks in advance if some one could help me on this and here is my part of client code in java:
String host = "xxxxxxxxx";
int port = xxxx;
Builder builder = CarSelection.Car.newBuilder();
builder.setLabel("Audi");
builder.setValue("A6");
Car car = builder.build();
Socket client = new Socket(host, port);
byte[] result = car.toByteArray() ;
car.writeDelimitedTo(client.getOutputStream());
Car recieveData= car.parseDelimitedFrom(client.getInputStream());
It is able to write to output stream but my program hangs when trying to read from input stream.
To create the certificates(pfx for C#, jks for Java) I followed this guide
C# client:
X509Certificate cert = new X509Certificate2(certPath, "pass");
TStreamTransport socket = new TTLSSocket(host, port, cert, (o, c, chain, errors) => true, null);
var transport = new TBufferedTransport(socket);
TProtocol protocol = new TBinaryProtocol(transport);
_client = new HfmConnectorService.Client(protocol);
_client.InputProtocol.Transport.Open();
Java Server:
TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters();
File keystoreFile = new File(System.getProperty("user.dir") + "\\keystore\\hfmcon.jks");
if(!keystoreFile.exists())
throw new IOException("Keystore file missing");
params.setKeyStore(keystoreFile.getPath(), "pass");
TServerSocket serverTransport = TSSLTransportFactory.getServerSocket(port, 10000, InetAddress.getByName("localhost"), params);
The client times out on this line
_client.InputProtocol.Transport.Open();
I am making a program to connect to FTP Server using FTPCLient class of apache commons net API. here is code:
FTPClient client = new FTPClient();
byte[] b = new byte[4];
b[ 0] = new Integer(127).byteValue();
b[ 1] = new Integer(0).byteValue();
b[ 2] = new Integer(0).byteValue();
b[ 3] = new Integer(1).byteValue();
try{
InetAddress address = InetAddress.getByAddress(b);
client.connect(address,22);
}
.....
I get the exception at connect line().
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Try with this
FTPClient f = new FTPClient();
f.connect(server);
f.login(username, password);
FTPFile[] files = listFiles(directory);
Note: port 22 is used for SSH,sftp not for ftp
If its sftp then you need to go for commons-vfs