using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace KEYENCE90340;
class Program
{
static Socket ClientSocket;
static void Main(string[] args)
{
String IP = "192.168.100.100";
int port = 9004;
IPAddress ip = IPAddress.Parse(IP); //将IP地址字符串转换成IPAddress实例
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//使用指定的地址簇协议、套接字类型和通信协议
IPEndPoint endPoint = new IPEndPoint(ip, port); // 用指定的ip和端口号初始化IPEndPoint实例
ClientSocket.Connect(endPoint); //与远程主机建立连接
Console.WriteLine("开始发送消息");
WriteMessage("开始发送消息。" + DateTime.Now);
byte[] message = Encoding.ASCII.GetBytes("LON\r"); //通信时实际发送的是字节数组,所以要将发送消息转换字节
ClientSocket.Send(message);
Console.WriteLine("发送消息为:" + Encoding.ASCII.GetString(message));
WriteMessage("发送消息为:" + Encoding.ASCII.GetString(message) + "。" + DateTime.Now);
byte[] receive = new byte[1024];
int length = ClientSocket.Receive(receive); // length 接收字节数组长度
Console.WriteLine("接收消息为:" + Encoding.ASCII.GetString(receive));
var result = Encoding.ASCII.GetString(receive);
result = result.Substring(0, result.IndexOf("\r"));
WriteMessage("接收消息为:" + result + "。" + DateTime.Now);
ClientSocket.Close(); //关闭连接
}
/// <summary>
/// 输出指定信息到文本文件
/// </summary>
/// <param name="msg">输出信息</param>
public static void WriteMessage(string msg)
{
using (FileStream fs = new FileStream(@"D:\KEYENCE90340.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("{0}\n", msg, DateTime.Now);
sw.Flush();
}
}
}
}