Thursday 7 April 2016

c# tcp/ip & pipe

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Pipes;

namespace clientpipe
{
    public partial class Form1 : Form
    {
        static NamedPipeClientStream p = new NamedPipeClientStream("localhost", "demo", PipeDirection.InOut);
        StreamReader r = new StreamReader(p);
        StreamWriter w = new StreamWriter(p);

        public Form1()
        {
            InitializeComponent();
            p.Connect();
            w.AutoFlush = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            w.WriteLine(send.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            receive.Text = r.ReadLine();
        }
    }
}

----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Pipes;

namespace server_pipe
{
    public partial class Form1 : Form
    {
        static NamedPipeServerStream p = new NamedPipeServerStream("demo", PipeDirection.InOut);

        StreamReader r = new StreamReader(p);
        StreamWriter w = new StreamWriter(p);

        public Form1()
        {
            InitializeComponent();
            //pipes uses contructors not form load
            p.WaitForConnection();
            w.AutoFlush = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            w.WriteLine(send.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            receive.Text = r.ReadLine();
        }
    }
}
-------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;

namespace client
{
    public partial class Form1 : Form
    {
        NetworkStream n;
        BinaryReader r;
        BinaryWriter w;
        TcpClient t;

        public Form1()
        {
            InitializeComponent();
        }


        //send
        private void button1_Click(object sender, EventArgs e)
        {
            w.Write(send.Text);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            t = new TcpClient();
            t.Connect("localhost", 5000);//127.0.0.1
            n = t.GetStream();
            r = new BinaryReader(n);
            w = new BinaryWriter(n);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            receive.Text = r.ReadString();
        }
    }
}

----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        NetworkStream n;
        BinaryReader r;
        BinaryWriter w;
        Socket s;
        TcpListener L;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            L = new TcpListener(5000);
            L.Start();
            s = L.AcceptSocket();
            n = new NetworkStream(s);
            w = new BinaryWriter(n);
            r = new BinaryReader(n);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            w.Write(send.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            receive.Text = r.ReadString();
        }
    }
}

No comments:

Post a Comment