千家信息网

如何实现.net socket客户端

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,本篇内容介绍了"如何实现.net socket客户端"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!客
千家信息网最后更新 2026年11月04日如何实现.net socket客户端

本篇内容介绍了"如何实现.net socket客户端"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

客户端代码

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Net;

using System.Threading;

using System.Net.Sockets;

namespace W.Common

{

public class CacheSocket

{

public Socket skClient;

public string ip = string.Empty;

public int port = -1;

public int netID;

// public int timeSleep = 1;

//每次接收发送的临时信息

private byte[] sendData;//发送的信息

private byte[] receiveData = new byte[1024];//接收信息

private int receiveN;

private bool isErr = false;

//--------

public CacheSocket(int pNetID)

{

this.netID = pNetID;

GetConfig();

Connection();

Cmd("netid:" + this.netID);

}

public CacheSocket(int pNetID, string pIP, int pPort)

{

this.ip = pIP;

this.port = pPort;

Connection();

Cmd("netid:" + pNetID);

}

public string Cmd(string key)

{

lock (this)//一个信息发送后再接收为一次完成过程

{

this.sendData = Encoding.UTF8.GetBytes(key);

try

{

this.skClient.Send(this.sendData);

}

catch (Exception ex)

{

isErr = true;

("Send" + ex.Message).WriteLine();

ReSocket(() => { this.skClient.Send(this.sendData); });

}

try

{

this.receiveN = this.skClient.Receive(this.receiveData);

}

catch (Exception ex)

{

isErr = true;

ReSocket(() => { this.receiveN = this.skClient.Receive(this.receiveData); });

("Receive" + ex.Message).WriteLine();

}

return Encoding.UTF8.GetString(this.receiveData, 0, this.receiveN);

}

}

public delegate void ReSocket_D();

private void ReSocket(ReSocket_D d)

{

if (isErr)

{

Connection();

this.sendData = Encoding.UTF8.GetBytes("netid:" + this.netID);

this.skClient.Send(this.sendData);

this.receiveN = this.skClient.Receive(this.receiveData);

if (Encoding.UTF8.GetString(this.receiveData, 0, this.receiveN) != "1")

{

}

d();

this.isErr = false;

}

}

#region 获取IP和端口

private void GetConfig()

{

this.ip = "127.0.0.1";

this.port = 1234;

}

#endregion

#region 连接套接字

private void Connection()

{

this.skClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPEndPoint ie = new IPEndPoint(IPAddress.Parse(this.ip), this.port);//服务器的IP和端口

skClient.Connect(ie);

byte[] data = new byte[7];

this.receiveN = this.skClient.Receive(data);

string s = Encoding.UTF8.GetString(data, 0, this.receiveN);

if (s != "success")

{

throw new Exception("连接不成功" + s);

}

}

#endregion

}

}

使用方法

复制代码 代码如下:

public static readonly CacheSocket cac=new CacheSocket(2);

cac.Cmd("发送内容");

"如何实现.net socket客户端"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

代码 信息 客户 客户端 内容 更多 知识 端口 过程 实用 成功 学有所成 接下来 使用方法 困境 套接字 实际 情况 文章 方法 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网高科技 南京欣网互联网络科技 互联网科技工作委员会毛军 没上市的科技互联网公司 中国互联网科技发展 互联网科技缩写 易通互联网络科技有限公司 北京禾田雨橡互联网科技 深圳天众互联网科技公司 互联网金融科技委员会 四个字互联网科技公司取名 农村开互联网科技讲堂 西藏七维空间互联网科技有限公司 互联网企业和科技型企业 互联网为啥属于高科技 高科技互联网发展前景如何 互联网第三次科技革命特征 福建互联网展示科技馆设计 众联互联网络科技 科技互联网专业工作会议发言稿 互联网科技与金融科技 上海挚品互联网科技 新华互联网科技校企合作 广西拼得多互联网科技有限公司 阿里互联网科技企业 山东有哪些互联网科技公司 互联网科技资金流入 富国互联网金融科技混合基金 第三届互联网大会安防黑科技 互联网科技大会大佬
0