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)开放系统互联参 ...
随机推荐
- 【转载】程序猿转型AI必须知道的几件事!
历史上AI火过两次,但是最终都已销声匿迹作为结束.这次AI大火的原因:AlphaGo 4比1战胜李世石,相对于一些外行人的恐慌和恐惧,其实很多业内人员在这场世纪之战结束后,都为人类点上了一个大大的赞. ...
- C# 2.0新加特性
泛型(Generics) 泛型是CLR 2.0中引入的最重要的新特性,使得可以在类.方法中对使用的类型进行参数化. 例如,这里定义了一个泛型类: class MyCollection<T> ...
- PhotoZoom放大图片,真的能无损吗?
当然想要无损放大一张很小的图片时,总会有人会和你推荐PhotoZoom这款软件,那么它真的和说的一样,可以无损放大吗?下面小编挑了2张图片做了一下对比. 案例1,我们选取一张尺寸为200x200像素的 ...
- 'input propertychange' 当输入框文字改变时触发的事件!
$('.amount_input').bind('input propertychange', function() { console.log(2); $('.list label').remove ...
- springboot-简介
SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品: 下载地址: Spring Boot 主要目标是: 为所有 Spring ...
- Java什么时候用static,public,private,protected?
这么说吧,假如你是一个类: public表示你愿意其他人看见你的物品(字段.属性),或者你愿意帮别人做事(方法): private表示你不愿意其他任何人看见你的私人物品,也不愿意帮任何人做事: pro ...
- shell问题-报错即退出
如下: #!/bin/bash set -o errexit 在最开头加上 set -o errexit 即可(或者 set -e) 要关闭的时候 set +o errexit (或者 ...
- python爬虫简单架构原理及示例
网页下载器示例: # coding:utf-8 import urllib2 import cookielib url = "http://www.baidu.com" print ...
- 爬虫工具--Beautifusoup
import requests from bs4 import BeautifulSoup s=requests.Session() r=s.get('https://www.tumblr.com/l ...
- k8s的概念
Kubernetes(简称为 K8s),最初由 Google 的工程师开发和设计.Kubernetes 是用于自动部署.扩展和管理容器化应用程序的开源系统,它旨在提供跨主机集群的自动部署.扩展以及运行 ...