如何让我的消息显示在聊天应用程序c#socket编程tcp的文本框中

如何让我的消息显示在聊天应用程序c#socket编程tcp的文本框中,c#,sockets,tcp,chat,C#,Sockets,Tcp,Chat,我已经学习了好几本关于socket编程的教程,除了这本之外,没有一本对我有用。我能够让服务器接收来自不同计算机上多个客户端的消息。现在我想不出如何将这些消息发布到我的文本框中。我非常感谢您的帮助,因为我对这方面还很陌生 这是服务器代码: namespace server_tutorial { class Program { static void Main(string[] args) { //IPAddress ip = Dns.GetHostEntry("lo

我已经学习了好几本关于socket编程的教程,除了这本之外,没有一本对我有用。我能够让服务器接收来自不同计算机上多个客户端的消息。现在我想不出如何将这些消息发布到我的文本框中。我非常感谢您的帮助,因为我对这方面还很陌生

这是服务器代码:

namespace server_tutorial
{
class Program
{
    static void Main(string[] args)
    {
        //IPAddress ip = Dns.GetHostEntry("localhost").AddressList[0];
        IPAddress ip = IPAddress.Parse("555.555.5.55");

        TcpListener server = new TcpListener(ip, 8080);
        TcpClient client = default(TcpClient);

        try
        {
            server.Start();
            Console.WriteLine("Server started...");



        }catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.Read();
        }

        while (true)
        {

            client = server.AcceptTcpClient();

            byte[] receivedBuffer = new byte[100];
            NetworkStream stream = client.GetStream();

            stream.Read(receivedBuffer, 0, receivedBuffer.Length);

            StringBuilder msg = new StringBuilder();

            foreach(byte b in receivedBuffer)
            {
                if (b.Equals(00))
                {
                    break;
                }
                else
                    msg.Append(Convert.ToChar(b).ToString());
            }

            Console.WriteLine(msg.ToString());

        }
    }
}
以下是客户端代码:

namespace client_tutorial
{
public partial class Form1 : Form
{
    string serverIP = "555.555.5.55";
    int port = 8080;

    public Form1()
    {
        InitializeComponent();

    }

    private void submit_Click(object sender, EventArgs e)
    {
        TcpClient client = new TcpClient(serverIP, port);

        int byteCount = Encoding.ASCII.GetByteCount(message.Text);

        byte[] sendData = new byte[byteCount];

        sendData = Encoding.ASCII.GetBytes(message.Text);

        NetworkStream stream = client.GetStream();

        stream.Write(sendData, 0, sendData.Length);

        stream.Close();
        client.Close();

    }
}

我知道这对某些人来说是一个非常简单的问题,但我将非常感谢任何帮助。

所以。。。你的文本框在哪里?对不起,忘了提一下,文本框现在叫做textbox1。它位于客户机上,我假设消息是另一个文本框,您可以在其中键入消息。。。。您想将该消息从该框推送到textbox1,还是想从服务器或其他客户端接收消息并将其放到textbox1?我希望该消息框能够将消息发送到服务器,而客户端能够将消息从服务器发送到textbox1,以便每个人都使用客户端将能够看到消息在这种情况下,您的方法有一个问题:您的服务器无法处理多个客户端,并且您的客户端没有与服务器的连接。。。这两件事都是你想做的事情的要求…也许你可以看看这里。。。