C# TCP 服务器帮助

发布于 2024-08-04 14:52:39 字数 1327 浏览 5 评论 0 原文

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;
using System.IO;

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

        private void btnConnect_Click(object sender, EventArgs e)
        {
            {
                IPAddress ipAd = IPAddress.Parse(txtAddress.Text);

                TcpListener myList = new TcpListener(ipAd, int.Parse(txtPort.Text));

                myList.Start();
                Socket s = myList.AcceptSocket();
                RPS rps = new RPS();
                rps.Show();            
            }
        }

        private void btnHost_Click(object sender, EventArgs e)
        {
            IPAddress ipAd = IPAddress.Parse(GV.strAddress);
            TcpListener myList = new TcpListener(ipAd, int.Parse(txtPort.Text));

            myList.Start();

            Socket s = myList.AcceptSocket();
        }
    }
}

txtAddress = 主机的 IP 地址

txtPort = 端口号

我对 TcpListener/Sockets 有一些困惑。有人可以帮我修复这个代码吗?单击 btnHost 让您托管连接,btnConnect 将连接到主机。另外,连接后,如何让 1 个事件触发另一台计算机上的事件?

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;
using System.IO;

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

        private void btnConnect_Click(object sender, EventArgs e)
        {
            {
                IPAddress ipAd = IPAddress.Parse(txtAddress.Text);

                TcpListener myList = new TcpListener(ipAd, int.Parse(txtPort.Text));

                myList.Start();
                Socket s = myList.AcceptSocket();
                RPS rps = new RPS();
                rps.Show();            
            }
        }

        private void btnHost_Click(object sender, EventArgs e)
        {
            IPAddress ipAd = IPAddress.Parse(GV.strAddress);
            TcpListener myList = new TcpListener(ipAd, int.Parse(txtPort.Text));

            myList.Start();

            Socket s = myList.AcceptSocket();
        }
    }
}

txtAddress = IP Address of Host

txtPort = Port Number

I have some confusion in terms of TcpListener/Sockets. Could someone help me fix this code? Clicking btnHost let's you host the connection and btnConnect connects to the host. Also, once connected, how can I have 1 event trigger an event on the other computer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脸赞 2024-08-11 14:52:39

然后调用 GetStream 在两个 TcpClient 上获取 Stream,您可以使用它与另一端通信(同步或异步)。

TcpClientTcpListener 在 MSDN 中都有大量示例。看看它们,很快就会有东西运行起来。

  • Use a TcpClient on the client-side to initiate a connection to the server (Connect).

  • Use a TcpListener on the server-side to accept incoming connections (AcceptTcpClient).
    AcceptTcpClient returns a TcpClient.

Then call GetStream on the two TcpClients to get a Stream that you can use to communicate with the other side (synchronously or asynchronously).

Both TcpClient and TcpListener have extensive examples in the MSDN. Have a look at them and you'll have something running pretty soon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文