using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.데이터;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public static Socket ClientSocket;
public static string IP = "192.168.0.200";
public static int port = 8899;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void SocketSendMessage(string CMD)
{
IP주소 ip = IP주소.Parse(IP); //将IP주소문자串转换成IP주소实例
ClientSocket = new Socket(주소Family.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//使用指定的주소簇프로토콜、套接字유형和통신 프로토콜
IPEndPoint endPoint = new IPEndPoint(ip, port); // 用指定的ip和포트号初始化IPEndPoint实例
ClientSocket.Connect(endPoint); //与远程主机建立연결
byte[] message = HexStrTobyte(CMD);
ClientSocket.Send(message);
byte[] receive = new byte[message.Length];
int length = ClientSocket.Receive(receive); // length 수신바이트数组길이
richTextBox1.Text = DateTime.Now.ToString("HH:mm:ss") + ":" + byteToHexStr(receive)+"\n" + richTextBox1.Text;
ClientSocket.닫기(); //닫기연결
}
private static byte[] HexStrTobyte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
return returnBytes;
}
// 바이트数组转16진법문자串
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr = returnStr+" "+ bytes[i].ToString("X2");//ToString("X2") 为C#中的문자串형식控制符
}
}
return returnStr;
}
private void button1_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 00 00 01 48 0A");
}
private void button2_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 00 00 00 89 CA");
}
private void button4_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 01 00 01 19 CA");
}
private void button3_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 01 00 00 D8 0A");
}
private void button6_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 02 00 01 E9 CA");
}
private void button5_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 02 00 00 28 0A");
}
private void button8_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 03 00 01 B8 0A");
}
private void button7_Click(object sender, EventArgs e)
{
SocketSendMessage("01 06 00 03 00 00 79 CA");
}
private void button9_Click(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
}
private void button9_Click_1(object sender, EventArgs e)
{
richTextBox1.Text = "";
}
}
}
C # 제어 네트워크 릴레이 코드 예제
무료무료 기술 자료
이 글은 바로 읽을 수 있으며 기초 학습과 검색 유입에 적합합니다.
답글 작성