热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【socket】学习日志——01

资源来源于b站——斯蒂芬朱z---------------------------------------------建立客户端-------------------------

资源来源于b站——斯蒂芬朱z
---------------------------------------------建立客户端----------------------------------------------




  • 1.打开自己VS,点击控制台程序
    在这里插入图片描述



  • 2.新建文件夹和相应的类,当建立控制台程序时就已经建立了执行的主程序入口
    在这里插入图片描述



  • 3.以下是为代码



一、【ServerDemo】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;public class ServerDemo
{///

/// 案例服务器/// Socket socket;/// /// IP/// private string ip;/// /// 端口号/// private int port;/// /// 一次接收容量/// private int maxBuffer = 1024;/// /// 缓冲/// private byte[] buffer;/// /// /// private Socket client;public ServerDemo(string _ip,int _port){this.ip = _ip;this.port = _port;}public void StartConnent(){try{socket = new Socket(AddressFamily.InterNetwork ,SocketType.Stream ,ProtocolType.Tcp);IPEndPoint iep = new IPEndPoint(IPAddress.Parse(ip),port);socket.Bind(iep);socket.Listen(1000);Console.WriteLine("服务器开启成功!");Console.WriteLine("监控客户端连接!");//--有阻塞效果,并且获得客户端的实例--client = socket.Accept();Console.WriteLine("有客户端连接!");Receive();}catch (Exception e){Console.WriteLine("服务器开启失败!");Console.WriteLine(e.ToString());}}/// /// 接收客户端的信息/// public void Receive(){buffer = new byte[maxBuffer];try{Console.WriteLine("准备BeginReceive");client.BeginReceive(buffer,0,1024,0,new AsyncCallback(RegRece),client);//客户端发送时调用RegReceConsole.WriteLine("执行完BeginReceive");//while (true)//{// int length = client.Receive(buffer);// Console.WriteLine("收到了客户端的消息" + "字节的长度=" + length);// string cOntent= Encoding.UTF8.GetString(buffer, 0, length);// Console.WriteLine("内容=" + content);//}////int length = client.Receive(buffer);////Console.WriteLine("收到了客户端的消息"+"字节的长度="+length);////string cOntent= Encoding.UTF8.GetString(buffer,0,length);////Console.WriteLine("内容=" + content);////string serverCOntent= "你好,我是服务端";////byte[] bytes = Encoding.UTF8.GetBytes(serverContent);////client.Send(bytes);//////释放客户端的资源 ////client.Close();}catch (Exception){}}private void RegRece(IAsyncResult ar)//子线程{Socket so=(Socket)ar.AsyncState;Console.WriteLine("收到了客户端的消息");int length = so.EndReceive(ar);Console.WriteLine("收到了客户端的消息" + "字节的长度=" + length);//string cOntent= Encoding.UTF8.GetString(buffer,0,length );//Console.WriteLine("内容=" + content);int i = BitConverter.ToInt32(buffer,0);Console.WriteLine("数字=" + i);//--清空再回调buffer = new byte[maxBuffer];client.BeginReceive(buffer, 0, 1024, 0, new AsyncCallback(RegRece), client);}
}

二、【Program】类【入口】

using System;namespace Serve
{class Program{static void Main(string[] args){ServerDemo sd = new ServerDemo("XXX.XXX.X.XXX",30000);sd.StartConnent();sd.Receive();Console.ReadKey();}}
}

以上的(new ServerDemo(“XXX.XXX.X.XXX”,30000);)IP自己写自己的地址:
1.Win+R
2.在这里插入图片描述
3.ipconfig回车即可看到自己的IP地址填入


---------------------------------------------建立客户端----------------------------------------------




  • 1.打开自己VS,点击控制台程序
    在这里插入图片描述

  • 2.新建文件夹和相应的类,当建立控制台程序时就已经建立了执行的主程序入口【同上】

  • 3.代码如下
    一、【Client】

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;public class Client
{Socket socket;IPEndPoint iep;///

/// 一次接收容量/// private int maxBuffer = 1024;/// /// 缓冲/// private byte[] buffer;public Client(){}/// /// 连接服务器/// public void connentServer(){try{socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp);iep = new IPEndPoint(IPAddress.Parse("192.168.1.105"),30000);socket.Connect(iep);Console.WriteLine("连接服务器成功!");int i = 4;byte[] HTT = BitConverter.GetBytes(i);string clientContent = "你好,我是客户端";byte[] bytes = Encoding.UTF8.GetBytes(clientContent);//转换为流,因为以UTF8编码流的形式传输//socket.Send(bytes);//Console.WriteLine("我们发送了"+bytes.Length+"个字节");while (true){Thread.Sleep(1000);//停一秒再循环socket.Send(bytes);Console.WriteLine("发送了" + bytes.Length + "个字节");socket.Send(HTT);Console.WriteLine("发送了长为" + HTT.Length + "的数"+ HTT);}//byte[] array = new byte[maxBuffer];//int length = socket.Receive(array);//Console.WriteLine("收到了服务端的消息,字长为" + length);//string cOntent= Encoding.UTF8.GetString(array);//Console.WriteLine("内容=" + content);}catch (Exception e){Console.WriteLine("连接服务器失败!");}}
}

二、【Program】【入口】
using System;

namespace ClientDemo
{
class Program
{
static void Main(string[] args)
{
Client cd = new Client();
cd.connentServer();
Console.ReadKey();
}
}
}


推荐阅读
author-avatar
手机用户2602886105
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有