惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
U
Unit 42
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
美团技术团队
雷峰网
雷峰网
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
D
DataBreaches.Net
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
罗磊的独立博客
MyScale Blog
MyScale Blog
博客园_首页
IT之家
IT之家
F
Fortinet All Blogs
博客园 - Franky

博客园 - huadust

msdn 委托用法 注册表操作 DateTime各种用法 [转]将dll汇入exe 用DataTable构建树 Lock Review String Review Class Review DataTable opertion Drag DataGridView Data To TreeView 2009_01_15_星期三 2009_01_02_星期五 2008_12_31_星期三 2008_12_24_星期三 2008_12_20_星期六 2008_12_17_星期三 2008_12_06_星期六 2008_12_04_星期四
网络编程
huadust · 2011-04-07 · via 博客园 - huadust

Uri 类

TcpClient 类

TcpServer 类

///////////////////////////////////////////////////////////////////////////

//Socket操作///////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////

using System.Net;

using System.Net.Sockets;

private Socket m_ConnectSocket = null;

private Socket m_ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

//Socket连接

public bool ConnectServer(string sHostName, int nPort)

{

    bool bRet = false;

    IPAddress serverIP = XIpOP.GetHostIP(sHostName);

    IPEndPoint endPoint = new IPEndPoint(serverIP, nPort);

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

    m_ConnectSocket.Connect(endPoint);

    bRet = m_ConnectSocket.Connected;

    return bRet;

}

//Socket断开连接

public bool DisconnectServer()

{

    if (m_ConnectSocket != null)

    {

        m_ConnectSocket.Shutdown(SocketShutdown.Both);

        m_ConnectSocket.Close();

    }

    return true;

}

//Socket监听

public bool StartListen(int nPort)

{

    bool bResult = false;

    IPAddress localHostIP = XIpOP.GetLocalIP();

    IPEndPoint localEndPoint = new IPEndPoint(localHostIP, nPort);

    m_ListenSocket.Bind(localEndPoint);

    m_ListenSocket.Listen(10);

    if (m_ListenSocket.IsBound == true)

    {

        m_ConnectSocket = m_ListenSocket.Accept();

        bResult = m_ConnectSocket.Connected;

    }

    return bResult;

}

//Socket停止监听

public bool StopListen()

{

    bool bRet = false;

    if ((m_ListenSocket != null) && (m_ListenSocket.IsBound == true))

    {

        if (m_ConnectSocket.Connected == true)

        {

            m_ConnectSocket.Shutdown(SocketShutdown.Both);

            m_ConnectSocket.Close();

        }

        m_ListenSocket.Close();

        bRet = true;

    }

    return bRet;

}

//Socket发送数据

public void SendData(string sSendData)

{

    byte[] sendBuffer = new byte[1024];

    sendBuffer = Encoding.GetEncoding("GBK").GetBytes(sSendData);

    m_ConnectSocket.Send(sendbuffer, 0, sendBuffer.Length, SocketFlags.None);

}

//Socket接收数据

public string ReceiveData()

{

    byte[] receiveBuffer = new byte[1024];

    m_ConnectSocket.Receive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None);

    string sRet = "";

    sRet = Encoding.GetEncoding("GBK").GetString(receiveBuffer);

    return sRet;

}

///////////////////////////////////////////////////////////////////////////

//TCPClient操作////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////

using System.Net;

using System.Net.Sockets;

private TcpClient m_ConnectTcpClient = new TcpClient();

private TcpListener m_ConnectTcpListener = new TcpListener(23);

//TcpClient连接

public bool ConnectServer(string sHostName, int nPort)

{

    bool bRet = false;

    IPAddress serverIP = XIpOP.GetHostIP(sHostName);

    IPEndPoint endPoint = new IPEndPoint(serverIP, nPort);

    TcpClient m_ConnectTcpClient = new TcpClient();

    m_ConnectTcpClient.Connect(endPoint);

    bRet = m_ConnectTcpClient.Connected;

    return bRet;

}

//TcpClient断开连接

public bool DisconnectServer()

{

    bool bRet = false;

    m_ConnectTcpClient.Close();

    bRet = true;

    return bRet;

}

//TcpListener监听

public bool StartListen(int nPort)

{

    bool bRet = false;

    m_ConnectTcpListener.Start(6);

    m_ConnectTcpClient = m_ConnectTcpListener.AcceptTcpClient();

    if (m_ConnectTcpListener != null)

    {

        bRet = true;

    }

    return bRet;

}

//TcpListener断开监听

public bool StopListen()

{

    bool bRet = false;

    if (m_ConnectTcpListener != null)

    {

        if (m_ConnectTcpClient.Connected == true)

        {

            m_ConnectTcpClient.Close();

        }

        m_ConnectTcpListener.Stop();

        bRet = true;

    }

    return bRet;

}

//TcpClient发送数据

public void SendData(byte[] writeBuffer)

{

    NetworkStream networkStream = m_ConnectTcpClient.GetStream();

    if (networkStream.CanWrite)

    {

        networkStream.Write(writeBuffer, 0, writeBuffer.Length);

    }

}

//TcpClient接收数据

public byte[] ReceiveData()

{

    byte[] readBuffer = null;

    NetworkStream networkStream = m_ConnectTcpClient.GetStream();

    readBuffer = new byte[1024];

    if (networkStream.CanRead)

    {

        networkStream.Read(readBuffer, 0, readBuffer.Length);

    }

    return readBuffer;

}