TCP/IP サーバースレッド
Namespace: FVIL.NetAssembly: FVILforms (in FVILforms.dll) Version: 3.1.0.0 (3.1.0.9)
Syntax
C# |
---|
public class TcpServerThread : IDisposable |
Visual Basic |
---|
Public Class TcpServerThread Implements IDisposable |
Remarks
TCP/IPサーバー(Listener)を包含したクラスです。
クライアントが Connect すると、サーバーの Listener が Accept して送受信スレッドを生成します。
生成された送受信スレッドは Clients プロパティに追加されます。
その後、Accepted イベントで通知しますが、通常はこのイベントを無視して構いません。
各クライアントからのデータ受信は Notify イベントで通知されます。
Examples
TCP/IP クライアントからサーバーへ送信する例を示します。
この例では SendAsBinary(Object) を使用して送信しています。
このメソッドを使用するには送信対象のクラスが Serializable 属性を持たなければなりません。
C# | Copy |
---|---|
/// <summary> /// TCP/IP 通信テスト /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuOpen_Click(object sender, EventArgs e) { FVIL.Net.IPInfo info = new FVIL.Net.IPInfo("127.0.0.1", 50000, "friend"); TcpServer = new FVIL.Net.TcpServerThread(info); TcpServer.Notify += new FVIL.Net.TcpClientEventHandler(TcpServer_Notify); TcpServer.Start(); TcpClient = new FVIL.Net.TcpClientThread(info); TcpClient.Start(); } /// <summary> /// TCP/IP サーバー /// </summary> FVIL.Net.TcpServerThread TcpServer = null; /// <summary> /// TCP/IP クライアント /// </summary> FVIL.Net.TcpClientThread TcpClient = null; /// <summary> /// TCP/IP 通信:解放 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuClose_Click(object sender, EventArgs e) { TcpClient.Dispose(); TcpServer.Dispose(); } /// <summary> /// TCP/IP 通信:送信 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void menuSend_Click(object sender, EventArgs e) { FVIL.Data.CFviPoint data = new FVIL.Data.CFviPoint(111.1, 222.2); TcpClient.SendAsBinary(data); } /// <summary> /// TCP/IP 通信:受信イベントハンドラ /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void TcpServer_Notify(object sender, FVIL.Net.TcpClientEventArgs e) { Console.WriteLine("TimeStamp = {0}", e.TimeStamp); Console.WriteLine("Exception = {0}", e.Exception); if (e.Data != null) { FVIL.Net.TcpClientThread client = (FVIL.Net.TcpClientThread)sender; object data = client.ReadAsBinary(e.Data); Console.WriteLine("data = {0}", data.GetType().FullName); if (data is FVIL.Data.CFviPoint) { FVIL.Data.CFviPoint point = (FVIL.Data.CFviPoint)data; Console.WriteLine("point.X = {0}", point.X); Console.WriteLine("point.Y = {0}", point.Y); } } } |
出力結果:
TimeStamp = 2012/05/31 19:40:59 Exception = data = FVIL.Data.CFviPoint point.X = 111.1 point.Y = 222.2
Inheritance Hierarchy
System..::..Object
FVIL.Net..::..TcpServerThread
FVIL.Net..::..TcpServerThread