Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在尝试在Visual Studio 2010中创建聊天应用程序_C#_.net_Sockets_Tcp_Tcplistener - Fatal编程技术网

C# 正在尝试在Visual Studio 2010中创建聊天应用程序

C# 正在尝试在Visual Studio 2010中创建聊天应用程序,c#,.net,sockets,tcp,tcplistener,C#,.net,Sockets,Tcp,Tcplistener,我是一个网络新手,但我必须从一些东西开始,所以我决定在VisualStudio2010中使用C#语言(winforms)创建一个聊天应用程序 我在谷歌上搜索了很多,几乎找到了我需要的东西。 我发现了以下代码示例(在C#控制台中): 我想使用TCP协议创建该应用程序(我不知道是否有更简单的方法,但当我尝试用C#创建聊天时,我了解了TCP的基本原理) 当我执行上面链接中的代码示例时,它们工作了!所以我尝试在聊天应用程序中调整这些示例 我的聊天应用程序实际上由两个应用程序组成:一个服务器和一个客户

我是一个网络新手,但我必须从一些东西开始,所以我决定在VisualStudio2010中使用C#语言(winforms)创建一个聊天应用程序

我在谷歌上搜索了很多,几乎找到了我需要的东西。 我发现了以下代码示例(在C#控制台中):

我想使用TCP协议创建该应用程序(我不知道是否有更简单的方法,但当我尝试用C#创建聊天时,我了解了TCP的基本原理)

当我执行上面链接中的代码示例时,它们工作了!所以我尝试在聊天应用程序中调整这些示例

我的聊天应用程序实际上由两个应用程序组成:一个服务器和一个客户端。它们都有相同的GUI(两个文本框、一个按钮和两个标签,用于显示客户端是否连接到服务器)

服务器/客户端应用程序上的textBox1显示客户端/服务器应用程序发送的消息。 在服务器/客户端应用程序的文本框2中,用户键入消息,然后按下按钮将消息发送到客户端/服务器应用程序

让我向您展示我迄今为止所做的尝试: 这是服务器应用程序代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;


namespace Server_TCP_WINFORMS
{
    public partial class Form1 : Form
    {
    //Int32 port = 13000;
    IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 13000);
    public Form1()
    {
        InitializeComponent();
        server.Start();
    }

    byte[] bytes = new byte[256];
    String data = null;
    TcpClient client = new TcpClient();


    bool sw = true;
    int data_at_byte_level = 0;

    private void Form1_Load(object sender, EventArgs e)
    {

        try
        {


                label2.Text = "Waiting for an incoming connection...";
                if (!server.Pending())
                {
                    label2.Text = "For moment, there are no connections requests!";
                }
                else
                {

                    client = server.AcceptTcpClient();
                    label2.Text = "Connected!";
                    sw = false;
                }



        }
        catch (SocketException xyz)
        {
            MessageBox.Show("Exception occured!");
        }
        if (sw == false)
        {
            NetworkStream stream = client.GetStream();
            while ((data_at_byte_level = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes);
                textBox1.Text += data;
                data = null;
                bytes = null;

            }

        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        String dataT;
        if (textBox2.Text!=null && sw == false)
        {
            NetworkStream stream = client.GetStream();
            dataT = textBox2.Text;
            byte[] msg = System.Text.Encoding.ASCII.GetBytes(dataT);
            stream.Write(msg, 0, msg.Length);
        }
    }


}
}

这是客户端应用程序代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
namespace Client_TCP_WINFORMS
{
  public partial class Form1 : Form
  {
     TcpClient client = new TcpClient("127.0.0.1", 13000); 

    public Form1()
    {
        InitializeComponent();
        label2.Text = "Conected to the server!";

    }

    private void button1_Click(object sender, EventArgs e)
    {
        NetworkStream stream = client.GetStream();
        if (textBox2.Text != null)
        {
            String data_str = textBox2.Text;
            Byte[] data_byte = System.Text.Encoding.ASCII.GetBytes(data_str);
            stream.Write(data_byte, 0, data_byte.Length);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Byte[] data_byte = new Byte[290];
        int bytes;
        string Data;
        NetworkStream stream = client.GetStream();
        bytes = stream.Read(data_byte, 0, data_byte.Length);
        Data = System.Text.Encoding.ASCII.GetString(data_byte, 0, bytes);
        textBox1.Text += Data;

    }
}
}
我希望这两个应用程序按以下方式运行:我启动服务器应用程序,然后启动客户端应用程序。当它们都打开时,我希望它们已经连接(因为我认为这样更简单)

然后,我希望它们都是可接受的,这意味着当服务器(例如)向客户端发送消息时,后者应该接收并显示消息。如果服务器发送另一条消息,客户端也应该接收并显示消息。 如果用户(客户端或服务器用户)按下发送按钮,应用程序应将消息从textBox2发送到其他应用程序。在windows窗体中如何执行这些操作

我在控制台中的代码示例中看到,有一个主循环,服务器在其中从客户端读取消息。但是,如果服务器也想发送消息,该怎么办?如果按下“发送”按钮,则按钮被按下的事件发生,然后它发送消息,但当它完成发送消息后,返回到主循环

请原谅我的英语,我不是以英语为母语的人

非常感谢。

“当它们都打开时,我希望它们已经连接起来(因为我认为这样更简单)。”


为此,您需要使用UDP(用户数据报协议)而不是TCP

我不认为我的问题如此复杂。。。