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;
}
在模仿中成长,在创新中成功