SERVERから起動。1:1通信です。TCP/IPのサンプルをチャット・非同期化してみました。
/// SERVER
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
// https://dobon.net/vb/dotnet/internet/tcpclientserver.html
namespace sertcp {
public partial class Form1 : Form {
static System.Net.Sockets.TcpClient client;
static System.Net.Sockets.NetworkStream ns;
static System.Text.Encoding enc;
public Form1() {
InitializeComponent();
Start();
}
private async void Start() {
textBox1.Text = "SERVER START\r\n";
string ipString = "127.0.0.1";
int port = 2001;
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(ipString);
//TcpListenerオブジェクトを作成する
System.Net.Sockets.TcpListener listener = new System.Net.Sockets.TcpListener(ipAdd, port);
//Listenを開始する
listener.Start();
textBox1.Text = "listener start\r\n" + textBox1.Text;
//接続要求があったら受け入れる
await Task.Run(() => {
client = listener.AcceptTcpClient();
});
textBox1.Text += "connect\r\n";
textBox1.Text += (((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Address).ToString() + "\r\n";
textBox1.Text += (((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Port).ToString() + "\r\n";
//NetworkStreamを取得
ns = client.GetStream();
enc = System.Text.Encoding.UTF8;
byte[] resBytes = new byte[256];
int resSize = 0;
while (true) {
System.IO.MemoryStream ms = new System.IO.MemoryStream();
await Task.Run(() => {
do {
//データの一部を受信する
try {
resSize = ns.Read(resBytes, 0, resBytes.Length);
}
catch (System.IO.IOException e) {
MessageBox.Show(e.Message);
continue;
}
//受信したデータを蓄積する
ms.Write(resBytes, 0, resSize);
} while (ns.DataAvailable || resBytes[resSize - 1] != '\n');
});
string resMsg = enc.GetString(ms.GetBuffer(), 0, (int)ms.Length);
ms.Close();
resMsg = resMsg.TrimEnd('\n');
textBox1.Text = resMsg + "\r\n" + textBox1.Text;
}
}
// SEND
private async void button1_Click(object sender, EventArgs e) {
while (true) {
byte[] sendBytes = enc.GetBytes(textBox2.Text + '\n');
await Task.Run(() => {
ns.Write(sendBytes, 0, sendBytes.Length);
});
textBox2.Text = "";
break;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// CLIENT
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace clitcp {
public partial class Form1 : Form {
public static System.Net.Sockets.NetworkStream ns;
public static System.Text.Encoding enc = System.Text.Encoding.UTF8;
string ipOrHost = "127.0.0.1";
//string ipOrHost = "localhost";
int port = 2001;
public Form1() {
InitializeComponent();
try {
System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient(ipOrHost, port);
ns = tcp.GetStream();
}
catch(System.Net.Sockets.SocketException e){
textBox2.Text=e.Message;
return;
}
Recv();
}
// SEND
private async void button1_Click(object sender, EventArgs e) {
while (true) {
byte[] sendBytes = enc.GetBytes(textBox1.Text + '\n');
await Task.Run(() => {
ns.Write(sendBytes, 0, sendBytes.Length);
});
textBox1.Text = "";
break;
}
}
// RECV
private async void Recv() {
while (true) {
System.IO.MemoryStream ms = new System.IO.MemoryStream();
enc = System.Text.Encoding.UTF8;
byte[] resBytes = new byte[256];
int resSize = 0;
await Task.Run(() => {
do {
//データの一部を受信する
try {
resSize = ns.Read(resBytes, 0, resBytes.Length);
}
catch (System.IO.IOException e) {
MessageBox.Show(e.Message);
continue;
}
//受信したデータを蓄積する
ms.Write(resBytes, 0, resSize);
} while (ns.DataAvailable || resBytes[resSize - 1] != '\n');
});
string resMsg = enc.GetString(ms.GetBuffer(), 0, (int)ms.Length);
ms.Close();
resMsg = resMsg.TrimEnd('\n');
textBox2.Text = resMsg + "\r\n" + textBox2.Text;
}
}
}
}
戻る