最近因为公司项目的需求,CAD作为服务端在服务器中常驻运行,等待客户端远程发送执行任务的指令,最终确认用Socket-tcp通讯,CAD需要实时监听客户端发送的消息,这时就需要开启线程执行Socket的监听任务。
这样问题就来了,CADAPI不支持多线程,您只能从主线程调用API函数,如果您处于不同的线程,则必须将调用集中到主线程上。
最后查了半天的资料发现,最简单的实现方式,是建立在主线程system.windows.forms.control对象并调用它的启动功能,做最后处理invoke()功能。
下面分别是客户端和服务端的demo
class Program
{
private static byte[] result = new byte[];
static void Main(string[] args)
{
//设定服务器IP地址
IPAddress ip = IPAddress.Parse("10.19.10.237");
//IPAddress ip = IPAddress.Parse("127.0.0.1");
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSocket.Connect(new IPEndPoint(ip, )); //配置服务器IP与端口
Console.WriteLine("连接服务器成功");
}
catch
{
Console.WriteLine("连接服务器失败,请按回车键退出!");
return;
}
//通过clientSocket接收数据
int receiveLength = clientSocket.Receive(result);
Console.WriteLine("接收服务器消息:{0}", Encoding.UTF8.GetString(result, , receiveLength));
//通过 clientSocket 发送数据
for (int i = ; i < ; i++)
{
try
{
Thread.Sleep(); //等待1秒钟
string sendMessage = "C:\\Users\\Administrator\\Desktop\\原始CAD文件\\14412-kujiale.dxf";
clientSocket.Send(Encoding.UTF8.GetBytes(sendMessage));
Console.WriteLine("向服务器发送消息:{0}" + sendMessage);
receiveLength = clientSocket.Receive(result);
Console.WriteLine("接收服务器消息:{0}", Encoding.UTF8.GetString(result, , receiveLength));
}
catch
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
break;
}
}
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
Console.WriteLine("发送完毕,按回车键退出");
Console.ReadLine();
}
}
Socket客户端-控制台测试
public class SocketServer
{
//通过winform控件进行后台处理,CADAPI不支持多线程
static Control syncCtrl;
//承载接收数据
private static byte[] result = new byte[];
//端口
private static int myProt = ; private static Socket serverSocket; delegate void FinishedProcessingDelegate();
public static void InitSocket()
{
//创建控件
syncCtrl = new Control();
syncCtrl.CreateControl();
//创建通讯对象
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//绑定IP地址:端口
serverSocket.Bind(new IPEndPoint(IPAddress.Any, myProt));
//设定最多10个排队连接请求
serverSocket.Listen();
//通过Clientsoket发送数据
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
}
/// <summary>
/// 监听客户端连接
/// </summary>
private static void ListenClientConnect()
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
try
{ clientSocket.Send(Encoding.UTF8.GetBytes("我是牛魔王"));
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
}
catch (Exception)
{ clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
} }
} /// <summary>
/// 接收消息
/// </summary>
/// <param name="clientSocket"></param>
private static void ReceiveMessage(object clientSocket)
{
Socket myClientSocket = (Socket)clientSocket;
while (true)
{
try
{
//通过clientSocket接收数据
int receiveNumber = myClientSocket.Receive(result);
if (receiveNumber>)
{
//获取文件名称
string filePath = Encoding.UTF8.GetString(result, , receiveNumber);
//判断文件是否存在
if (FileOperate.FileExists(filePath))
{
FileStandardization.filePathByJava = filePath;
if (syncCtrl.InvokeRequired)
{
//开始执行CAD数据处理的任务
syncCtrl.Invoke(new FinishedProcessingDelegate(FileStandardization.TransMianByJava));
}
myClientSocket.Send(Encoding.UTF8.GetBytes("我是牛魔王:处理结果是" + FileStandardization.resultJava));
FileOperate.Log(SystemData.logPath, "我是牛魔王,文件处理完成:" + filePath);
}
else
{
myClientSocket.Send(Encoding.UTF8.GetBytes("我是牛魔王:文件不存在"));
}
}
else
{
FileOperate.Log(SystemData.logPath, "没有收到数据了");
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
break;
} }
catch (Exception ex)
{
FileOperate.Log(SystemData.logPath,"文件处理异常:"+ex.Message);
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
break;
}
}
} }
Socket服务端