.net Socket客户端

本文详细介绍了Socket通信的基本原理及实现方法,包括如何创建Socket、连接服务器、发送和接收数据等核心步骤。通过实例演示了从初始化Socket到完成通信的全过程,旨在帮助读者理解和掌握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, 0this.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, 0this.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, 0this.receiveN);
            
if (s != "success")
            {
                
throw new Exception("连接不成功" + s);
            }
        }
        
#endregion
    }
}

使用方法

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值