I'm building a TCP/IP connection for my application with a warehouse system. The communication goes like this.
- I send a message to the TCP/IP(Socket) server of the warehouse system.
- The warehouse system responds with a message the my local TCP/IP server.
So there are no direct response messages. Instead each application as it's own server. Yet I want my application to wait for the response coming from the other server.
So basicly I have the following code.
public string ControllerFunction() {
    startLocalTcpIpServer();
    sendMessage("a message");
    return clientMessage;
}
This is my own server started with the start() function
    public void Start() {
        // Start TcpServer background thread        
        tcpListenerThread = new Thread(new ThreadStart(ListenForIncommingRequests)) {
            IsBackground = true
        };
        tcpListenerThread.Start();
    }
    private void ListenForIncommingRequests() {
        try {
            tcpListener = new TcpListener(IPAddress.Parse(serverIp), port);
            tcpListener.Start();
            byte[] bytes = new byte[1024];
            Console.WriteLine("Server Started");
            while(true) {
                // Get a stream object for reading 
                using(NetworkStream stream = tcpListener.AcceptTcpClient().GetStream()) {
                    int length;
                    // Read incomming stream into byte arrary.                      
                    while((length = stream.Read(bytes, 0, bytes.Length)) != 0) {
                        byte[] incommingData = new byte[length];
                        Array.Copy(bytes, 0, incommingData, 0, length);
                        // Convert byte array to string message.                            
                        string clientMessage = Encoding.ASCII.GetString(incommingData);
                    }
                }
            }
        }
        catch(SocketException socketException) {
            Console.WriteLine("SocketException " + socketException.ToString());
        }
    }
So I want to use the result string clientMessage again as a return for my ControllerFunction. But how do I get the data there in a proper way?
 
    