static void NetworkManager_ReceiveMessage(object sender, ReciveEventArgs e) {//なにか受信した時に呼ばれる var message = e.message; int mesleng = message.Length; string mode = ""; string sendmod = ""; string sendtxt = ""; if (mode == "") mode = e.message; //モードの判別準備 if (message.Length > 7) { //int mesLength = message.Length; sendmod = message.Substring(0, 7);//modeを出すため sendtxt = message.Remove(0, 7);//文字列を出すため } if (sendmod == "sendtxt") //文字送受信モード { Console.WriteLine(sendtxt); } if (mode == "sendfile") //ファイル送信モード { string fonamae = Console.ReadLine(); NetworkManager.ReadFile(fonamae); } }
/// <summary> /// 接收信息 /// </summary> /// <param name="ar"></param> public void ReceiveMessage(IAsyncResult ar) { var sok = ar.AsyncState as Socket; var length = 0; try { if (sok.Connected) { length = socket.EndReceive(ar); if (length <= 0) { IniBeginReceive(sok); return; } } } catch (Exception ex) { LogUtil.WriteLog(typeof(TcpClinetSocket), "ReceiveMessage" + ex.Message); IniBeginReceive(sok); } if (length != 0) { //读取出来消息内容 byte[] message = new byte[length]; Array.ConstrainedCopy(buffer, 0, message, 0, length); // Buffer.BlockCopy(buffer, 0, message, 0, length); //注册消息事件 ReciveEventArgs asEventArgs = new ReciveEventArgs(message); ReciveHandler(this, asEventArgs); } IniBeginReceive(sok); }
public void Client_ReciveHandler(object sender, ReciveEventArgs e) { if (e.data.Length > 0) { ReciveEventArgs asEventArgs = new ReciveEventArgs(e.data); ReciveHandler(sender, asEventArgs); byteToObj(e.data); } }
private void SendData(DesoutterData PsetData, bool isHandler = true) { byte[] senddata = PsetData.GetDesoutterData(); if (isHandler) { //发送事件通知 ReciveEventArgs asEventArgs = new ReciveEventArgs(senddata); SendHandler(this, asEventArgs); } client.SendData(senddata); }
private void CallReciveEvent(object sender, ReciveEventArgs e) { if (InvokeRequired) { // 別スレッドから呼び出された場合 object[] param = { sender, e }; Invoke(new CallReciveEventDelegate(CallReciveEvent), param); return; } try { ReciveEvent(sender, e); } catch (Exception es) { LogPrint("ReciveEvent内で例外が発生しました - " + es.Message); } }
static void Server_ReceiveMessage(object sender, ReciveEventArgs e) { var message = e.message; int mesleng = message.Length; string mode = e.message; //モードの判別準備 string sendmod = ""; string sendtxt = ""; if(message.Length > 7) { sendmod = message.Substring(0, 7);//sendtxtを出したい sendtxt = message.Remove(0, 7);//受信したユーザー指定の文字列を出すため } string dlfilename = message.Remove(0, 2); string dl = message.Substring(0, 2); if (dl == "dl") mode = "dl"; if (sendmod == "sendtxt") //文字送受信モード { Console.WriteLine(sendtxt); } if (mode == "sendfile") //ファイル送信モード { Server.FileRead("sendfile.jpg",1);//1 = 画像送受信 } if (mode == "list")//リスト送信 { Console.WriteLine("list"); Server.WriteFile("List.lst"); Server.WriteFile("List.lst");//多分こうしないと送信してくれない } if (mode == "up")//MIDIMETA運指を受け取る { int number; string name = ""; string username = ""; string date = ""; Server.FileRead("meta.tmp",0);//METADATAを受信保存 StreamReader sr = new StreamReader(@"meta.tmp" , System.Text.Encoding.GetEncoding("shift-jis")); int cnt = 0; while (sr.Peek() > -1)//読み込み { if(cnt == 0) name = sr.ReadLine();//1行目 if(cnt == 1) username = sr.ReadLine();//2行目 if(cnt == 2) date = sr.ReadLine();//3行目 cnt++; }//numberは通算番号 number = Server.UpdateList(name, username, date); string rename = number + ".meta"; sr.Close(); File.Move("meta.tmp", rename);//ファイル名の書き換え string midi = number + ".midi"; Server.FileRead(midi,0);//MIDIファイル読み込み string unsi = number + ".data"; Server.FileRead(unsi,0);//運指ファイル読み込み /*Environment.Exit(0); Console.WriteLine("OnDisconnect"); Server.Dispose();*/ } if (mode == "dl") { Console.WriteLine(dlfilename); Thread.Sleep(500); Server.WriteFile(dlfilename + ".meta"); Thread.Sleep(1000);//無効 Server.WriteFile(dlfilename + ".midi"); Console.WriteLine("midi finish"); Thread.Sleep(1000);//有効 Server.WriteFile(dlfilename + ".data"); Thread.Sleep(1000);//無効 Server.WriteFile(dlfilename + ".data"); Thread.Sleep(1000);//有効 Server.WriteFile(dlfilename + ".data"); Console.WriteLine("data finish"); Thread.Sleep(2500);// Server.WriteFile(dlfilename + ".meta"); Server.WriteFile(dlfilename + ".meta"); Console.WriteLine("meta finish"); Console.WriteLine("all send process finish"); Environment.Exit(0); Console.WriteLine("OnDisconnect"); Server.Dispose(); } }
private void RdataSend(DesoutterData ddata) { switch (ddata.MID) { //case "0002": // { // Thread.Sleep(500); // SendedData = new DesoutterData(23, 18, 0, PsetID.ToString().PadLeft(3, '0')); // SendData(SendedData); // } // break; //case "0004": // { // if (ddata.DataField.Contains("0001")) // { // Thread.Sleep(500); // SendedData = new DesoutterData(23, 18, 0, PsetID.ToString().PadLeft(3, '0')); // SendData(SendedData); // } // } // break; //case "0005": // { // if (ddata.DataField == "0018") // { // Thread.Sleep(500); // SendedData = new DesoutterData(20, 60, 1, ""); // SendData(SendedData); // } // } // break; case "0061": { // send = true; byte[] resutData = Encoding.ASCII.GetBytes(ddata.DataField); byte TighteningStatus = resutData[106 - 21 + 2]; //数组第106位-107位为 09 标志,108位为状态 byte[] Torque = new byte[6]; Buffer.BlockCopy(resutData, 141 - 21, Torque, 0, 6); byte[] Angle = new byte[5]; Buffer.BlockCopy(resutData, 170 - 21, Angle, 0, 5); double RealTor = Convert.ToInt32(Encoding.ASCII.GetString(Torque)) / 100.00; int realAng = Convert.ToInt32(Encoding.ASCII.GetString(Angle)); TestResult testResult = new TestResult() { RealTar = RealTor.ToString(), RealAgi = realAng.ToString(), AddDate = DateTime.Now }; if (TighteningStatus == 0x31) { testResult.IsOk = true; } else { testResult.IsOk = false; } string requestData = JsonConvert.SerializeObject(testResult); ReciveEventArgs asEventArgs = new ReciveEventArgs(Encoding.ASCII.GetBytes(requestData)); RequestHandler(this, asEventArgs); SendData(new DesoutterData(20, 62, 1, "")); //Thread.Sleep(500); //SendData(new DesoutterData(20, 42, 1, "")); } break; default: //SendData(SendedData); break; } }
/// <summary> /// UDP受信スレッド /// </summary> private void UDPReciveThread() { int rcvlen = 0; int cpylen = 0; IPAddress fromIP = null; IPEndPoint remoteEP = null; // 受信データ(送信IPアドレス、送信ポート番号など) byte[] rcvBytes = new byte[0]; // 受信データ try { LogPrint("UDP受信スレッド開始しました"); while (true) { // 受信ループ(Abort例外で終了) // 未コピーデータがなければ受信 if (rcvBytes.Length == cpylen) { if (udp.Available == 0) { rcvBytes = new byte[0]; } else { rcvBytes = udp.Receive(ref remoteEP); fromIP = remoteEP.Address; } cpylen = 0; } // 未コピーデータがあり、バッファが空いていれば格納しイベントを上げる if ((reciveBuffer.Length != reciveBufferDataLength) && (rcvBytes.Length != cpylen)) { labelRecive.BackColor = backColors.ReciveOn; Application.DoEvents(); // Mutexの所有権を得る - ∞秒待つ mutex.WaitOne(-1, false); // 今回格納可能なデータ長計算 rcvlen = reciveBuffer.Length - reciveBufferDataLength; if (rcvlen > rcvBytes.Length - cpylen) { rcvlen = rcvBytes.Length - cpylen; } // 格納 Array.Copy(rcvBytes, cpylen, reciveBuffer, reciveBufferDataLength, rcvlen); // 受信データサイズを更新 reciveBufferDataLength += rcvlen; cpylen += rcvlen; // Mutexの所有権を解放 mutex.ReleaseMutex(); if (reciveBufferDataLength == 0) { // 受信データなし labelRecive.BackColor = backColors.ReciveOff; Application.DoEvents(); return; } // 受信通知イベント ReciveEventArgs eArgs = new ReciveEventArgs(); eArgs.FromIP = fromIP; eArgs.DataCount = reciveBufferDataLength; eArgs.FreeCount = reciveBuffer.Length - reciveBufferDataLength; if (dataDump == true) { LogPrint("Status[RCV:" + eArgs.DataCount.ToString() + ",FREE:" + eArgs.FreeCount.ToString() + "]"); } if (ReciveEvent != null) { // ハンドラが設定されていたらイベント発生 CallReciveEvent(this, eArgs); } if (reciveBuffer.Length - reciveBufferDataLength < 1) { labelRecive.BackColor = backColors.BufferFull; LogPrint("受信バッファがいっぱいです"); } else { labelRecive.BackColor = backColors.ReciveOff; } Application.DoEvents(); } } } catch (ThreadAbortException) { // スレッド終了 LogPrint("UDP受信スレッド停止しました"); } catch (Exception es) { LogPrint("UDP受信スレッドで例外が発生しました - " + es.Message); throw es; } }