Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement. Net socket client

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to implement the. Net socket client". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Client code

The copy code is as follows:

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

/ / receive and send temporary messages each time

Messages sent by private byte [] sendData;//

Private byte [] receiveData = new byte [1024]; / / receive information

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) / / A message is sent and then received as a completion process

{

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 gets IP and port

Private void GetConfig ()

{

This.ip = "127.0.0.1"

This.port = 1234

}

# endregion

# region connection socket

Private void Connection ()

{

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

IPEndPoint ie = new IPEndPoint (IPAddress.Parse (this.ip), this.port); / / IP and port of the server

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 ("connection is not successful" + s)

}

}

# endregion

}

}

Usage

The copy code is as follows:

Public static readonly CacheSocket cac=new CacheSocket (2)

Cac.Cmd ("send content")

This is the end of "how to implement the. Net socket client". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report