0

I have the following code for a server:

public class TCPListener
    {
        public static void Main()
        {
            try
            {
                JSONMobile json_mobile = new JSONMobile();

                IPAddress ip_address = IPAddress.Parse("127.0.0.1");
                int port_number = 5000;

                TcpListener server = new TcpListener(ip_address, port_number);
                server.Start();

                Byte[] bytes = new Byte[2048];
                String received_data = null;
                JObject obj = new JObject();

                while (true)
                {
                    TcpClient client = server.AcceptTcpClient();
                    received_data = null;

                    NetworkStream stream = client.GetStream();
                    int i;

                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        received_data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        obj = json_mobile.DeserializeToObject<JObject>(received_data);

                        string transaction_status = obj["Transaction_Status"].ToString();
                        string transaction_id = obj["Transaction_ID"].ToString();
                        string processed_date = obj["Processed_Date"].ToString();
                        string customer_username = obj["Customer_Username"].ToString();

                        byte[] msg = System.Text.Encoding.ASCII.GetBytes("The message was received!");
                        stream.Write(msg, 0, msg.Length);
                    }
                    client.Close();
                }
            }
            catch (SocketException)
            {
                //Catch socket exception
            }
        }

I have the following code for the client:

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);
                //int received_bytes_count = 0;
                //string received_data = "";

                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);

                //Send the JSON Object
                TcpClient client = new TcpClient(ip_address, converted_port);
                Byte[] sent_message = System.Text.Encoding.ASCII.GetBytes(json);
                NetworkStream stream = client.GetStream();
                stream.Write(sent_message, 0, sent_message.Length);

                //Receive the response
                /*Byte[] received_message = new Byte[2048];
                received_bytes_count = stream.Read(received_message, 0, received_message.Length);

                received_data = System.Text.Encoding.Default.GetString(received_message);*/

                //Close the connection
                stream.Close();
                client.Close();

                success = true;
            }
            catch (Exception)
            {
                success = false;
            }
            return success;
        }

Now, I have opened port 5000 in Windows Firewall. However, if I try to telnet 127.0.0.1 5000 (the server), all I get is a bad request message. Obviously, if I place a breakpoint in the server and run the client, the breakpoint never enters. What is the problem? What am I doing wrong?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Matthew
  • 4,477
  • 21
  • 70
  • 93
  • possible duplicate of [Problem reading from a TCPClient](http://stackoverflow.com/questions/6178883/problem-reading-from-a-tcpclient) – jgauffin Jul 03 '13 at 21:50
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 03 '13 at 23:04
  • BTW, never ignore exceptions. The exception could be telling you exactly what's wrong, and you'll never know it. – John Saunders Jul 03 '13 at 23:05

1 Answers1

1

I created a new solution around your server class and ran it (removing references to JSONMobile). Then using standard telnet I was able to get responses from the server.

So I would say that it is environmental. Since you have already opened a port some things that you could try are;

  • disable firewall (but given you are using localhost the firewall should not really cause problems)
  • check that you do not have another application already listening on that port
  • try another port
  • Thanks for the response :) As a matter of fact, I copied the code to a console application and it works. When I put it in a web application, the code does not work for some reason or another. The telnet client connects however I don't receive a response. – Matthew Jul 04 '13 at 08:51