我先将信息存储到一个消息队列中
然后逐条将消息队列中的信息通过socket传递到服务器之前第一次使用,服务器依次接收到了两条信息后来添加了一个判断消息队列body是否为空的判断,服务器这边就开始抛异常再改回原代码,客户端这里一发送信息,客户端就假死,要重新运行谁能给看看到底是什么原因呢客户端代码:
try
            {
                bool check = true;
                int port = 8152;
                string host = "101.1.*.***";
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);
                while(check)
                {
                    System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".\\Private$\\MSMBBan"); 
                    System.Messaging.Message message = queue.Receive();
                    message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
                    if (message.Body != null)
                        {
                            Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                            c.Connect(ipe);
                            string sendStr = message.Body.ToString();
                            byte[] bs = Encoding.UTF8.GetBytes(sendStr);
                            c.Send(bs, bs.Length, 0);
                            c.Close();
                        }
                        else
                        {
                            check = false;
                            return;
                        }    
                    }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException ex)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
服务器代码:
            int i = 1;
            int f = 1;
            while (i == 1)
            {
                try
                {
                    int port = 8152;
                    string host = "101.1.*.***";
                    IPAddress ip = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ip, port);
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    s.Bind(ipe);
                    s.Listen(0);
                    Socket temp = s.Accept();
                    string recvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
                    recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
                    this.textBox1.Text = recvStr.ToString();
                    string sendStr = "Sucess" + f++;
                    byte[] bs = Encoding.UTF8.GetBytes(sendStr);
                    temp.Send(bs, bs.Length, 0);
                    temp.Shutdown(SocketShutdown.Both);
                    temp.Close();
                    s.Shutdown(SocketShutdown.Both);
                    s.Close();
                }
                catch (ArgumentNullException ae)
                {
                    Console.WriteLine("ArgumentNullException: {0}", ae);
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException: {0}", se);
                }
                Console.ReadLine();