简体   繁体   English

服务器-客户端Windows窗体应用程序C#

[英]Server-Client Windows Form Application C#

I'm trying to set up a server and client simulation in visual studio but having issues in getting it to work. 我正在尝试在Visual Studio中设置服务器和客户端模拟,但是在使其工作时遇到了问题。 Both programs run but that's all I get as output is "System.Byte[]". 这两个程序都运行,但是我得到的全部是输出“ System.Byte []”。 This is my first time in trying to make an application such as this. 这是我第一次尝试制作这样的应用程序。 I worked off some code from a console application so I've been trying to adapt it to suit a windows form. 我从控制台应用程序处理了一些代码,因此我一直在尝试使其适应Windows窗体。

Here's the client code: 这是客户端代码:

private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = "Client has Made Connection to Server OK";

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        int port;
        port = int.Parse(textBox4.Text);
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(textBox5.Text), port);


        sck.Connect(endpoint);

        //textBox1.Text = "Client has Made Connection to Server OK";

        Byte[] recbuffer = new byte[255];
        int rec = sck.Receive(recbuffer, 0, recbuffer.Length, 0);
        Array.Resize(ref recbuffer, rec);

        string temp = recbuffer.ToString();

        textBox2.Text = "Received: {0}" + temp;

        // sending string to server
        string msg = textBox3.Text;
        byte[] msgbuffer = Encoding.Default.GetBytes(msg);
        sck.Send(msgbuffer, 0, msgbuffer.Length, 0);


        Console.Read();
    }

And here's the server code: 这是服务器代码:

private void Form1_Load(object sender, EventArgs e)
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9939);
        Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        sck.Bind(ip);
        sck.Listen(0);

        Socket acc = sck.Accept();

        textBox1.Text = "Connection has been made to the server from the Client";

        byte[] buffer = Encoding.Default.GetBytes("Hello sent from Server to Client");
        string displayBuffer = buffer.ToString();
        textBox2.Text = displayBuffer;
        acc.Send(buffer, 0, buffer.Length, SocketFlags.None);

        byte[] recbuffer = new byte[255];

        int rec = acc.Receive(recbuffer, 0, recbuffer.Length, 0);

        Array.Resize(ref recbuffer, rec);

        string temp = recbuffer.ToString();

        textBox3.Text = "received from client the string: " + temp;

        Console.Read();

    }

The problem is in the line 问题出在行中

 string temp = recbuffer.ToString();

both on client code and on server code. 在客户端代码和服务器代码上均如此。

You need to change to 您需要更改为

 string temp = System.Text.Encoding.Default.GetString(recbuffer);

Asking a byte array to return a string through the ToString method returns just the name of the class because a byte array is not capable to convert itself in a string. 通过ToString方法要求字节数组返回字符串仅返回类的名称,因为字节数组无法将自身转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM