none
就一行代码,字符串相加不成功,求各位高手帮忙看看? RRS feed

  • 问题

  • //这是服务端

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Diagnostics;


    namespace Sever
    {
        public partial class Sever : Form
        {
            public Sever()
            {
                InitializeComponent();
            }


            private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(new ThreadStart(StartListening));
                thread.Start();
            }


            private void StartListening()
            {
                int listenport = 65512;
                IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });

                TcpListener listener = new TcpListener(ip, listenport);
                listener.Start();


                while (true)
                {
                    const int bufferSize = 256;

                    TcpClient client = listener.AcceptTcpClient();
                    NetworkStream clientStream = client.GetStream();

                    byte[] buffer = new byte[bufferSize];
                    int readBytes = 0;
                    readBytes = clientStream.Read(buffer, 0, bufferSize);
                    string request1 = Encoding.Unicode.GetString(buffer).Substring(0, readBytes);                               
                   
                    string request2 = "为什么这个不显示?";

                    string request = request1 + request2;               
                   
                    Debug.WriteLine(request);
                    Debug.WriteLine(request.Length);

                    MessageBox.Show(request);
                   
                    clientStream.Close();


                   
                }
            }


        }
    }

    //这是客户端

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    using System.Diagnostics;

    namespace Client
    {
        public partial class Client : Form
        {
            public Client()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });           

                TcpClient client = new TcpClient();
                client.Connect(ip, 65512);

                NetworkStream clientStream = client.GetStream();

                string request = "这个可以显示!";
                byte[] requestBuffer = Encoding.Unicode.GetBytes(request);
                clientStream.Write(requestBuffer, 0, requestBuffer.Length);
              
                clientStream.Close();
                client.Close();        

            }     

        }
    }

     Debug.WriteLine(request);  //执行的时候只有 request1 内容,没有 request2 的内容

    VS2005

    2013年2月19日 22:02

答案

  • string request1 = Encoding.Unicode.GetString(buffer).Substring(0, readBytes);  
    //改为  
    string request1 = Encoding.Unicode.GetString(buffer).Substring(0, readBytes/2);        

    已经解决了,把这行代码改下就行了。
    2013年2月20日 11:37

全部回复