title: C# - Sending and Receiving a TCP/IP message to an IP Address and Port
body: <p>I have the following code to send a TCP/IP message to a specific IP Address and Port:</p>
  
  <pre><code>public bool sendTCPMessage(string ip_address, string port, string transaction_id, string customer_username, DateTime date)
          {
              bool success = false;
  
              try
              {
                  int converted_port = Convert.ToInt32(port);
                  string converted_date = date.ToString("dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
  
                  JObject obj = new JObject();
                  obj["Transaction_Status"] = "Paid";
                  obj["Transaction_ID"] = transaction_id;
                  obj["Processed_Date"] = converted_date;
                  obj["Customer_Username"] = customer_username;
  
                  JSONMobile json_mobile = new JSONMobile();
                  string json = json_mobile.SerializeToString(obj);
  
                  TcpClient client = new TcpClient(ip_address, converted_port);
                  Byte[] message = System.Text.Encoding.ASCII.GetBytes(json);
                  NetworkStream stream = client.GetStream();
                  stream.Write(message, 0, message.Length);
                  stream.Close();
                  client.Close();
  
                  success = true;
              }
              catch (Exception)
              {
                  success = false;
              }
              return success;
          }
  </code></pre>
  
  <p>Now, let us assume that I pass the ip_address as '127.0.0.1' and the port as '1'.  When the method executes, I am getting the following exception:</p>
  
  <p><img src="https://i.stack.imgur.com/Z8K2V.png" alt="enter image description here"></p>
  
  <p>Is this happenning because there is no one listening at the other end?  If yes, how can I set up a server at that ip address (ok, not 0.0.0.45 but 127.0.0.1) and port number to accept the message and reply to it?  Thank you :)</p>