【问题标题】:C# TCP Server HelpC# TCP 服务器帮助
【发布时间】:2009-09-10 23:33:01
【问题描述】:
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 个事件触发另一台计算机上的事件?

【问题讨论】:

    标签: c# sockets host tcplistener


    【解决方案1】:

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

    TcpClientTcpListener 在 MSDN 中有大量示例。看看它们,你很快就会有一些东西在运行。

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 2023-03-29
      • 2010-11-08
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多