有没有适合托管c++使用的socket编程头文件
展开全部
1.简单服务器端
/*
using namespace System::Data;
using namespace System::Net::Sockets;
using namespace System::Net;
using namespace System::Threading;
*/
private static int port = %%2;
private static Thread thThreadRead;
private static TcpListener TcpListen;
private static bool bListener = true;
private static Socket stRead;
private static void Listen()
{
try
{
TcpListen = gcnew TcpListener(port);
TcpListen.Start();
stRead = TcpListen.AcceptSocket();
EndPoint tempRemoteEP = stRead.RemoteEndPoint;
IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address);
String^ sHostName = host.HostName;
while (bListener)
{
stRead.Send(Encoding.ASCII.GetBytes("Hello"));
String^ sTime = DateTime.Now.ToShortTimeString();
Byte[] byRead = gcnew Byte[1024];
int iRead = stRead.ReceiveFrom(byRead, ref tempRemoteEP);
Byte[] byText = gcnew Byte[iRead];
Array.Copy(byRead, 0, byText, 0, iRead);
String^ line = System.Text.Encoding.Default.GetString(byRead);
}
}
catch (System.Security.SecurityException)
{
//监听失败
}
}
thThreadRead = gcnew Thread(gcnew ThreadStart(Listen));
thThreadRead.Start();
2.简单客户端
/*
using namespace System::Data;
using namespace System::Net::Sockets;
using namespace System::Net;
*/
private static IPEndPoint dateTimeHost;
String^ hostIPString=%%1;
String^ hostPortString=%%2;
IPAddress hostIP=IPAddress.Parse(hostIPString);
dateTimeHost=gcnew IPEndPoint(hostIP,Int32.Parse(hostPortString));
Socket conn=gcnew Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
conn.Connect(dateTimeHost);
int bytes=0;
Byte[] RecvBytes=gcnew Byte[256];
bytes=conn.Receive(RecvBytes,RecvBytes.Length,0);
String^ RecvString=Encoding.ASCII.GetString(RecvBytes,0,bytes);
Console.WriteLine(RecvString);
conn.Shutdown(SocketShutdown.Both);
conn.Close();
3.获得本机IP
//using namespace System::Net;
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
String^ %%1=nullptr;
for (int i = 0; i < addressList.Length; i++)
{
%%1 += addressList[i].ToString();
}
/*
using namespace System::Data;
using namespace System::Net::Sockets;
using namespace System::Net;
using namespace System::Threading;
*/
private static int port = %%2;
private static Thread thThreadRead;
private static TcpListener TcpListen;
private static bool bListener = true;
private static Socket stRead;
private static void Listen()
{
try
{
TcpListen = gcnew TcpListener(port);
TcpListen.Start();
stRead = TcpListen.AcceptSocket();
EndPoint tempRemoteEP = stRead.RemoteEndPoint;
IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;
IPHostEntry host = Dns.GetHostByAddress(tempRemoteIP.Address);
String^ sHostName = host.HostName;
while (bListener)
{
stRead.Send(Encoding.ASCII.GetBytes("Hello"));
String^ sTime = DateTime.Now.ToShortTimeString();
Byte[] byRead = gcnew Byte[1024];
int iRead = stRead.ReceiveFrom(byRead, ref tempRemoteEP);
Byte[] byText = gcnew Byte[iRead];
Array.Copy(byRead, 0, byText, 0, iRead);
String^ line = System.Text.Encoding.Default.GetString(byRead);
}
}
catch (System.Security.SecurityException)
{
//监听失败
}
}
thThreadRead = gcnew Thread(gcnew ThreadStart(Listen));
thThreadRead.Start();
2.简单客户端
/*
using namespace System::Data;
using namespace System::Net::Sockets;
using namespace System::Net;
*/
private static IPEndPoint dateTimeHost;
String^ hostIPString=%%1;
String^ hostPortString=%%2;
IPAddress hostIP=IPAddress.Parse(hostIPString);
dateTimeHost=gcnew IPEndPoint(hostIP,Int32.Parse(hostPortString));
Socket conn=gcnew Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
conn.Connect(dateTimeHost);
int bytes=0;
Byte[] RecvBytes=gcnew Byte[256];
bytes=conn.Receive(RecvBytes,RecvBytes.Length,0);
String^ RecvString=Encoding.ASCII.GetString(RecvBytes,0,bytes);
Console.WriteLine(RecvString);
conn.Shutdown(SocketShutdown.Both);
conn.Close();
3.获得本机IP
//using namespace System::Net;
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
String^ %%1=nullptr;
for (int i = 0; i < addressList.Length; i++)
{
%%1 += addressList[i].ToString();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询