public void startServer() { if (server == null && WSServer == null) { server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); String localIP = Utils.getConfig("localIP"); if (localIP != null && !"".Equals(localIP)) { try { isServe = true; if (!gc.IsConnected(0)) //接入手套 { var PortName = cbb_port.SelectedItem.ToString(); gc.Connect(PortName, 0); lbl_gloveStatus.Content = "手套已接入"; } server.Bind(new IPEndPoint(IPAddress.Parse(localIP), 6001)); //绑定端口号和IP Thread t = new Thread(sendMsg); //开启发送消息线程 t.IsBackground = true; t.Start(); WSServer = new WebSocketServer(String.Format("ws://{0}", localIP));//new WebSocket WSServer.AddWebSocketService <GloveData>(GloveDataServerName); WSServer.AddWebSocketService <CommandData>(CommandDataServerName); WSServer.Start(); } catch (NullReferenceException) { MessageBox.Show("请检查手套是否连接正常", "出错了"); throw new Exception("这是已处理错误"); } catch (IOException) { MessageBox.Show("请检查手套是否连接正常", "出错了"); throw new Exception("这是已处理错误"); } catch (FormatException) { MessageBox.Show("源IP地址无效", "出错了"); throw new Exception("这是已处理错误"); } catch (Exception e) { MessageBox.Show(e.ToString(), "出错了"); } } else { MessageBox.Show("本机IP地址不能为空", "出错了"); } } else { MessageBox.Show("服务启动失败,端口已被占用", "出错了"); } }
public void Conected() { if (!gc.IsConnected((int)handType)) //接入手套 { gc.Connect(chuankou, 0); //连接手套和串口 } }