I'm working on an application that communicates with a ServerSocket
.
I'm using the spring integration's TCP client to connect to the server for sending and receiving messages.
Each part is as following snap code:
Server:
public void startSocketServer(){
try (final ServerSocket serverSocket = new ServerSocket(9992)) {
gl.info("Server is listening on: " + serverSocket.getLocalSocketAddress());
while (true) {
final Socket socket = serverSocket.accept();
gl.info("A new client connected");
new SocketThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private class SocketThread extends Thread {
private final Socket socket;
private final PrintWriter writer;
private final BufferedReader reader;
public SocketThread(Socket socket) throws IOException {
this.socket = socket;
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
reader = new BufferedReader(new InputStreamReader(input));
writer = new PrintWriter(output, true);
}
public void run() {
try {
while (true) {
String inputMessage = reader.readLine();
if (inputMessage != null) {
MessageType messageType = getTypeInstance(inputMessage);
if (messageType instanceof LoginMessage loginMessage) {
if (isAuthenticated(loginMessage.getUsername(), loginMessage.getPassword())) {
gl.info("#### SERVER => User authorized");
final String messageBody = createConnectionAckMessage();
print(writer, messageBody);
} else {
print(writer, createRefusalMessage());
}
} else if (messageType instanceof StartTransferingData startData) {
getMessages().forEach(message-> print(writer, message));
} else if (messageType instanceof DisconnectionAck disAck) {
print(writer, "By then")
break;
}
}
}
socket.close();
} catch (IOException ex) {
gl.info("Server exception: " + ex.getMessage());
}
}
private void print(PrintWriter writer, String msg) {
writer.print(msg);
writer.print("\r\n");
}
}
And Client:
public class CapConfig {
@MessagingGateway(defaultRequestChannel = "toTcp", errorChannel = "errorChannel")
public interface TcpGateway {
@Gateway
void send(String in);
}
@Bean
public MessageChannel toTcp() {
return new DirectChannel();
}
@Bean
public AbstractClientConnectionFactory clientCF() {
return Tcp.netClient("localhost", 9992)
.serializer(TcpCodecs.crlf())
.deserializer(TcpCodecs.crlf())
.get();
}
@Bean
public IntegrationFlow tcpOutFlow(AbstractClientConnectionFactory connectionFactory) {
return IntegrationFlows.from(toTcp())
.handle(Tcp.outboundAdapter(connectionFactory))
.get();
}
@Bean
public IntegrationFlow tcpInFlow(AbstractClientConnectionFactory connectionFactory) {
return IntegrationFlows.from(Tcp.inboundAdapter(connectionFactory))
.transform(stringTransformer)
.log()
//---- Do some other stuffs
.get();
}
}
And the scenario is as following:
The client sends username&password with the gateway and then the server receives the message and authenticates it, if the client authenticated, the server sends connectionAck message to the client to show the connection accepted. Then the client sends startData message to the server to start data transmission.
The problem is:
When the client sends username&pass to the server, and the server sends connectionAck to the client, the client does not receive the message!!!!.
Any help? Thanks in advance.
I just used writer.println(msg)
instead of writer.print(msg)
and the problem solved.
I don't know why but it worked.