title: How do I set up a Spring TCP client and server model?
body: <p>So following this question (<a href="https://stackoverflow.com/questions/20574038/how-to-plug-a-tcp-ip-client-server-in-a-spring-mvc-application/20574548#comment42382560_20574548">how to plug a TCP-IP client server in a spring MVC application</a>), 
  I was successfully able to wire a Gateway into my Spring REST controller.  However, I'm confused as to where to go next.  Here's what I'm trying to accomplish:</p>
  
  <p>1) When a certain route is hit with a POST request, open a connection to a certain IP (or work with a connection that's already open with this IP) passed from the POST and send a message.</p>
  
  <pre><code>@RequestMethod(value = '/sendTcpMessage', method=RequestMethod.POST)
  public void sendTcpMessage(@RequestParam(value="ipAddress", required=true) String ipAddress,
                             @RequestParam(value="message", required=true) String message) {
  
  //send the message contained in the 'message' variable to the IP address located 
  //at 'ipAddress' - how do I do this?
  
  }
  </code></pre>
  
  <p>2) I also need my Spring backend to listen to TCP "messages" passed to it, and store them in a buffer.  My Javascript will call a route every 5 seconds or so and read the information out of the buffer.</p>
  
  <p><b>Here is my controller code:</b></p>
  
  <pre><code>@Controller
  public class HomeController {
  
      @Resource(name = "userDaoImpl")
      private UserDAO userDao;
      @Resource(name = "receiveTcp")
      private ReceiveTcp tcpMessageReceiver;
      @Autowired
      SimpleGateway gw;
  
      String tcpBuffer[] = new String[100];
  
      @RequestMapping(value="/")
      public String home() {
          return "homepage";
      }
  
      @RequestMapping(value = "/checkTcpBuffer", method=RequestMethod.POST)
      public String[] passTcpBuffer() {
          return tcpMessageReceiver.transferBuffer();
      }
  
  }
  </code></pre>
  
  <p><b> root-context.xml </b></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:int="http://www.springframework.org/schema/integration"
      xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd
                          http://www.springframework.org/schema/integration/ http://www.springframework.org/schema/integration/spring-integration.xsd"&gt;
  
      &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  &lt;int:gateway id="gw"
     service-interface="net.codejava.spring.interfaces.SimpleGateway"
     default-request-channel="input"/&gt;
  
  &lt;bean id="javaSerializer"
        class="org.springframework.core.serializer.DefaultSerializer"/&gt;
  &lt;bean id="javaDeserializer"
        class="org.springframework.core.serializer.DefaultDeserializer"/&gt;
  
  &lt;int-ip:tcp-connection-factory id="server"
      type="server"
      port="8081"
      deserializer="javaDeserializer"
      serializer="javaSerializer"
      using-nio="true"
      single-use="true"/&gt;
  
  &lt;int-ip:tcp-connection-factory id="client"
      type="client"
      host="localhost"
      port="8081"
      single-use="true"
      so-timeout="10000"
      deserializer="javaDeserializer"
      serializer="javaSerializer"/&gt;
  
      &lt;int:channel id="input" /&gt;
  
      &lt;int:channel id="replies"&gt;
          &lt;int:queue/&gt;
      &lt;/int:channel&gt;
  
      &lt;int-ip:tcp-outbound-channel-adapter id="outboundClient"
      channel="input"
      connection-factory="client"/&gt;
  
      &lt;int-ip:tcp-inbound-channel-adapter id="inboundClient"
      channel="replies"
      connection-factory="client"/&gt;
  
      &lt;int-ip:tcp-inbound-channel-adapter id="inboundServer"
      channel="loop"
      connection-factory="server"/&gt;
  
      &lt;int-ip:tcp-outbound-channel-adapter id="outboundServer"
      channel="loop"
      connection-factory="server"/&gt;
  
      &lt;int:service-activator input-channel="input" ref="receiveTcp" method = "saveValue"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><b> ReceiveTcp.java </b></p>
  
  <pre><code>@Component(value = "receiveTcp")
  public class ReceiveTcp {
  
      String buf[] = new String[100];
      int currentPosition = 0;
  
      @ServiceActivator
      public void saveValue(String value){
          System.out.println(value);
          buf[currentPosition] = value;
          currentPosition++;
      }
  
      public String[] transferBuffer() {
          String tempBuf[] = new String[100];
          tempBuf = buf;
          buf = new String[100];
  
          return tempBuf;
      }
  }
  </code></pre>
  
  <p>How can I resolve these issues?</p>