简单实现进程间的通信

AppOne:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace AppOne
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket s = new Socket(AddressFamily.InterNetwork,
                          SocketType.Stream, ProtocolType.Tcp);
            IPAddress IPA = IPAddress.Parse("172.17.13.77");
            IPEndPoint ipEp = new IPEndPoint(IPA, 5567);
            s.Bind(ipEp);
            s.Listen(1);
            Socket ms = s.Accept();
            Console.WriteLine("connecting success");
            Console.WriteLine("please input a text");
            string message = Console.ReadLine();

            byte[] buffer = new byte[1024];
            buffer = Encoding.UTF8.GetBytes(message);
            ms.Send(buffer, buffer.Length, SocketFlags.None);

            Console.ReadKey();
            ms.Close();
            s.Close();
        }
    }
}
View Code

AppTwo:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace AppTwo
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket s = new Socket(AddressFamily.InterNetwork,
                               SocketType.Stream, ProtocolType.Tcp);
             
            IPEndPoint rIPEP = new IPEndPoint(IPAddress.Parse("172.17.13.77"), 5567);
            s.Connect(rIPEP);
 
            byte[] buffer = new byte[1024];
            int rbn;
            rbn = s.Receive(buffer);

            string message = Encoding.UTF8.GetString(buffer);
            Console.WriteLine(message);
            Console.ReadKey(true);
            s.Close();
        }
    }
}
View Code

运行结果如下图所示:

原文地址:https://www.cnblogs.com/jizhiqiliao/p/9884079.html