Spring TCP Client - PullRequest
       9

Spring TCP Client

0 голосов
/ 24 января 2019

Пытаюсь подключиться к серверу Java TCP от клиента Spring TCP.Отправить запрос от Spring TCP Client.Java TCP Server для отправки ответа Spring Spring Client.Код сервера:

public static void main(String args[]) throws IOException, ClassNotFoundException
    {
        server = new ServerSocket(port);
        while (true)
        {
            Socket socket = server.accept();
            InputStream ois = socket.getInputStream();
            System.out.println(new Timestamp(System.currentTimeMillis()));
            String message = convert(ois, Charset.defaultCharset());

            System.out.println("Message Received: " + message);
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write("Hello World");
            ois.close();
            os.close();
            socket.close();
            if (message.equalsIgnoreCase("exit"))
                break;
        }
        System.out.println("Shutting down Socket server!!");
        server.close();
    }

    public static String convert(InputStream inputStream, Charset charset) throws IOException
    {

        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset)))
        {
            return br.lines().collect(Collectors.joining(System.lineSeparator()));
        }
    }

XML-клиент Spring XML:

<int:channel id="input" />
<int:channel id="output" />
<int-ip:tcp-outbound-gateway id="outgateway" request-channel="input" reply-channel="clientBytes2StringChannel" connection-factory="client" reply-timeout="5000000"
    request-timeout="500000"
></int-ip:tcp-outbound-gateway>
<int:object-to-string-transformer id="clientBytes2String" input-channel="clientBytes2StringChannel" />

Код TCP-клиента Spring:

 public class EchoService
    {

        public String receive(String input)
        {
            return "echo :" + input;
        }
    }

public interface SimpleGateway
{

    public String send(String text);

}


public class Main
{

    public static void main(String args[])
    {

        final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("Client.xml");

        SimpleGateway gateway = applicationContext.getBean(SimpleGateway.class);

        try
        {

            System.out.println("Waiting for Server to Accept Connections");

            String reply = null;

            reply = gateway.send("hello");
            System.out.println("My Reply" + reply);

        }
        catch (MessagingException exc)
        {

            System.out.println("Exception occurred : Timed out --" + exc);
        }

    }

}

Получение ошибки нижеиз TCP Client

failedMessage=GenericMessage [payload=hello, headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@1b4c3a9, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@1b4c3a9, id=7c8a9096-c08a-ed6b-b6d6-9874a66bd036, timestamp=1548304570325}]

Сокет сервера занимает больше времени для обработки сообщения.Таким образом, клиент закрывает сокет сервера соединений, pgm выдает следующую ошибку

Exception in thread "main" java.net.SocketException: Socket is closed
    at java.net.Socket.getOutputStream(Unknown Source)
    at com.tcs.bancs.CMI.incomingInterfaces.TestPgm.main(TestPgm.java:42)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...