误区
socket.Accept(); 必须要先绑定IP节点
socket.Listen(10);也是必须先绑定IP 节点
listen 设置需要先设置
socket.Bind(endPoint); 先确定该端口是否占用
最好不用 中文,传递消息。
console 显示中文 需要在 mian 里添加
Console.OutputEncoding = Encoding.GetEncoding("gb2312");
服务端
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.GetEncoding("gb2312");
server obj = new server();
obj = null;
}
客户端
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.GetEncoding("gb2312");//.UTF8;//.GetEncoding("gb2312");
client obj = new client();
obj = null;
}
public class server {
private Socket socket;
private bool _IsListen;
//记录通信用的Socket
Dictionary<string, Socket> dic = new Dictionary<string, Socket>();
public server()
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8900 );
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(endPoint);
socket.Listen(10);
EndPoint ep = (EndPoint)endPoint;
Thread thread = new Thread(AcceptInfo);
thread.Start(socket);
while (true)
{
string strmsg = Console.ReadLine();
if (strmsg == "exit") { break; }
else if (strmsg == "d") { }
else {
Console.WriteLine(dic.Count.ToString());
}
}
foreach (string key in dic.Keys)
{
dic[key].Close();
}
}
private void AcceptInfo(object o)
{
Socket socket = o as Socket;
while (true)
{
try
{
Socket clientSocket = socket.Accept();//如果客户端有请求,生成一个新的Socket
string point = clientSocket.RemoteEndPoint.ToString();
show(point + "连接客户端成功!");
dic.Add(point, clientSocket);
//接收消息
Thread th = new Thread(receiveMsg);
th.Start(clientSocket);
}
catch (Exception ex)
{
break;
}
}
socket.Close();
socket = null;
}
private void receiveMsg(object socket)
{
Socket clientSocket = socket as Socket;
while (true)
{
try
{
byte[] buffer = new byte[1024 * 1024];
int n = clientSocket.Receive(buffer);//接收过来的数据放到buffer
//字节转换成字符串
string words = Encoding.UTF8.GetString(buffer, 0, n);
Console.WriteLine(clientSocket.RemoteEndPoint.ToString() + ":" + words);
if (words == "exit")
{
dic.Remove(clientSocket.RemoteEndPoint.ToString());
break;
}
//处理事务 可以异步 实现 这里可以发送 服务器已接受到你信息 等等 友好信息 表示我收到了
byte[] msg = Encoding.UTF8.GetBytes("已收到message:" + words);
clientSocket.Send(msg);//发送数据,字节数组
}
catch {
break;
}
}
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
private void show(string str)
{
Console.WriteLine(str);
}
}
public class client
{
private Socket _Client;
private IPEndPoint endPoint;
public client()
{
endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8900);
_Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_Client.Connect(endPoint);
show("服务器" + _Client.RemoteEndPoint.ToString());
show("客户端:" + _Client.LocalEndPoint.ToString());
//EndPoint ep = (EndPoint)endPoint;
while (true)
{
string strmsg = Console.ReadLine();
byte[] bytes = Encoding.UTF8.GetBytes(strmsg);
_Client.Send(bytes);
if (strmsg == "exit") { break; }
//定义byte数组存放从客户端接收过来的数据
byte[] buffer = new byte[1024 * 1024];
int n = _Client.Receive(buffer);//将接收过来的数据放到buffer中,并返回实际接受数据的长度
//将字节转换成字符串
string words = Encoding.UTF8.GetString(buffer, 0, n);
Console.WriteLine("received:" + strmsg);
}
_Client.Shutdown(SocketShutdown.Both);
_Client.Close();
}
private void show(string str)
{
Console.WriteLine(str);
}
}