C#TCP服务器帮助

时间:2009-09-10 23:33:01

标签: c# sockets host tcplistener

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个事件触发事件?

1 个答案:

答案 0 :(得分:3)

然后在两个TcpClient上调用GetStream以获得可用于与另一方通信的Stream(同步或异步)。

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