首页 技术 正文
技术 2022年11月6日
0 收藏 725 点赞 1,008 浏览 3524 个字

服务端

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading;

namespace SocketDemo {

class Program     {         static Socket serverSocket;         static Socket clientSocket;         static Thread thread;         static void Main(string[] args)         {             //将网络端点表示为IP地址和端口 用于socket侦听时绑定             IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001);             //使用指定的地址族、套接字类型和协议初始化socket类的新实例             serverSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);             //将所创建的Socket与IPEndPoint绑定             serverSocket.Bind(ipep);             //设置套接字为收听模式 定队列中最多可容纳的等待接受的传入链接数为10             serverSocket.Listen(10);             //在Socket上接收接入的连接             while (true)             {                 //serverSocket.Accept()接收客户端Socket的连接请求 当程序运行到serverSocket.Accept()时会等待直到有客户端Socket发起连接请求时 获取客户端Socket

clientSocket = serverSocket.Accept();                 thread = new Thread(new ThreadStart(Program.doWork));                 thread.Start();             }             Console.Read();         }         private static void doWork()         {

Socket clinet = clientSocket;             //客户端信息             IPEndPoint ipEndPoint = (IPEndPoint)clinet.RemoteEndPoint;             String address = ipEndPoint.Address.ToString();             String port = ipEndPoint.Port.ToString();             Console.WriteLine(address + “:” + port + ” 连接过来了”);             while (true)             {                 Byte[] inBuffer = new Byte[clinet.ReceiveBufferSize];                 Byte[] outBuffer=new Byte[1024];                 String inBufferStr;                 String outBufferStr;                 try                 {                     //如果接收的消息为空 阻塞 当前循环                     clinet.Receive(inBuffer, clinet.ReceiveBufferSize, SocketFlags.None);                 }                 catch(SocketException)                 {                     clinet.Close();                     return;                 }                 inBufferStr = Encoding.ASCII.GetString(inBuffer);                 Console.WriteLine(address + “:” + port + “说:”);                 Console.WriteLine(inBufferStr.Trim());

outBufferStr = Console.ReadLine().Trim();

outBuffer = Encoding.ASCII.GetBytes(outBufferStr);                 clinet.Send(outBuffer, outBuffer.Length, SocketFlags.None);                             }

}     } }

客户端

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net;

namespace clientSocket {     class Program     {         static Socket clientSocket;         static void Main(string[] args)         {             try             {                 //将网络端点表示为IP地址和端口,用于socket侦听时绑定                 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(“127.0.0.1”), 3001);                 clientSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);                 //将socket连接到服务器                 clientSocket.Connect(ipep);                 String outBufferStr;                 Byte[] outBuffer = new Byte[1024];                 Byte[] inBuffer = new Byte[1024];                 //发送消息                 outBufferStr = Console.ReadLine().Trim();                 outBuffer = Encoding.ASCII.GetBytes(outBufferStr);                 clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None);                 Console.WriteLine(“服务器响应:”);                 //接收服务器端信息                 clientSocket.Receive(inBuffer, 1024, SocketFlags.None);                 Console.WriteLine(Encoding.ASCII.GetString(inBuffer));                 Console.ReadLine();             }             catch(Exception exp)             {                 Console.WriteLine(exp.Message);                 Console.ReadLine();             }         }     } }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902