Socket通信Net例クライアント、ローカルサービス
8631 ワード
メインモジュール:1:クライアント、2:サービス側、3:SocketFactory、4:Connection接続オブジェクト、5:ConnectionCollection接続管理オブジェクト
テスト:
///
SocketLibrary.SocketFactory factory = new SocketLibrary.SocketFactory();
factory.StartServer(ip1, port1);
///
SocketLibrary.Connection conn = factory.StartClient(IPAddress.Parse(ip1), port1);
SocketLibrary.SocketFactory.SendMessage(" ", conn);
1:クライアント
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SocketLibrary
{
/// <summary>
/// Socket
/// </summary>
public class Client
{
// ,
public const int CONNECT_TIMEOUT = 10;
public Client() { }
/// <summary>
/// Socket
/// </summary>
/// <param name="ipaddress"></param>
/// <param name="port"></param>
/// <returns></returns>
public static Connection StartClient(IPAddress ipaddress, int port)
{
TcpClient client = new TcpClient();
client.SendTimeout = CONNECT_TIMEOUT;
client.ReceiveTimeout = CONNECT_TIMEOUT;
client.Connect(ipaddress, port);
Connection connection = new Connection(client.GetStream());
return connection;
}
}
}
2:サービス側
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace SocketLibrary
{
/// <summary>
/// Socket
/// </summary>
public class Server
{
private ConnectionCollection connections;
public ConnectionCollection Connections
{
get { return connections; }
set { connections = value; }
}
private TcpListener listener;
private Thread listenningthread;
public Server(TcpListener listener)
{
this.connections = new ConnectionCollection();
this.listener = listener;
}
public void Start()
{
while (true) {
if (listener.Pending()) {
TcpClient client = listener.AcceptTcpClient();
NetworkStream stream = client.GetStream();
this.connections.Add(new Connection(stream));
}
}
}
/// <summary>
///
/// </summary>
public void Listenning()
{
while (true)
{
Thread.Sleep(200);
foreach (Connection connection in this.connections) {
if (connection.NetworkStream.CanRead && connection.NetworkStream.DataAvailable)
{
byte[] buffer = new byte[1024];
int count = connection.NetworkStream.Read(buffer, 0, buffer.Length);
Console.Write("================Server ==================" + SocketFactory.encoding.GetString(buffer, 0, count));
}
}
}
}
/// <summary>
///
/// </summary>
public void StartListen()
{
listenningthread = new Thread(new ThreadStart(Listenning));
listenningthread.Start();
}
}
}
3:SocketFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace SocketLibrary
{
public class SocketFactory
{
private Thread serverListenThread;
public static Encoding encoding = Encoding.GetEncoding("utf-8");
public void StartServer(string ip, int port)
{
IPAddress ipa = IPAddress.Parse(ip);
TcpListener listener = new TcpListener(ipa, port);
listener.Start();
Server server = new Server(listener);
serverListenThread = new Thread(new ThreadStart(server.Start));
serverListenThread.Start();
server.StartListen();
}
public Connection StartClient(IPAddress ip, int port)
{
return Client.StartClient(ip, port);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="connection"></param>
public static void SendMessage(string message, Connection connection)
{
byte[] buffer = encoding.GetBytes(message);
connection.NetworkStream.Write(buffer, 0, buffer.Length);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="connection"></param>
public static void GetMessage(Connection connection)
{
// connection.NetworkStream.Length;
byte[] buffer = new byte[1024];
connection.NetworkStream.Write(buffer, 0, buffer.Length);
}
}
}
4:Connection接続オブジェクト
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace SocketLibrary
{
public class Connection
{
private NetworkStream networkStream;
public NetworkStream NetworkStream
{
get { return networkStream; }
set { networkStream = value; }
}
private string connectionName;
public string ConnectionName
{
get { return connectionName; }
set { connectionName = value; }
}
public Connection(NetworkStream networkStream, string connectionName)
{
this.networkStream = networkStream;
this.connectionName = connectionName;
}
public Connection(NetworkStream networkStream):this(networkStream, string.Empty) { }
}
}
5:ConnectionCollection接続管理オブジェクト
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace SocketLibrary
{
public class ConnectionCollection : CollectionBase
{
public ConnectionCollection() { }
public void Add(Connection conn) {
List.Add(conn);
}
public Connection this[int index] {
get {
return List[index] as Connection;
}
set{
List[index] = value;
}
}
public Connection this[string connectionName] {
get {
foreach(Connection connection in List) {
if(connection.ConnectionName == connectionName)
return connection;
}
return null;
}
}
}
}