[WebMethod]
public string SendMsgToPort(string ip, int port)
{
IPAddress ipaddress = IPAddress.Parse(ip);//转换IP地址
IPEndPoint ipe = new IPEndPoint(ipaddress, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//建立连接
s.Connect(ipe);
byte[] bytes = { 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 };
s.Send(bytes);
s.Close();
return "";
}
public string SendMsgToPort(string ip, int port)
{
IPAddress ipaddress = IPAddress.Parse(ip);//转换IP地址
IPEndPoint ipe = new IPEndPoint(ipaddress, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//建立连接
s.Connect(ipe);
byte[] bytes = { 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 };
s.Send(bytes);
s.Close();
return "";
}
TCP消息发送
本文介绍了一个通过TCP协议向指定IP及端口发送固定字节的方法。使用C#创建Socket连接,并发送预定义的字节数组数据。该方法首先解析输入的IP地址,然后建立TCP连接并发送数据。
2203

被折叠的 条评论
为什么被折叠?



