这些方法是对socket的一些封装,用法基本和原本用法一样。
Tcp服务端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Lesson1
{
class Program
{
static void Main(string[] args)
{
string ip = "127.0.0.1";
int port = 2333;
IPAddress address = IPAddress.Parse(ip);
//1.对socket进行了一层封装,这个类自动创建socket对象
TcpListener listener = new TcpListener(address, port);
//2.开始监听
listener.Start();
Console.WriteLine("服务器开始侦听");
while (true)
{
//3.等待客户端连接过来
TcpClient client = listener.AcceptTcpClient();//阻塞获取客户端连接
//把client交给SocketClien类,开启新线程读取数据
SocketClient socket = new SocketClient(client);
}
}
}
class SocketClient
{
const int BufferSize = 8192;//定义buffer缓冲区长度
byte[] buffer = new byte[BufferSize];
TcpClient client;
NetworkStream stream;
public SocketClient(TcpClient _client)
{
client = _client;
Console.WriteLine("客户端连接成功:" + client.Client.RemoteEndPoint);
//4.取得客户端发送过来的数据
stream = client.GetStream();//获取网络流,这个网络流可以取得客户端发来的数据
//while (true)
//{
// int readCount = stream.Read(buffer, 0, BufferSize);//阻塞读取网络流
// string msg = Encoding.UTF8.GetString(buffer, 0, readCount);
// Console.WriteLine("接收到" + client.Client.RemoteEndPoint + "的消息:" + msg);
//}
//开启异步读取数据
stream.BeginRead(buffer, 0, BufferSize, Read, null);
}
//子线程读取消息
void Read(IAsyncResult ar)
{
try
{
int readCount = stream.EndRead(ar);
string msg = Encoding.UTF8.GetString(buffer, 0, readCount);
Console.WriteLine("接收到客户端:" + client.Client.RemoteEndPoint + "的消息:" + msg);
lock (client) //线程锁,解决互斥(资源不共享)问题
{
Array.Clear(buffer, 0, BufferSize); //清除缓冲区
stream.BeginRead(buffer, 0, BufferSize, Read, null);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
Tcp客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Lesson2
{
class Program
{
static void Main(string[] args)
{
//新建客户端连接
SocketClient client = new SocketClient();
while (true)
{
Console.WriteLine("请输入消息:");
string msg = Console.ReadLine();
client.Send(msg);
}
}
}
class SocketClient
{
string ip = "127.0.0.1";
int port = 8500;
TcpClient client;
NetworkStream stream;
const int BufferSize = 8192;
byte[] buffer = new byte[BufferSize];
public SocketClient()
{
client = new TcpClient();
client.Connect(ip, port);//连接服务器
Console.WriteLine("连接上服务器:" + client.Client.RemoteEndPoint);
stream = client.GetStream();//获取网络流
//开启异步读取数据
stream.BeginRead(buffer, 0, BufferSize, Read, null);
}
//子线程读取消息
void Read(IAsyncResult ar)
{
try
{
int readCount = stream.EndRead(ar);
string msg = Encoding.UTF8.GetString(buffer, 0, readCount);
Console.WriteLine("接收到客户端:" + client.Client.RemoteEndPoint + "的消息:" + msg);
lock (client)
{
Array.Clear(buffer, 0, BufferSize);
stream.BeginRead(buffer, 0, BufferSize, Read, null);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
//往流里面写入==给服务器发消息
void Write(string msg)
{
byte[] bytes = Encoding.UTF8.GetBytes(msg);
//write用来写入数据,和Send基本一样,Send不用控制长度
stream.Write(bytes, 0, bytes.Length);
}
//向服务器发送消息
public void Send(string msg)
{
Write(msg);
}
}
}
UDP服务端(udp协议不需要连接,所以只有一个UdpClient)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Lesson1
{
class Program
{
static void Main(string[] args)
{
//1.创建udpclient并绑定端口号
UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2333));
//2.接收数据
while (true)
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] data = udpClient.Receive(ref endPoint);//ref获取时哪个ip传过来的,返回一个字节数组就是我们的数据
string msg = Encoding.UTF8.GetString(data);
}
}
}
}
UDP客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace Lesson2
{
class Program
{
static void Main(string[] args)
{
//1.创建udpclient并绑定端口号
UdpClient udpClient = new UdpClient();
//2.发送数据
while (true)
{
string msg = Console.ReadLine();
byte[] data = Encoding.UTF8.GetBytes(msg);
udpClient.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2333));//第三个参数表示要发送到的地方
}
}
}
}












网友评论