作者:手机用户2602886105 | 来源:互联网 | 2023-07-04 14:53
资源来源于b站——斯蒂芬朱z---------------------------------------------建立客户端-------------------------
资源来源于b站——斯蒂芬朱z
---------------------------------------------建立客户端----------------------------------------------
一、【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
{Socket socket;private string ip;private int port;private int maxBuffer = 1024;private byte[] buffer;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());}}public void Receive(){buffer = new byte[maxBuffer];try{Console.WriteLine("准备BeginReceive");client.BeginReceive(buffer,0,1024,0,new AsyncCallback(RegRece),client);Console.WriteLine("执行完BeginReceive");}catch (Exception){}}private void RegRece(IAsyncResult ar){Socket so=(Socket)ar.AsyncState;Console.WriteLine("收到了客户端的消息");int length = so.EndReceive(ar);Console.WriteLine("收到了客户端的消息" + "字节的长度=" + length);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地址填入
---------------------------------------------建立客户端----------------------------------------------
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;private int maxBuffer = 1024;private byte[] buffer;public Client(){}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);while (true){Thread.Sleep(1000);socket.Send(bytes);Console.WriteLine("发送了" + bytes.Length + "个字节");socket.Send(HTT);Console.WriteLine("发送了长为" + HTT.Length + "的数"+ HTT);}}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();
}
}
}