数据库socket客户端实例代码分享,Socket服务器端

            byte[] data = new byte[7];

        public delegate void ReSocket_D();
        private void ReSocket(ReSocket_D d)
        {
            if (isErr)
            {
                Connection();

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

        public delegate void ReSocket_D();

                this.sendData = Encoding.UTF8.GetBytes(“netid:” + this.netID);
                this.skClient.Send(this.sendData);

                    default:
                        sendStr = this.netID + ” ” + s;
                        break;
                }
            }
            else
            {
                sendStr = s;//需要删除
            }

        public CacheSocket(int pNetID)

                }

            this.clientIP = (IPEndPoint)this.skClient.RemoteEndPoint;
            Console.WriteLine(“IP:” + clientIP.Address + “:” + clientIP.Port);
            this.sendData = Encoding.UTF8.GetBytes(“success”);
            try
            {
                this.skClient.Send(sendData, sendData.Length, SocketFlags.None);//发送信息
            }
            catch (Exception ex)
            {
                LogBLL.Err(“发送第一次连接成功信息 ThrClient.ThrClient Socket.Send”, ex);
            }
        }

            {

        #region 获取IP和端口
        private void GetConfig()
        {
            this.ip = “127.0.0.1”;    
            this.port = 1234;
        }
        #endregion

        public ThrClient(Socket pSkServer, Socket pSkClient)
        {
            this.skServer = pSkServer;
            this.skClient = pSkClient;

        public int netID;

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

        private static void LL()
        {
            Thread thr = new Thread(ServerListen.Run);
            thr.Start();
        }
    }
}

        public string ip = string.Empty;

                try
                {
                    this.skClient.Send(this.sendData);
                }
                catch (Exception ex)
                {
                    isErr = true;
                    (“Send” + ex.Message).WriteLine();
                    ReSocket(() => { this.skClient.Send(this.sendData); });
                }

            string filename = AppDomain.CurrentDomain.BaseDirectory + “log/” + DateTime.Now.ToString(“yyyy-MM-dd HH”) + “.txt”;
            lock (objLock)
            {
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, true))
                {
                    sw.WriteLine(s);
                    sw.WriteLine(ex.StackTrace);
                    sw.WriteLine(“time:” + DateTime.Now.ToString());
                    sw.WriteLine(“———————————-“);
                }
            }
        }
    }
}

        private int receiveN;

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

                this.sendData = this.GetData();

using System.Linq;

            string s = Encoding.UTF8.GetString(data, 0, this.receiveN);
            if (s != “success”)
            {
                throw new Exception(“连接不成功” + s);
            }
        }
        #endregion
    }
}

 using System;

                this.skClient.Send(this.sendData);

                d();
                this.isErr = false;
            }
        }

 

**使用方法

        public CacheSocket(int pNetID, string pIP, int pPort)
        {
            this.ip = pIP;
            this.port = pPort;
            Connection();
            Cmd(“netid:” + pNetID);
        }

        //———
        private int netID;

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

使用方法

namespace W.Common

        public string Cmd(string key)
        {
            lock (this)//一个信息发送后再接收为一次完成过程
            {
                this.sendData = Encoding.UTF8.GetBytes(key);

namespace TCPServer
{
    public class ThrClient
    {
        private static readonly StringBuilder sb = new StringBuilder();
        private Socket skServer;
        private Socket skClient;
        private IPEndPoint clientIP;

using System.Collections.Generic;

                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();
                }

 

}

            byte[] data = new byte[7];
            this.receiveN = this.skClient.Receive(data);

                ThrClient thrC = new ThrClient(skServer, skClient);
                Thread t = new Thread(thrC.Run);
                t.Start();
            }
        }
    }
}

            if (isErr)

        public CacheSocket(int pNetID)
        {
            this.netID = pNetID;
            GetConfig();
            Connection();
            Cmd(“netid:” + this.netID);
        }

        //每次接收发送的临时信息
        private byte[] sendData;//发送的信息
        private byte[] receiveData = new byte[128];//接收信息
        private int receiveN;
        //——–

        {

                this.receiveN = this.skClient.Receive(this.receiveData);
                if (Encoding.UTF8.GetString(this.receiveData, 0, this.receiveN) != “1”)
                {

            Console.Write(sendStr + ” | “);//需要删除
            return Encoding.UTF8.GetBytes(sendStr + ” | “);
        }
    }

 

cac.Cmd(“发送内容”);

                try
                {
                    skClient.Send(this.sendData);//发送
                }
                catch (Exception ex)
                {
                    LogBLL.Err(“发送 ThrClient.Run Send”, ex);
                    break;
                }
            }
        }

            {

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 TCPServer
{
    class Program
    {
        static void Main(string[] args)
        {
            LL();
        }

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

        //每次接收发送的临时信息
        private byte[] sendData;//发送的信息
        private byte[] receiveData = new byte[1024];//接收信息
        private int receiveN;
        private bool isErr = false;
        //——–

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

 

        #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);

namespace TCPServer
{
    public class LogBLL
    {
        public static readonly object objLock = new object();
        public static void Err(string msg, Exception ex)
        {
            string s = “err:” + msg + “:” + ex.Message;
            Console.WriteLine(“err:” + msg + “:” + ex.Message);

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

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;

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

 

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

        }

 

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

 

                }

namespace TCPServer
{
    public class ServerListen
    {
        public static void Run()
        {
            IPEndPoint serverIP = new IPEndPoint(IPAddress.Any, 1234);//本机预使用的IP和端口
            Socket skServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            skServer.Bind(serverIP);
            skServer.Listen(100);

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

            while (true)
            {
                Socket skClient;
                try
                {
                    //当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
                    skClient = skServer.Accept();
                }
                catch (Exception ex)
                {
                    LogBLL.Err(“接收用户连接失败 ServerListen.Run Socket.Accept”, ex);
                    continue;
                }

            this.netID = pNetID;

        public void Run()
        {
            while (true)
            {
                try
                {
                    this.receiveN = skClient.Receive(this.receiveData);//接收
                    if (this.receiveN == 0)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    LogBLL.Err(“接收 ThrClient.Run Receive”, ex);
                    break;
                }

    }

            if (s.Split(‘-‘).Length > 2)
            {
                //若执行到这里,说明并发有问题
                Console.Write(sendStr + ” | “);
            }

 

 using System;

        private void GetConfig()

        private byte[] GetData()
        {
            string s = Encoding.UTF8.GetString(receiveData, 0, this.receiveN);
            string[] s1 = s.Split(‘:’);
            string sendStr = “null”;
            if (s1.Length >= 2)
            {
                switch (s1[0])
                {
                    case “netid”:
                        this.netID = s1[1].ToInt();
                        sendStr = “1”;
                        break;

            Connection();

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

using System.Net.Sockets;

            if (s != “success”)

using System.Text;

**

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

        public Socket skClient;

                    (“Send” + ex.Message).WriteLine();

 

                    isErr = true;

 

            }

            Cmd(“netid:” + this.netID);

 cac.Cmd(“发送内容”);
 

            GetConfig();

        }

        {

        }

您可能感兴趣的文章:

  • java.net.SocketException: Connection reset
    解决方法
  • 基于Socket的网络连接
    Flex与.NET互操作(一)
  • Asp.net
    Socket客户端(远程发送和接收数据)
  • Visual C#.Net
    网络程序开发-Socket篇
  • .net的socket异步通讯示例分享

                catch (Exception ex)

 

            this.ip = “127.0.0.1”;   

 

            this.port = pPort;

using System.Threading;

        }

using System;

 

        #endregion

            }

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

                }

                {

            Cmd(“netid:” + pNetID);

        {

复制代码 代码如下:

            Connection();

            skClient.Connect(ie);

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

                {

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

        }

        }

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

using System.Net;

        private void ReSocket(ReSocket_D d)

        private void Connection()

                d();

 

using System.Data;

                catch (Exception ex)

            this.port = 1234;

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

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

                Connection();

 

 

        #region 连接套接字

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

{

                    (“Receive” + ex.Message).WriteLine();

                this.isErr = false;

                {

        private bool isErr = false;

                try

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

 

        //——–

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

    {

                {

    public class CacheSocket

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

                }

            this.ip = pIP;

        public int port = -1;

                }

        {

复制代码 代码如下:

            }

                    this.skClient.Send(this.sendData);

                }

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

        #region 获取IP和端口

 

                {

        // public int timeSleep = 1;

        #endregion

                try

 

客户端代码

            {

        {

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

 

        {

 

                    isErr = true;

        public string Cmd(string key)