服务端
using System;
class Launch
{
static void Main(string[] args)
{
Server s = new Server("192.168.1.157", 8000);
s.InitServer();
s.Listener(0);
Console.ReadKey();
}
}
---
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
public delegate void RemoveCallBack(ClientSocket cs);
public delegate void BroadCastCallBack(string msg, ClientSocket cs);
public class Server
{
string ip;
int port, index = 0;
IPAddress id;
IPEndPoint iPEndPoint;
List<ClientSocket> clientSockets;
public Socket serverSocket;
public Server(string ip, int port)
{
this.ip = ip;
this.port = port;
clientSockets = new List<ClientSocket>();
}
public void InitServer()
{
AddressFamily af = AddressFamily.InterNetwork;
SocketType st = SocketType.Stream;
ProtocolType pt = ProtocolType.Tcp;
serverSocket = new Socket(af, st, pt);
BindIPAndPort();
}
private void BindIPAndPort()
{
if(string.IsNullOrEmpty(ip) || port == default(int))
{
Console.WriteLine("serverSocket ip or port is null!!!");
return;
}
id = IPAddress.Parse(ip);
iPEndPoint = new IPEndPoint(id, port);
serverSocket.Bind(iPEndPoint);
}
public void Listener(int count = 0)
{
serverSocket.Listen(count);
ServerAccept();
}
private void ServerAccept()
{
serverSocket.BeginAccept(AcceptCallBack, null);
}
private void AcceptCallBack(IAsyncResult ar)
{
Socket clientSocket = serverSocket.EndAccept(ar);
Console.WriteLine("client is connect!!!");
string tmpStr = "server is connect!!!";
byte[] tmpBytes = Encoding.UTF8.GetBytes(tmpStr);
clientSocket.Send(tmpBytes);
AddSocket(clientSocket);
serverSocket.BeginAccept(AcceptCallBack, null);
}
private void AddSocket(Socket socket)
{
ClientSocket cs;
cs = new ClientSocket(socket, RemoveSocket, BroadCast);
cs.Receive();
clientSockets.Add(cs);
Console.WriteLine(++index);
}
public void RemoveSocket(ClientSocket cs)
{
clientSockets.Remove(cs);
}
public void BroadCast(string msg, ClientSocket clientSocket)
{
clientSockets.ForEach(cs => {
if (cs != clientSocket)
cs.ReturnClient(msg);
});
}
~Server()
{
serverSocket.Close();
}
}
---
using System;
using System.Net.Sockets;
using System.Text;
public class ClientSocket
{
SocketFlags sf;
public Socket clientSocket;
public string name;
public byte[] buffer;
public RemoveCallBack cb;
public BroadCastCallBack bc;
public ClientSocket(Socket socket, RemoveCallBack cb, BroadCastCallBack bc)
{
clientSocket = socket;
buffer = new byte[1024];
this.cb = cb;
this.bc = bc;
}
public void Receive()
{
sf = SocketFlags.None;
clientSocket.BeginReceive(buffer, 0, buffer.Length, sf, ReceiveCallBack, null);
}
private void ReceiveCallBack(IAsyncResult ar)
{
try
{
int count = clientSocket.EndReceive(ar);
if(count == 0)
{
clientSocket.Close();
return;
}
string str = Encoding.UTF8.GetString(buffer, 0, count);
if (string.IsNullOrEmpty(name))
{
name = str;
}
else
{
string strComplete = name + ":" + str;
Console.WriteLine(strComplete);
bc(strComplete, this);
}
clientSocket.BeginReceive(buffer, 0, buffer.Length, sf, ReceiveCallBack, null);
}
catch
{
Console.WriteLine(name + "is disconnect!!!");
cb(this);
clientSocket.Close();
}
}
public void ReturnClient(string msg)
{
clientSocket.Send(Encoding.UTF8.GetBytes(msg));
}
}
客户端
using System;
class Launch
{
static void Main(string[] args)
{
Client client = new Client("192.168.1.157", 8000);
client.InitClient();
client.ClientConnect();
while (true)
{
string str = Console.ReadLine();
if (str == "c")
{
client.Close();
break;
}
client.SendMsg(str);
}
}
}
---
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
class Client
{
SocketFlags sf;
Socket clientSocket;
IPEndPoint IPEndPoint;
IPAddress id;
string ip;
int port;
byte[] buffer = new byte[1024];
public Client(string ip, int port)
{
this.ip = ip;
this.port = port;
}
public void InitClient()
{
AddressFamily af = AddressFamily.InterNetwork;
SocketType st = SocketType.Stream;
ProtocolType pt = ProtocolType.Tcp;
clientSocket = new Socket(af, st, pt);
}
public void ClientConnect()
{
if(string.IsNullOrEmpty(ip) || port == default(int))
{
Console.WriteLine("clientServer bind ip or port Error");
return;
}
id = IPAddress.Parse(ip);
IPEndPoint = new IPEndPoint(id, port);
clientSocket.BeginConnect(IPEndPoint, ConnectCallBack, null);
}
private void ConnectCallBack(IAsyncResult ar)
{
try
{
clientSocket.EndConnect(ar);
}
catch
{
Console.WriteLine("client connect is error");
}
try
{
sf = SocketFlags.None;
clientSocket.BeginReceive(buffer, 0, buffer.Length, sf, ReceiveCallBack, null);
}
catch
{
Console.WriteLine("client beginReceive is error!!!");
}
}
private void ReceiveCallBack(IAsyncResult ar)
{
try
{
int count = clientSocket.EndReceive(ar);
string msg = Encoding.UTF8.GetString(buffer, 0, count);
Console.WriteLine(msg);
clientSocket.BeginReceive(buffer, 0, buffer.Length, sf, ReceiveCallBack, null);
}
catch
{
Console.WriteLine("client endreceive is error!!!!");
}
}
public void SendMsg(string str)
{
clientSocket.Send(Encoding.UTF8.GetBytes(str));
}
public void Close()
{
clientSocket.Close();
}
}