using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Collections.Generic;
using System.Net.NetworkInformation;
namespace TcpServer
{
class Program
{
static void Main(string[] args)
{
Server server = new Server();
server.StartUp();
while(true)
{
string str = Console.ReadLine();
server.SendAll(str);
}
}
}
public class Server
{
private string _ip = "192.168.30.16";
private int _port = 10000;
private Socket _server;
private Thread _acceptClientConnectThread;
private List<Socket> _clientList = new List<Socket>();
public void StartUp()
{
try
{
_server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint endPoint = new IPEndPoint(GetIpv4(NetworkInterfaceType.Ethernet), _port);
_server.Bind(endPoint);
_server.Listen(30);
_acceptClientConnectThread = new Thread(AcceptClientConnect);
_acceptClientConnectThread.Start();
Console.WriteLine("{0}:{1} StartUp...", _ip, _port);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
public void AcceptClientConnect()
{
while(true)
{
try
{
Socket clientSocket = _server.Accept();
_clientList.Add(clientSocket);
IPEndPoint clientEndPoint = clientSocket.RemoteEndPoint as IPEndPoint;
Console.WriteLine("{0}:{1} Connect...", clientEndPoint.Address.ToString(), clientEndPoint.Port);
Thread acceptClientMsg = new Thread(AcceptMsg);
acceptClientMsg.Start(clientSocket);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public void AcceptMsg(object obj)
{
Socket client = obj as Socket;
byte[] buffer = new byte[client.ReceiveBufferSize];
IPEndPoint clientEndPoint = client.RemoteEndPoint as IPEndPoint;
try
{
while (true)
{
int len = client.Receive(buffer);
string str = Encoding.UTF8.GetString(buffer, 0, len);
Console.WriteLine("Receive {0} : {1} :{2}", clientEndPoint.Address.ToString(), _port, str);
}
}
catch(SocketException e)
{
Console.WriteLine(e.Message);
_clientList.Remove(client);
}
}
public void Send(string str,Socket client)
{
try
{
byte[] strBytes = Encoding.UTF8.GetBytes(str);
client.Send(strBytes);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
public void SendAll(string str)
{
for (int i = 0; i < _clientList.Count; i++)
{
Send(str, _clientList[i]);
}
}
public IPAddress GetIpv4(NetworkInterfaceType type)
{
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
for (int i = 0; i < networkInterfaces.Length; i++)
{
if(type == networkInterfaces[i].NetworkInterfaceType && networkInterfaces[i].OperationalStatus == OperationalStatus.Up)
{
UnicastIPAddressInformationCollection ips = networkInterfaces[i].GetIPProperties().UnicastAddresses;
foreach (UnicastIPAddressInformation item in ips)
{
if(item.Address.AddressFamily == AddressFamily.InterNetwork)
{
return item.Address;
}
}
}
}
return null;
}
public void Close()
{
if(_clientList.Count > 0)
{
for (int i = 0; i < _clientList.Count; i++)
{
_clientList[i].Close();
}
}
_clientList.Clear();
_server.Close();
_acceptClientConnectThread.Abort();
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
namespace TcpClient
{
class Program
{
static void Main(string[] args)
{
Client client = new Client();
client.StartUp();
while (true)
{
string str = Console.ReadLine();
client.Send(str);
}
}
}
public class Client
{
private Socket _client;
private Thread _acceptServerMsg;
public void StartUp()
{
try
{
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_client.Connect("192.168.30.16", 10000);
_acceptServerMsg = new Thread(AcceptServerMsg);
_acceptServerMsg.Start();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
public void AcceptServerMsg()
{
byte[] buffer = new byte[1024 * 64];
while(true)
{
try
{
int len = _client.Receive(buffer);
string str = Encoding.UTF8.GetString(buffer, 0, len);
Console.WriteLine("Reveive Msg From Server : {0}", str);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public void Send(string str)
{
try
{
byte[] strBytes = Encoding.UTF8.GetBytes(str);
_client.Send(strBytes);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
public void Close()
{
if (_client.Connected)
{
_client.Close();
}
_acceptServerMsg.Abort();
}
}
}