资源来源于b站——斯蒂芬朱z
---------------------------------------------建立客户端----------------------------------------------
-
1.打开自己VS,点击控制台程序
-
2.新建文件夹和相应的类,当建立控制台程序时就已经建立了执行的主程序入口
-
3.以下是为代码
一、【ServerDemo】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
public class ServerDemo
{
/// <summary>
/// 案例服务器
/// </summary>
Socket socket;
/// <summary>
/// IP
/// </summary>
private string ip;
/// <summary>
/// 端口号
/// </summary>
private int port;
/// <summary>
/// 一次接收容量
/// </summary>
private int maxBuffer = 1024;
/// <summary>
/// 缓冲
/// </summary>
private byte[] buffer;
/// <summary>
///
/// </summary>
private Socket client;
public ServerDemo(string _ip,int _port)
{
this.ip = _ip;
this.port = _port;
}
public void StartConnent()
{
try
{
socket = new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(ip),port);
socket.Bind(iep);
socket.Listen(1000);
Console.WriteLine("服务器开启成功!");
Console.WriteLine("监控客户端连接!");
//--有阻塞效果,并且获得客户端的实例--
client = socket.Accept();
Console.WriteLine("有客户端连接!");
Receive();
}
catch (Exception e)
{
Console.WriteLine("服务器开启失败!");
Console.WriteLine(e.ToString());
}
}
/// <summary>
/// 接收客户端的信息
/// </summary>
public void Receive()
{
buffer = new byte[maxBuffer];
try
{
Console.WriteLine("准备BeginReceive");
client.BeginReceive(buffer,0,1024,0,new AsyncCallback(RegRece),client);//客户端发送时调用RegRece
Console.WriteLine("执行完BeginReceive");
//while (true)
//{
// int length = client.Receive(buffer);
// Console.WriteLine("收到了客户端的消息" + "字节的长度=" + length);
// string content = Encoding.UTF8.GetString(buffer, 0, length);
// Console.WriteLine("内容=" + content);
//}
int length = client.Receive(buffer);
Console.WriteLine("收到了客户端的消息"+"字节的长度="+length);
string content = Encoding.UTF8.GetString(buffer,0,length);
Console.WriteLine("内容=" + content);
string serverContent = "你好,我是服务端";
byte[] bytes = Encoding.UTF8.GetBytes(serverContent);
client.Send(bytes);
//释放客户端的资源
client.Close();
}
catch (Exception)
{
}
}
private void RegRece(IAsyncResult ar)//子线程
{
Socket so=(Socket)ar.AsyncState;
Console.WriteLine("收到了客户端的消息");
int length = so.EndReceive(ar);
Console.WriteLine("收到了客户端的消息" + "字节的长度=" + length);
//string content = Encoding.UTF8.GetString(buffer,0,length );
//Console.WriteLine("内容=" + content);
int i = BitConverter.ToInt32(buffer,0);
Console.WriteLine("数字=" + i);
//--清空再回调
buffer = new byte[maxBuffer];
client.BeginReceive(buffer, 0, 1024, 0, new AsyncCallback(RegRece), client);
}
}
二、【Program】类【入口】
using System;
namespace Serve
{
class Program
{
static void Main(string[] args)
{
ServerDemo sd = new ServerDemo("XXX.XXX.X.XXX",30000);
sd.StartConnent();
sd.Receive();
Console.ReadKey();
}
}
}
以上的(new ServerDemo(“XXX.XXX.X.XXX”,30000);)IP自己写自己的地址:
1.Win+R
2.
3.ipconfig回车即可看到自己的IP地址填入
---------------------------------------------建立客户端----------------------------------------------
- 1.打开自己VS,点击控制台程序
- 2.新建文件夹和相应的类,当建立控制台程序时就已经建立了执行的主程序入口【同上】
- 3.代码如下
一、【Client】
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class Client
{
Socket socket;
IPEndPoint iep;
/// <summary>
/// 一次接收容量
/// </summary>
private int maxBuffer = 1024;
/// <summary>
/// 缓冲
/// </summary>
private byte[] buffer;
public Client()
{
}
/// <summary>
/// 连接服务器
/// </summary>
public void connentServer()
{
try
{
socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp);
iep = new IPEndPoint(IPAddress.Parse("192.168.1.105"),30000);
socket.Connect(iep);
Console.WriteLine("连接服务器成功!");
int i = 4;
byte[] HTT = BitConverter.GetBytes(i);
string clientContent = "你好,我是客户端";
byte[] bytes = Encoding.UTF8.GetBytes(clientContent);//转换为流,因为以UTF8编码流的形式传输
//socket.Send(bytes);
//Console.WriteLine("我们发送了"+bytes.Length+"个字节");
while (true)
{
Thread.Sleep(1000);//停一秒再循环
socket.Send(bytes);
Console.WriteLine("发送了" + bytes.Length + "个字节");
socket.Send(HTT);
Console.WriteLine("发送了长为" + HTT.Length + "的数"+ HTT);
}
//byte[] array = new byte[maxBuffer];
//int length = socket.Receive(array);
//Console.WriteLine("收到了服务端的消息,字长为" + length);
//string content = Encoding.UTF8.GetString(array);
//Console.WriteLine("内容=" + content);
}
catch (Exception e)
{
Console.WriteLine("连接服务器失败!");
}
}
}
二、【Program】【入口】
using System;
namespace ClientDemo
{
class Program
{
static void Main(string[] args)
{
Client cd = new Client();
cd.connentServer();
Console.ReadKey();
}
}
}