Question

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 = Adresse IP de l'hôte

txtPort = Numéro de port

J'ai une certaine confusion en termes de TcpListener / Sockets. Est-ce que quelqu'un pourrait m'aider à corriger ce code? En cliquant sur btnHost, vous hébergez la connexion et btnConnect se connecte à l'hôte. De plus, une fois connecté, comment puis-je qu'un événement déclenche un événement sur l'autre ordinateur?

Était-ce utile?

La solution

Appelez ensuite GetStream sur les deux TcpClient pour obtenir un Flux que vous pouvez utiliser pour communiquer avec l'autre côté (de manière synchrone ou asynchrone).

TcpClient et TcpListener ont des exemples détaillés dans le MSDN. Regardez-les et vous aurez bientôt quelque chose en marche.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top