当前位置: 首页 > news >正文

天津建设工程合同备案网站网页制作网站制作

天津建设工程合同备案网站,网页制作网站制作,上海装修公司前十强排名榜,给政府做网站报价---部分截图来自 siki学院Unity网络通讯课程 Socket 网络上的两个程序通过一个双向的通信连接实现数据交换,这个连接的一端称为一个 Socket ,Socket 包含了网络通信必须的五种信息 Socket 例子{ 协议: TCP 本地: IP &#xff…

---部分截图来自 siki学院Unity网络通讯课程

Socket 

网络上的两个程序通过一个双向的通信连接实现数据交换,这个连接的一端称为一个 Socket ,Socket 包含了网络通信必须的五种信息

Socket 例子{ 

协议: TCP

本地: IP ,端口

远程: IP ,端口

}

可以通过ipconfig,netstat   -ano 查看 Ip 和端口

创建客户端连接服务端

客户端代码:

using System;
using System.Net.Sockets;namespace wangluo
{class Program{static void Main(string[] args){//创建一个Socket 需要using System.Net.Sockets;Socket tempClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);tempClient.Connect("127.0.0.1", 8888);}}
}

服务端代码:

using System;
using System.Net.Sockets;
using System.Net;namespace Server
{class Program{static void Main(string[] args){Socket tempServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);IPEndPoint tempEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);  //using System.Net;tempServer.Bind(tempEndPoint);  //绑定端口tempServer.Listen(0);  //监听,0表示挂起的队列无限长Console.WriteLine("服务端启动成功");Socket tempConnectClient = tempServer.Accept();Console.WriteLine("客户端连接成功:" + tempConnectClient);}}
}

先开启服务端再开启客户端 

客户端服务端互发信息

客户端代码:

using System;
using System.Net.Sockets;
using System.Text;namespace wangluo
{class Program{static void Main(string[] args){//创建一个Socket 需要using System.Net.Sockets;Socket tempClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);tempClient.Connect("127.0.0.1", 8888);//客户端向服务端发信息string tempSendInfo = Console.ReadLine();//将字符串转换成 bufferbyte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);//发送信息tempClient.Send(tempSendData);//接收来自服务端的信息byte[] tempReadBuffer = new byte[1024];tempClient.Receive(tempReadBuffer);string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);Console.WriteLine("接收到服务端信息:" + tempReadString);}}
}

服务端代码:

using System;
using System.Net.Sockets;
using System.Net;
using System.Text;namespace Server
{class Program{static void Main(string[] args){Socket tempServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);IPEndPoint tempEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);  //using System.Net;tempServer.Bind(tempEndPoint);  //绑定端口tempServer.Listen(0);  //监听,0表示挂起的队列无限长Console.WriteLine("服务端启动成功");Socket tempConnectClient = tempServer.Accept();Console.WriteLine("客户端连接成功:" + tempConnectClient);byte[] tempReadBuffer = new byte[1024];tempConnectClient.Receive(tempReadBuffer);//将 byte 转换成字符串string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);Console.WriteLine("接收到客户端信息:" + tempReadString);//服务端向客户端发信息string tempSendInfo = Console.ReadLine();byte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);tempConnectClient.Send(tempSendData);}}
}

封装一下代码:

客户端:

using System;
using System.Net.Sockets;
using System.Text;namespace wangluo
{class Program{static void Main(string[] args){//创建一个Socket 需要using System.Net.Sockets;Socket tempClient = CreateSocket();tempClient.Connect("127.0.0.1", 8888);Send(tempClient);Receive(tempClient);}static Socket CreateSocket(){return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);}static void Send(Socket tempClient){//客户端向服务端发信息string tempSendInfo = Console.ReadLine();//将字符串转换成 bufferbyte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);//发送信息tempClient.Send(tempSendData);}static void Receive(Socket tempClient){//接收来自服务端的信息byte[] tempReadBuffer = new byte[1024];tempClient.Receive(tempReadBuffer);string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);Console.WriteLine("接收到服务端信息:" + tempReadString);}}
}

 服务端:

using System;
using System.Net.Sockets;
using System.Net;
using System.Text;namespace Server
{class Program{static void Main(string[] args){Socket tempServer = CreateSocket();BindAndListen(tempServer);Socket tempConnectClient = Accept(tempServer);Receive(tempConnectClient);Send(tempConnectClient);}static Socket CreateSocket(){return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);}static void BindAndListen(Socket pSocket){IPEndPoint tempEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);  //using System.Net;pSocket.Bind(tempEndPoint);  //绑定端口pSocket.Listen(0);  //监听,0表示挂起的队列无限长Console.WriteLine("服务端启动成功");}static Socket Accept(Socket pSocket){Socket tempConnectClient = pSocket.Accept();Console.WriteLine("客户端连接成功:" + tempConnectClient);return tempConnectClient;}static void Receive(Socket pSocket){byte[] tempReadBuffer = new byte[1024];pSocket.Receive(tempReadBuffer);//将 byte 转换成字符串string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);Console.WriteLine("接收到客户端信息:" + tempReadString);}static void Send(Socket pSocket){//服务端向客户端发信息string tempSendInfo = Console.ReadLine();byte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);pSocket.Send(tempSendData);}}
}

Socket 代码解析:

AddressFamily: InterNetwork 使用IPv4    InterNetworkV6 使用IPv6

SocketType:

异步

客户端:异步 Connect

// Main
tempClient.BeginConnect("127.0.0.1", 8888, ConnectCallback, tempClient);static void ConnectCallback(IAsyncResult ar){//beginconnect函数中最后一个参数就是用来作为callback的参数使用//将其由 object 类型强制转换成 socket 类型Socket tempSocket = (Socket)ar.AsyncState;//与 BeginConnect 配套的 EndConnect//异步的结束tempSocket.EndConnect(ar);Console.WriteLine("连接服务器成功");Send(tempSocket);Receive(tempSocket);Close(tempSocket);}

异步 Receive

//接收来自服务端的信息  调用 Receive 的地方tempSocket.BeginReceive(tempReadBuffer, 0, 1024,SocketFlags.None, ReceiveCallback, tempSocket);static void ReceiveCallback(IAsyncResult ar){        Socket tempSocket = (Socket)ar.AsyncState;tempSocket.EndReceive(ar);string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);Console.WriteLine("接收到服务端信息:" + tempReadString);Close(tempSocket);}

异常处理:

static void ReceiveCallback(IAsyncResult ar){try{Socket tempSocket = (Socket)ar.AsyncState;tempSocket.EndReceive(ar);string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);Console.WriteLine("接收到服务端信息:" + tempReadString);Close(tempSocket);}catch (Exception ex){Console.WriteLine("连接异常" + ex.ToString());}}

异步send

tempClient.BeginSend(tempSendData,0,tempSendData.Length,SocketFlags.None, SendCallback, tempClient);static void SendCallback(IAsyncResult ar){try{Socket tempSocket = (Socket)ar.AsyncState;tempSocket.EndSend(ar);Console.WriteLine("发送数据成功");}catch (Exception ex){Console.WriteLine("发送异常");}

总览:

using System;
using System.Net.Sockets;
using System.Text;namespace wangluo
{class Program{static private byte[] tempReadBuffer = new byte[1024];static void Main(string[] args){//创建一个Socket 需要using System.Net.Sockets;Socket tempClient = CreateSocket();tempClient.BeginConnect("127.0.0.1", 8888, ConnectCallback, tempClient);System.Threading.Thread.Sleep(1000);}static Socket CreateSocket(){return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);}static void Send(Socket tempClient){//客户端向服务端发信息string tempSendInfo = Console.ReadLine();//将字符串转换成 bufferbyte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);//发送信息tempClient.BeginSend(tempSendData,0,tempSendData.Length,SocketFlags.None, SendCallback, tempClient);}static void Close(Socket pSocket){pSocket.Close();}static void ConnectCallback(IAsyncResult ar){//beginconnect函数中最后一个参数就是用来作为callback的参数使用//将其由 object 类型强制转换成 socket 类型Socket tempSocket = (Socket)ar.AsyncState;//与 BeginConnect 配套的 EndConnect//异步的结束tempSocket.EndConnect(ar);Console.WriteLine("连接服务器成功");Send(tempSocket);//接收来自服务端的信息tempSocket.BeginReceive(tempReadBuffer, 0, 1024, SocketFlags.None, ReceiveCallback, tempSocket);}static void ReceiveCallback(IAsyncResult ar){try{Socket tempSocket = (Socket)ar.AsyncState;tempSocket.EndReceive(ar);string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);Console.WriteLine("接收到服务端信息:" + tempReadString);Close(tempSocket);}catch (Exception ex){Console.WriteLine("接收失败" + ex.ToString());}}static void SendCallback(IAsyncResult ar){try{Socket tempSocket = (Socket)ar.AsyncState;tempSocket.EndSend(ar);Console.WriteLine("发送数据成功");}catch (Exception ex){Console.WriteLine("发送异常");}}}
}

服务端:异步 Accept

//main
tempServer.BeginAccept(AcceptCallback, tempServer);static void AcceptCallback(IAsyncResult ar){try{Socket tempSeverSocket = (Socket)ar.AsyncState;Socket tempClientSocket = tempSeverSocket.EndAccept(ar);Console.WriteLine("客户端接收成功:" + ((IPEndPoint)tempClientSocket.RemoteEndPoint));Receive(tempClientSocket);Send(tempClientSocket);}catch (Exception ex){Console.WriteLine("接收客户端失败");}}

服务端:异步Send和异步Receive

using System;
using System.Net.Sockets;
using System.Net;
using System.Text;namespace Server
{class Program{static byte[] readBuffer = new byte[1024];static void Main(string[] args){Socket tempServer = CreateSocket();BindAndListen(tempServer);tempServer.BeginAccept(AcceptCallback, tempServer);System.Threading.Thread.Sleep(1000);}static Socket CreateSocket(){return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);}static void BindAndListen(Socket pSocket){IPEndPoint tempEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);  //using System.Net;pSocket.Bind(tempEndPoint);  //绑定端口pSocket.Listen(0);  //监听,0表示挂起的队列无限长Console.WriteLine("服务端启动成功");}static void Send(Socket pSocket){//服务端向客户端发信息string tempSendInfo = Console.ReadLine();byte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);pSocket.BeginSend(tempSendData, 0, tempSendData.Length, SocketFlags.None, SendCallback, pSocket);}static void Close(Socket pSocket){pSocket.Close();}static void AcceptCallback(IAsyncResult ar){try{Socket tempSeverSocket = (Socket)ar.AsyncState;Socket tempClientSocket = tempSeverSocket.EndAccept(ar);Console.WriteLine("客户端接收成功:" + ((IPEndPoint)tempClientSocket.RemoteEndPoint));tempClientSocket.BeginReceive(readBuffer, 0, 1024, SocketFlags.None,ReceiveCallback, tempClientSocket);Send(tempClientSocket);}catch (Exception ex){Console.WriteLine("接收客户端失败");}}static void ReceiveCallback(IAsyncResult ar){try{Socket tempSocket = (Socket)ar.AsyncState;tempSocket.EndReceive(ar);string tempReadString = Encoding.UTF8.GetString(readBuffer);Console.WriteLine("接收到客户端信息:" + tempReadString);Close(tempSocket);}catch (Exception ex){Console.WriteLine("接收失败" + ex.ToString());}}static void SendCallback(IAsyncResult ar){try{Socket tempSocket = (Socket)ar.AsyncState;tempSocket.EndSend(ar);Console.WriteLine("发送数据成功");}catch (Exception ex){Console.WriteLine("发送异常");}}}
}

让服务端支持多个客户端连接

//用来存放连接到的客户端private static List<Socket> clientList = new List<Socket>();static void AcceptCallback(IAsyncResult ar){Socket tempSeverSocket = (Socket)ar.AsyncState;Socket tempClientSocket = tempSeverSocket.EndAccept(ar);Console.WriteLine("客户端接收成功:" + ((IPEndPoint)tempClientSocket.RemoteEndPoint));tempClientSocket.BeginReceive(readBuffer, 0, 1024, SocketFlags.None,ReceiveCallback, tempClientSocket);tempSeverSocket.BeginAccept(AcceptCallback, tempSeverSocket);clientList.Add(tempClientSocket);Send(tempClientSocket);;}

HTTP协议

C#使用 http 协议的示例: 

private static void Get(){HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.metools.info");request.Method = "GET";//request.AcceptHttpWebResponse response = (HttpWebResponse)request.GetResponse();response.GetResponseStream();Stream responseStream = response.GetResponseStream();string result = new StreamReader(responseStream).ReadToEnd();Console.WriteLine(result);}

FTP 协议

http://www.dt0577.cn/news/244.html

相关文章:

  • 池州公司做网站seo关键词排名优化案例
  • 南皮县网站建设湖南关键词优化推荐
  • 网站发布小说封面怎么做名优网站关键词优化
  • 射阳网站设计怎么弄一个网站
  • o2o网站咋建设百度推广代理商利润
  • qt 做网站网站不收录怎么办
  • 有教做翻糖的网站吗东莞关键词排名优化
  • 伊利网站建设水平评价新乡网站优化公司价格
  • 网站续费多少钱百度网站怎么优化排名靠前
  • 遵义市住房和城乡建设局官方网站短期培训就业学校
  • 做电商网站一般多少钱seo关键词优化的技巧和方法
  • 包头教育云网站建设seo服务内容
  • 免费建站平台哪个稳定软文推广策划方案
  • 浏览器加速器免费版东莞seo建站公司哪家好
  • 网站建设方案书 个人摘抄一则新闻
  • 贵州住建设局官方网站搜索引擎排名优化是什么意思
  • 建站工具大全如何免费引流推广
  • 网站开发项目企划书宁波seo哪家好快速推广
  • wordpress去掉页脚seo关键词是怎么优化的
  • 做h的游戏视频网站百度推广案例及效果
  • 马云做中国最大的网站惠州seo公司
  • 济南企业做网站推广网站好的推广方式
  • bbs网站开发报告安徽疫情最新情况
  • 建设电子商务网站的花费汕头百度网站排名
  • 网站建设服务类型现状2020十大网络热词
  • 展览网站制作长沙seo代理商
  • 网站的定位山东济南seo整站优化费用
  • 西宁seo网站建设石家庄关键词排名提升
  • 常州钟楼建设局网站营销咨询师
  • 学校精品课网站怎么做百度推广登录入口官网