C#中网络通信
一、服务端代码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace 网络编程.SockteHandel{/// <summary>/// 服务端处理类/// </summary>publicclassServerHandle{/// <summary>/// 端口号/// </summary>publicstaticstringPoint{ get;set;}/// <summary>/// IP地址/// </summary>publicstaticstringIpSite{ get;set;}/// <summary>/// 服务端信息/// </summary>publicstaticTextBoxServerMsg{ get;set;}/// <summary>/// 接收到客户端的消息/// </summary>publicstaticTextBoxReceiveMsg{ get;set;}/// <summary>/// 发送消息/// </summary>publicstaticTextBoxSendMessage{ get;set;}/// <summary>/// 负责通信的Socket/// </summary>privateSocketConnectionSocket;/// <summary>/// 监听端口/// </summary>publicvoidWatchPort(){//在服务器端创建一个负责监听ID跟端口号 的SocketSocket socketWatch =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//IPAddress ip = IPAddress.Any; //监听IPAddress ip =IPAddress.Parse(IpSite);//创建端口号对象IPEndPoint pointObj =newIPEndPoint(ip,Convert.ToInt32(Point));//监听socketWatch.Bind(pointObj);ServerMsg.Text+="监听成功······\r\n ";//设定最多十个排队连接请求socketWatch.Listen(10);//开启一个新线程Thread thread =newThread(Listen);thread.IsBackground=true;thread.Start(socketWatch);}/// <summary>/// 等待客户端连接,并且创建与之通信的Socket/// </summary>/// <param name="obj"></param>voidListen(object obj){Socket soketlisten = obj asSocket;while(true){//等待客户端连接,并创建一个负责通信的SocketSocket socketSend = soketlisten.Accept();ServerMsg.Text+="连接成功·······"+ socketSend.RemoteEndPoint.ToString()+"\r\n";//开启一个新线程不停接收客户端发来的消息Thread reciveThread =newThread(Recive);reciveThread.IsBackground=true;reciveThread.Start(socketSend);}}/// <summary>/// 服务器端不停接收客户端发来的消息/// </summary>/// <param name="obj"></param>voidRecive(object obj){//接收消息ConnectionSocket= obj asSocket;if(ConnectionSocket==null){return;}while(true){//接收客户端发来信息byte[] buffer =newbyte[1024*1024*2];//实际接收的有效字节数int receiveIndex =ConnectionSocket.Receive(buffer);//以实际接收有效字节数来判断客户端是否下线了if(receiveIndex ==0){break;}string str =Encoding.UTF8.GetString(buffer,0, buffer.Length);ReceiveMsg.Text+= str +"\r\n";}}/// <summary>/// 服务端发送消息/// </summary>publicvoidServerSendMessage(){byte[] buffer =Encoding.UTF8.GetBytes(SendMessage.Text);int connectionIndex =ConnectionSocket.Send(buffer);}}}
二、客户端代码
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace 网络编程.SockteHandle{/// <summary>/// 客户端/// </summary>publicclassClientHandle{/// <summary>/// IP地址/// </summary>publicstaticstringConnectionIp{ get;set;}/// <summary>/// 端口号/// </summary>publicstaticstringPoint{ get;set;}//发送消息publicTextBoxSendMsg{ get;set;}/// <summary>/// 接收消息/// </summary>publicTextBoxReciveMsg{ get;set;}/// <summary>/// 客户端Socket对象/// </summary>publicSocket socketSend =null;/// <summary>/// 创建负责通信的Socket/// </summary>publicvoidCreateClientSocket(){socketSend =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPAddress ip =IPAddress.Parse(ConnectionIp);IPEndPoint endPoint =newIPEndPoint(ip,Convert.ToInt32(Point));socketSend.Connect(endPoint);ReciveMsg.Text+="连接到服务器";//创建一个线程来接收服务器端数据Thread reciveThread =newThread(ReciveServerMessage);reciveThread.IsBackground=true;reciveThread.Start(socketSend);}/// <summary>/// 接收服务端信息/// </summary>voidReciveServerMessage(object obj){Socket reciveSocket = obj asSocket;while(true){byte[] buffer =newbyte[1024*1024*2];int reciveIndex = reciveSocket.Receive(buffer);if(reciveIndex ==0){break;}ReciveMsg.Text+="\r\n"+Encoding.UTF8.GetString(buffer)+"\r\n";}}/// <summary>/// 发送消息/// </summary>publicvoidSendMessage(){byte[] buffer =Encoding.UTF8.GetBytes(SendMsg.Text);int sendIndex = socketSend.Send(buffer);}}}
C#中网络通信的更多相关文章
- RocketMq中网络通信之服务端
一,Broker服务端入口(NettyServer端) 首先RocketMq网络通信采用的Netty通信.服务端主要集中在Broker中.我们先看一下Broker的启动类BrokerStartup 显 ...
- Linux中网络通信中 使用的结构体
"+++++++++++++++++++++++++ Linux TCP/UDP通信中的结构体 +++++++++++++++++++++++++++++++++++++++" s ...
- java中网络通信 Scoket
在客户/服务器通信模式中,客户端需要主动建立与服务器连接的Socket,服务器端收到客户端的连接请求,也会创建与客户端连接的Socket.Socket可以看做是通信连接两端的收发器,客户端和服务店都通 ...
- Linux下UPnP sample分析
一.UPnP简介 UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术,建立在TCP/IP.HTTP协 ...
- python高级之网络编程
python高级之网络编程 本节内容 网络通信概念 socket编程 socket模块一些方法 聊天socket实现 远程执行命令及上传文件 socketserver及其源码分析 1.网络通信概念 说 ...
- android考试题
一.选择题 1. Math.round(11.5)等于多少( ). Math.round(-11.5) 等于多少( C ). A.11 ,-11 B.11 ,-12 C.12 ,-1 ...
- 第六篇:python高级之网络编程
python高级之网络编程 python高级之网络编程 本节内容 网络通信概念 socket编程 socket模块一些方法 聊天socket实现 远程执行命令及上传文件 socketserver及 ...
- ZooKeeper 03 - ZooKeeper集群的脑裂问题 (Split Brain问题)
目录 1 ZooKeeper的主从机制 2 什么是ZooKeeper的脑裂 2.1 脑裂现象的表现 2.2 为什么会出现脑裂 3 ZooKeeper如何解决"脑裂" 3.1 3种可 ...
- JAVA自学笔记26
JAVA自学笔记26 1.网络编程 1)用来实现网络互联的不同计算机上运行的程序可以进行数据交换 2)网络模型一般泛指 OSI:(Open System Interconnection)开放系统互联参 ...
随机推荐
- 更换WordPress后台登录地址
在后台找到wp-content—themes—twentyfifteen(当前的网站主题)—functions.php 在代码的最下面加入以下代码: //后台唯一登录地址 add_action('lo ...
- 那些年 IE 下踩过的坑
1年过去了,换了一个不用兼容IE8一下浏览器的工作了! 1.:before,:after(伪类) 所有主流浏览器都支持 :before 选择器. 注释:对于 IE8 及更早版本中的 :before,必 ...
- Delphi中实现文件拷贝的三种方法
1.调用API函数procedure CopyFile(FromFileName,ToFileName:string);varf1,f2:file;BeginAssignFile(f1,FromFil ...
- Maven安装+配置
原先的项目构建属于Ant,就是先export成jar文件,然后引用. Maven依赖一定是引用本地仓库的,所以会先从中央仓库把依赖下载下来存到本地.和NuGet是一样的. 下载 地址 选择一个zip, ...
- vue自己写了一个div菜单,点击按钮展开,点击其他地方关闭这个div菜单
需求是通过点击body页面,在其他地方就关闭这个<div>菜单,给这个div一个id:problemList,但是点击我打开的按钮,不关闭. created () { document.o ...
- ClipboardJS实现点击复制功能
<script src="//lib.baomitu.com/clipboard.js/1.7.1/clipboard.min.js"></script> ...
- ELO kernels 记录
these kernel for discuss how to handle outliers in target values. 一:Ashish Gupta: 在16年6月到18年8月,激活卡的人 ...
- 配置sudo命令行为审计
1.检查是否安装 rpm -aq sudo rsyslog #检验是否安装此软件 ***如果没有需执行(yum install sudo rsyslog -y)安装*** 2.配置审计 echo &q ...
- 新人--使用layui做的表格,复杂表头,固定列,操作单元格数据计算,点击查询重载表格,可以选择部分或者全部导出
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python for Xpath
# Xpath- 在XML文件中查找信息的一套规则/语言,根据XML的元素或者属性进行遍历 ## Xpath开发工具- 开源的Xpath表达式编辑工具:XMLQuire- Chrome插件:Xpath ...