この記事で紹介する「名前付きパイプ」を利用する以外に、共有メモリを利用する方法やDDE通信を利用する方法があります。共有メモリ(メモリ マップト ファイル)を利用する方法はこちらの記事を参照してください。また、DDE通信を利用する方法はこちらの記事を参照してください。
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 SimpleNamedPipeServer
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
NamedPipeServerStream npss = new NamedPipeServerStream("TestPipe", PipeDirection.InOut);
npss.WaitForConnection();
if (npss.IsConnected == true) {
int data = npss.ReadByte();
textBox1.Text += data.ToString("x") + "\r\n";
}
}
}
}
NamedPipeServerStream npss = new NamedPipeServerStream("TestPipe", PipeDirection.InOut);
npss.WaitForConnection();
if (npss.IsConnected == true) {
int data = npss.ReadByte();
textBox1.Text += data.ToString("x") + "\r\n";
}
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 SimpleNamedPipeClient
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte data = Convert.ToByte(textBox1.Text,16);
NamedPipeClientStream npcs = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut);
npcs.Connect();
npcs.WriteByte(data);
npcs.WaitForPipeDrain();
}
}
}
byte data = Convert.ToByte(textBox1.Text,16);
NamedPipeClientStream npcs = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut);
npcs.Connect();
npcs.WriteByte(data);
npcs.WaitForPipeDrain();
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 SimpleNamedPipeServer
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
NamedPipeServerStream npss = new NamedPipeServerStream("TestPipe", PipeDirection.InOut);
npss.WaitForConnection();
if (npss.IsConnected == true) {
byte[] buffer = new byte[256];
int readsize = npss.Read(buffer,0,256);
string text = Encoding.UTF8.GetString(buffer);
textBox1.Text += text + "\r\n";
}
}
}
}
buffer
配列に格納します。
取得したバイト配列をEncoding.UTF8.GetString()
メソッドで文字列に変換して、文字列をテキストボックスに表示します。 byte[] buffer = new byte[256];
int readsize = npss.Read(buffer,0,256);
string text = Encoding.UTF8.GetString(buffer);
textBox1.Text += text + "\r\n";
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 SimpleNamedPipeClient
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
byte[] data = new byte[256];
Encoding.UTF8.GetBytes(textBox1.Text, 0, textBox1.Text.Length, data, 0);
NamedPipeClientStream npcs = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut);
npcs.Connect();
npcs.Write(data, 0, 256);
npcs.WaitForPipeDrain();
}
}
}
byte[] data = new byte[256];
Encoding.UTF8.GetBytes(textBox1.Text, 0, textBox1.Text.Length, data, 0);
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 SimpleNamedPipeAsyncServer
{
public partial class FormMain : Form
{
private NamedPipeServerStream npss;
private delegate void MyEventHandler(string msg);
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
npss = new NamedPipeServerStream("TestPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
npss.BeginWaitForConnection(OnConnect, null);
}
private void OnConnect(IAsyncResult result)
{
if (npss != null) {
npss.EndWaitForConnection(result);
int data = npss.ReadByte();
this.Invoke(new MyEventHandler(OutputMessage), new string[] { data.ToString("x") + "\r\n" });
}
}
private void OutputMessage(string msg)
{
textBox1.Text += msg;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
npss = new NamedPipeServerStream("TestPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
npss.BeginWaitForConnection(OnConnect, null);
}
private void OnConnect(IAsyncResult result)
{
if (npss != null) {
npss.EndWaitForConnection(result);
int data = npss.ReadByte();
this.Invoke(new MyEventHandler(OutputMessage), new string[] { data.ToString("x") + "\r\n" });
}
}
private void OutputMessage(string msg)
{
textBox1.Text += msg;
}
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 SimpleNamedPipeAsyncClient
{
public partial class FormMain : Form
{
private delegate void MyEventHandler();
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte data = Convert.ToByte(textBox1.Text, 16);
NamedPipeClientStream npcs = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut);
npcs.Connect();
npcs.BeginWrite(new byte[] { data }, 0, 1, OnComplete, null);
}
private void OnComplete(IAsyncResult result)
{
this.Invoke(new MyEventHandler(OutputMessage));
}
private void OutputMessage()
{
textBox2.Text += "Send Complete";
}
}
}
private void button1_Click(object sender, EventArgs e)
{
byte data = Convert.ToByte(textBox1.Text, 16);
NamedPipeClientStream npcs = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut);
npcs.Connect();
npcs.BeginWrite(new byte[] { data }, 0, 1, OnComplete, null);
}
private void OnComplete(IAsyncResult result)
{
this.Invoke(new MyEventHandler(OutputMessage));
}
private void OutputMessage()
{
textBox2.Text += "Send Complete";
}
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.Pipes;
namespace SimpleNamedPipeAsyncTaskServer
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
NamedPipeServerStream npss = new NamedPipeServerStream("TestPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
textBox1.Text += "待機中です。\r\n\r\n";
await npss.WaitForConnectionAsync();
byte[] buffer = new byte[1];
int size = await npss.ReadAsync(buffer, 0, 1);
textBox1.Text += buffer[0].ToString("x");
}
}
}
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.Pipes;
namespace SimpleNamedPipeAsyncTaskClient
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
byte data = Convert.ToByte(textBox1.Text, 16);
NamedPipeClientStream npcs = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut);
await npcs.ConnectAsync();
await npcs.WriteAsync(new byte[] { data }, 0, 1);
textBox2.Text = "送信しました。";
}
}
}