客户端服务器架构1

时间:2013-07-03 08:16:16

标签: c# asp.net client

我有一个服务器的代码:

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
            }
        }

我有以下客户端代码:

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

现在,我在Windows防火墙中打开了端口5000。但是,如果我尝试telnet 127.0.0.1 5000(服务器),我得到的只是一个错误的请求消息。显然,如果我在服务器中放置断点并运行客户端,断点永远不会进入。问题是什么?我究竟做错了什么?

1 个答案:

答案 0 :(得分:1)

我围绕您的服务器类创建了一个新的解决方案并运行它(删除对JSONMobile的引用)。然后使用标准的telnet我能够从服务器获得响应。

所以我会说它是环保的。既然你已经开了一个端口,你可以尝试一些东西;

  • 禁用防火墙(但鉴于您使用的是localhost,防火墙不应该真正导致问题)
  • 检查您是否有其他应用程序已在该端口上侦听
  • 尝试另一个端口