TCPサーバーとTCPクライアントでデータの送受信を行う方法
【TCPサーバーの作成】
VB.NET
Public Class Server
Public Shared Sub Main()
Dim ipString As String = "127.0.0.1"
Dim ipAdd As System.Net.IPAddress = System.Net.IPAddress.Parse(ipString)
Dim port As Integer = 8012
Dim listener As New System.Net.Sockets.TcpListener(ipAdd, port)
listener.Start()
Console.WriteLine("開始({0}:{1})。", _
DirectCast(listener.LocalEndpoint, System.Net.IPEndPoint).Address, _
DirectCast(listener.LocalEndpoint, System.Net.IPEndPoint).Port)
Dim client As System.Net.Sockets.TcpClient = listener.AcceptTcpClient()
Console.WriteLine("クライアント({0}:{1})接続", _
DirectCast(client.Client.RemoteEndPoint, System.Net.IPEndPoint).Address, _
DirectCast(client.Client.RemoteEndPoint, System.Net.IPEndPoint).Port)
Dim ns As System.Net.Sockets.NetworkStream = client.GetStream()
ns.ReadTimeout = 10000
ns.WriteTimeout = 10000
Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim disconnected As Boolean = False
Dim ms As New System.IO.MemoryStream()
Dim resBytes As Byte() = New Byte(255) {}
Dim resSize As Integer = 0
Do
resSize = ns.Read(resBytes, 0, resBytes.Length)
If resSize = 0 Then
disconnected = True
Console.WriteLine("クライアント切断")
Exit Do
End If
ms.Write(resBytes, 0, resSize)
Loop While ns.DataAvailable OrElse _
resBytes(resSize - 1) <> AscW(ControlChars.Lf)
Dim resMsg As String = enc.GetString(ms.GetBuffer(), 0, CInt(ms.Length))
ms.Close()
resMsg = resMsg.TrimEnd(ControlChars.Lf)
Console.WriteLine(resMsg)
If Not disconnected Then
Dim sendMsg As String = resMsg.Length.ToString()
Dim sendBytes As Byte() = enc.GetBytes(sendMsg & ControlChars.Lf)
ns.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(sendMsg)
End If
ns.Close()
client.Close()
Console.WriteLine("閉じました")
listener.Stop()
Console.WriteLine("閉じました")
Console.ReadLine()
End Sub
End Class
Network programming in .NET: C# & Visual Basic …
C#
using System;
public class Server
{
public static void Main()
{
string ipString = "127.0.0.1";
System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(ipString);
int port = 8012;
System.Net.Sockets.TcpListener listener =
new System.Net.Sockets.TcpListener(ipAdd, port);
listener.Start();
Console.WriteLine("開始({0}:{1})。",
((System.Net.IPEndPoint)listener.LocalEndpoint).Address,
((System.Net.IPEndPoint)listener.LocalEndpoint).Port);
System.Net.Sockets.TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("({0}:{1})接続",
((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Address,
((System.Net.IPEndPoint)client.Client.RemoteEndPoint).Port);
System.Net.Sockets.NetworkStream ns = client.GetStream();
ns.ReadTimeout = 10000;
ns.WriteTimeout = 10000;
System.Text.Encoding enc = System.Text.Encoding.UTF8;
bool disconnected = false;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] resBytes = new byte[256];
int resSize = 0;
do
{
resSize = ns.Read(resBytes, 0, resBytes.Length);
if (resSize == 0)
{
disconnected = true;
Console.WriteLine("切断");
break;
}
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');
Console.WriteLine(resMsg);
if (!disconnected)
{
string sendMsg = resMsg.Length.ToString();
byte[] sendBytes = enc.GetBytes(sendMsg + '\n');
ns.Write(sendBytes, 0, sendBytes.Length);
Console.WriteLine(sendMsg);
}
ns.Close();
client.Close();
Console.WriteLine("閉じました");
listener.Stop();
Console.WriteLine("閉じました");
Console.ReadLine();
}
}
【TCPサーバーでIPv4とIPv6への対応方法】
VB.NET
Dim listener As New System.Net.Sockets.TcpListener( _
System.Net.IPAddress.IPv6Any, 2001)
listener.Server.SetSocketOption( _
System.Net.Sockets.SocketOptionLevel.IPv6, _
System.Net.Sockets.SocketOptionName.IPv6Only, _
0)
listener.Start()
Dim client As System.Net.Sockets.TcpClient = listener.AcceptTcpClient()
Console.WriteLine("IPアドレス:{0} ポート番号:{1})。", _
DirectCast(client.Client.LocalEndPoint, System.Net.IPEndPoint).Address, _
DirectCast(client.Client.LocalEndPoint, System.Net.IPEndPoint).Port)
C#System.Net.Sockets.TcpListener listener =
new System.Net.Sockets.TcpListener(System.Net.IPAddress.IPv6Any, 2001);
listener.Server.SetSocketOption(
System.Net.Sockets.SocketOptionLevel.IPv6,
System.Net.Sockets.SocketOptionName.IPv6Only,
0);
listener.Start();
System.Net.Sockets.TcpClient client = listener.AcceptTcpClient();
Console.WriteLine("IPアドレス:{0} ポート番号:{1})。",
((System.Net.IPEndPoint)client.Client.LocalEndPoint).Address,
((System.Net.IPEndPoint)client.Client.LocalEndPoint).Port);
【TCPクライアントの作成】
VB.NET
Public Class Client
Public Shared Sub Main()
Console.WriteLine("文字列入力、Enterキー")
Dim sendMsg As String = Console.ReadLine()
If sendMsg Is Nothing OrElse sendMsg.Length = 0 Then
Return
End If
Dim ipOrHost As String = "127.0.0.1"
Dim port As Integer = 8012
Dim tcp As New System.Net.Sockets.TcpClient(ipOrHost, port)
Console.WriteLine("サーバー({0}:{1})と接続しました({2}:{3})。", _
DirectCast(tcp.Client.RemoteEndPoint, System.Net.IPEndPoint).Address, _
DirectCast(tcp.Client.RemoteEndPoint, System.Net.IPEndPoint).Port, _
DirectCast(tcp.Client.LocalEndPoint, System.Net.IPEndPoint).Address, _
DirectCast(tcp.Client.LocalEndPoint, System.Net.IPEndPoint).Port)
Dim ns As System.Net.Sockets.NetworkStream = tcp.GetStream()
ns.ReadTimeout = 10000
ns.WriteTimeout = 10000
Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8
Dim sendBytes As Byte() = enc.GetBytes(sendMsg & ControlChars.Lf)
ns.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(sendMsg)
Dim ms As New System.IO.MemoryStream()
Dim resBytes As Byte() = New Byte(255) {}
Dim resSize As Integer = 0
Do
resSize = ns.Read(resBytes, 0, resBytes.Length)
If resSize = 0 Then
Console.WriteLine("サーバー切断")
Exit Do
End If
ms.Write(resBytes, 0, resSize)
Loop While ns.DataAvailable OrElse _
resBytes(resSize - 1) <> AscW(ControlChars.Lf)
Dim resMsg As String = enc.GetString(ms.GetBuffer(), 0, CInt(ms.Length))
ms.Close()
resMsg = resMsg.TrimEnd(ControlChars.Lf)
Console.WriteLine(resMsg)
ns.Close()
tcp.Close()
Console.WriteLine("切断")
Console.ReadLine()
End Sub
End Class
C#using System;
public class Client
{
public static void Main()
{
Console.WriteLine("文字列入力、Enterキー");
string sendMsg = Console.ReadLine();
if (sendMsg == null || sendMsg.Length == 0)
{
return;
}
string ipOrHost = "127.0.0.1";
int port = 8012;
System.Net.Sockets.TcpClient tcp =
new System.Net.Sockets.TcpClient(ipOrHost, port);
Console.WriteLine("サーバー({0}:{1})接続({2}:{3})。",
((System.Net.IPEndPoint)tcp.Client.RemoteEndPoint).Address,
((System.Net.IPEndPoint)tcp.Client.RemoteEndPoint).Port,
((System.Net.IPEndPoint)tcp.Client.LocalEndPoint).Address,
((System.Net.IPEndPoint)tcp.Client.LocalEndPoint).Port);
System.Net.Sockets.NetworkStream ns = tcp.GetStream();
ns.ReadTimeout = 10000;
ns.WriteTimeout = 10000;
System.Text.Encoding enc = System.Text.Encoding.UTF8;
byte[] sendBytes = enc.GetBytes(sendMsg + '\n');
ns.Write(sendBytes, 0, sendBytes.Length);
Console.WriteLine(sendMsg);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] resBytes = new byte[256];
int resSize = 0;
do
{
resSize = ns.Read(resBytes, 0, resBytes.Length);
if (resSize == 0)
{
Console.WriteLine("サーバー切断");
break;
}
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');
Console.WriteLine(resMsg);
ns.Close();
tcp.Close();
Console.WriteLine("切断");
Console.ReadLine();
}
}
Network programming in .NET: C# & Visual Basic …