我试图通过Socket从Java Web应用程序中将C ++应用程序作为服务器进行通信。服务器在协议缓冲区中公开二进制API(双方使用2.6.1和Java 8)。在客户端上,我尝试了一个套接字连接池来加速性能。第一次调用总是成功,后续调用超时,迫使Apache Commons Pool破坏套接字连接。所以我写了一个简单的Java服务器和Java客户端来调试正在进行的工作。消息来源如下:
syntax = "proto2";
option java_package = "com.es.protos";
option java_outer_classname = "RequestProtos";
option optimize_for = SPEED;
message Request {
oneof request {
string ping = 3;
string field1 = 4;
string field2 = 5;
string field3 = 6
}
}
syntax = "proto2";
option java_package = "com.es.protos";
option java_outer_classname = "ResponseProtos";
option optimize_for = SPEED;
message Response {
required string status = 1;
oneof response {
string ping = 3;
string field1 = 4;
string field2 = 5;
string field3 = 6
}
}
package com.es.socket;
import com.es.protos.RequestProtos.Request;
import com.es.protos.ResponseProtos.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer1 {
final static Logger LOGGER = LoggerFactory.getLogger(TcpServer1.class.getName());
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0]));
Socket socket = null;
while (true) {
try {
socket = serverSocket.accept();
} catch (IOException e) {
LOGGER.warn("Could not listen on port");
System.exit(-1);
}
Thread thread = new Thread(new ServerConnection1(socket));
thread.start();
}
}
}
class ServerConnection1 implements Runnable {
static final Logger LOGGER = LoggerFactory.getLogger(ServerConnection.class.getName());
private Socket socket = null;
ServerConnection1(Socket socket) {
this.socket = socket;
}
public void run() {
try {
serveRequest(socket.getInputStream(), socket.getOutputStream());
//socket.close();
} catch (IOException ex) {
LOGGER.warn("Error", ex);
}
}
public void serveRequest(InputStream inputStream, OutputStream outputStream) {
try {
read(inputStream);
write(outputStream);
} catch (IOException ex) {
LOGGER.warn("ERROR", ex);
}
}
private void write(OutputStream outputStream) throws IOException {
Response.Builder builder = Response.newBuilder();
Response response = builder.setStatus("SUCCESS").setPing("PING").build();
response.writeDelimitedTo(outputStream);
LOGGER.info("Server sent {}", response.toString());
}
private void read(InputStream inputStream) throws IOException {
Request request = Request.parseDelimitedFrom(inputStream);
LOGGER.info("Server received {}", request.toString());
}
}
package com.es.socket;
import com.es.protos.RequestProtos.Request;
import com.es.protos.ResponseProtos.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.Socket;
public class TcpClient1 {
final static Logger LOGGER = LoggerFactory.getLogger(TcpClient1.class.getName());
private Socket openConnection(final String hostName, final int port) {
Socket clientSocket = null;
try {
clientSocket = new Socket(hostName, port);
} catch (IOException e) {
LOGGER.warn("Exception occured while connecting to server", e);
}
return clientSocket;
}
private void closeConnection(Socket clientSocket) {
try {
LOGGER.info("Closing the connection");
clientSocket.close();
} catch (IOException e) {
LOGGER.warn("Exception occured while closing the connection", e);
}
}
private void write(OutputStream outputStream) throws IOException {
Request.Builder builder = Request.newBuilder();
Request request = builder.setPing("PING").build();
request.writeDelimitedTo(outputStream);
LOGGER.info("Client sent {}", request.toString());
}
private void read(InputStream inputStream) throws IOException {
Response response = Response.parseDelimitedFrom(inputStream);
LOGGER.info("Client received {}", response.toString());
}
public static void main(String args[]) throws Exception {
TcpClient1 client = new TcpClient1();
try {
LOGGER.info("Start - One socket for all calls");
Socket clientSocket = client.openConnection("localhost", Integer.parseInt(args[0]));
OutputStream outputStream = clientSocket.getOutputStream();
InputStream inputStream = clientSocket.getInputStream();
for (int i = 0; i < 2; i++) {
LOGGER.info("REQUEST {}", i);
client.write(outputStream);
client.read(inputStream);
}
client.closeConnection(clientSocket);
LOGGER.info("End - One socket for all calls");
} catch (Exception e) {
LOGGER.warn("Exception occured", e);
System.exit(1);
}
}
}
此处请求和响应是协议缓冲区生成的类。客户端发送一个请求并收到响应。它再次发送另一个请求重用套接字。服务器永远不会收到第二个请求,客户端永远不会收到响应。如何通过同一个套接字传输多个消息。
下面的示例输出
客户端输出
18:09:10.733 [main] INFO c.d.e.socket.TcpClient1 - Start --> One socket for all calls
18:09:10.733 [main] INFO c.d.e.socket.TcpClient1 - REQUEST 0
18:09:10.734 [main] INFO c.d.e.socket.TcpClient1 - Client sent ping: "PING"
18:09:10.734 [main] INFO c.d.e.socket.TcpClient1 - Client received status: "SUCCESS"
ping: "PING"
18:09:10.734 [main] INFO c.d.e.socket.TcpClient1 - REQUEST 1
18:09:10.735 [main] INFO c.d.e.socket.TcpClient1 - Client sent ping: "PING"
服务器输出
18:09:10.734 [Thread-0] INFO c.d.e.socket.ServerConnection - Server received ping: "PING"
18:09:10.734 [Thread-0] INFO c.d.e.socket.ServerConnection - Server sent status: "SUCCESS"
ping: "PING"
谢谢,
AK
答案 0 :(得分:0)
我找到了问题并解决了问题。它是ServerConnection1类中的一个问题。该线程的run方法旨在处理一个请求/响应。在run方法中放置while循环可以处理任意数量的请求/响应。