Announcement Announcement Module
No announcement yet.
Integration with TCP server (read/write string to socket) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Integration with TCP server (read/write string to socket)


    I want to integrate via Spring integration.
    My standalone program connects to a ip address and port.

    I write String commands to the socket. For example: "S*1,2,3,4;" always ended with the ';' character
    And in a thread I read the incoming commands from server. All incoming commands are als Strings that ends with the ';' character.

    In standalone code like this:

    Sending data to socket:
    String command = "[email protected]@@[email protected][email protected][email protected][email protected];"
    this.socket = new Socket(, this.port); = this.socket.getInputStream();
        this.outputStreamWriter = new OutputStreamWriter(this.socket.getOutputStream());
        this.reader = new BufferedReader(
            new InputStreamReader(, "UTF-8"));
    this.outputStreamWriter.write(command, 0, command.length());
    Reading data from socket in thread:

    String buffer = "";
    int character =;
             String c = String.valueOf((char) character);
            if (!c.equals(";")) {
              buffer = buffer + c;
    } else {
              buffer = buffer + c;
              String command = buffer;
             //do something
             buffer = "";
    Now I want to integrate this functionallity in my spring appliction.
    I can use this:

    But I don't found an example how to write my custom class to write and read the socket.
    Can someone help me with that?


  • #2
    That blog is a little out of date; see the documentation and the tcp sample

    All you need is a custom serializer/deserializer. For this application, it might be as simple as subclassing ByteArraySingleTerminatorSerializer, in the same way that does, but using ";" instead of 0x0a.

    Note: this is available in the upcoming 2.2 release (currently at release candidate 2 - 2.2.0.RC2).

    For earlier versions, you'll need to clone the code.


    • #3
      Ok thanks!
      So I add this configuration to my application-context.xml

      	<int-ip:tcp-connection-factory id="client"
      		type="client" host="localhost" port="5555" single-use="true"
      		so-timeout="10000" />
      		id="outboundClient" channel="input" connection-factory="client" />
      	<int:channel id="input" />
      This means that when a message received on channel "input", it will open a socket to localhost port 5555 and write the message to that socket. yes?

      I use MessagingTemplate like this:

      MessagingTemplate messagingTemplate = new MessagingTemplate();
          messagingTemplate.send("input", new GenericMessage<String>(command));
      I got this exception:

      Caused by: java.lang.IllegalStateException: No 'channelResolver' specified for MessagingTemplate. Unable to invoke methods with a channel name argument.
      	at org.springframework.util.Assert.state(
      	at org.springframework.integration.core.MessagingTemplate.getRequiredChannelResolver(
      	at org.springframework.integration.core.MessagingTemplate.resolveChannelName(
      	at org.springframework.integration.core.MessagingTemplate.send(
      	at nl.eleqtron.qvisual.spring.service.GiaBusServiceImpl.sendCommand(
      	at nl.eleqtron.qvisual.vaadin.ui.MainWindow$1.buttonClick(
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      Any idea?


      • #4
        When you create a MessagingTemplate manually (rather than by declaring it as a bean) you can't send by channel name by default. You either have to send to a channel reference (e.g. @Autowire private MessageChannel input; ) or provide it with a channel resolver, or declare it as a <bean/> and @Autowire private MessagingTemplate template; (and Spring will wire up the template for you to enable sending by channel name).

        However, it's much better to isolate your sending code from Spring Integration by using a Messaging Gateway...