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)开放系统互联参 ...
随机推荐
- jQuery动画知识总结
jQuery动画概述 我们之前实现的下拉菜单的案例,是没有动画效果的,但是在日常开发中,动画效果是经常会用到的,所以我们可以尝试使用jQuery动画,将下拉菜单案例实现的更动感一些. jQuery提供 ...
- vue首次进入微信没有标题问题
首先实在路由改变的时候可以有标题的 首次进入路由不显示标题,查到很多,最有解决可以自定义标签, 后者引入一个包vue-wechat-title,我就是用的后者,前面的没有式过 上地址 http ...
- 励志:98岁老爷爷用Windows系统自带画图软件制作的神作
哈尔拉斯科,是一位很出名的老爷爷,他70岁才接触MS Paint(就是我们熟知的Windows自带的画图软件).他曾经是一名图形艺术家,但是之前他都是手工创作.他熟知怎么用双手进行艺术创作.但是后来, ...
- redis 安装成功后外部服务器链接不上
1.reids服务器的6379端口telnet不通 2. 查看reids进程和端口,都是存在的.只是ip地址是127.0.0.1而不是0.0.0.0,只是本机能使用 3.查找redis的配置文件red ...
- jquery为元素绑定事件
语法 $(selector).live(event,data,function) 参数event 必需,规定附加到元素的一个或多个事件.由空格分隔多个事件,必须是有效的事件.data 可选,规定传递到 ...
- HDU1114 - Piggy-Bank
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. ...
- 训练1-E
有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程. 例如: x + y = 9,x * y ...
- 运行级别(init)
一.运行级别(查看:cat /etc/inittab) 级别: 0:关机 1:服务器出问题(单用户状态) 2:无NFS的多用户模式 3:完整的多用户模式 4:无保留无使用 5:桌面模式 6:重新启动 ...
- Python JSON - 世界人口图
世界人口图 从https://datahub.io/网站搜索population,下载世界人口json数据. from pygal.maps.world import COUNTRIES def ge ...
- Redis 报错:MISCONF Redis is configured to save RDB snapshots
MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Com ...