前提・実現したいこと
UDP通信を使ってスマホとpcでfloat型の配列を送ろうとしています
スマホ(android mate P20)
pc(windows,unity2018)
unityでandroid用にビルドして、アンドロイドアプリを動かしつつ
パソコンで値をunityで受け取ろうとしています。
その過程で値が届けられない問題にぶつかりました。
いろいろ試したところアンドロイドにビルトする前に問題が起こっている気がし、
自分のパソコンだけでデータの送受信を動かしました。
参考URLを参考にfloat型の配列を送るようにプログラムを書き直しました。
下のプログラム(送信も受信もまとまっている)をパソコンだけで動かしたところうまく動作しました。
該当のソースコード
C#
1using forudp; 2public class udp : MonoBehaviour 3{ 4 private UDP commUDP = new UDP(); 5 6 // Start is called before the first frame update 7 void Start() 8 { 9 //commUDP.init(int型の送信用ポート番号, int型の送信先ポート番号, int型の受信用ポート番号); 10 commUDP.init(40001, 40002, 40002); 11 } 12 13 // Update is called once per frame 14 void Update() 15 { 16 float[] av = { 1f, 2f ,3f,4f,5f,6f,7f, 8f, 9f, 10f, 11f, 12f, 13f , 14f,15f, 16f, 17f }; 17 commUDP.send_xyz_arr(av); 18 commUDP.start_receive(); 19 //var a = commUDP.rcvMsg_xyz; 20 var b = commUDP.rcv_float_arr; 21 foreach (int i in b) 22 { 23 Debug.Log(i); 24 } 25 } 26}
これは下のプログラムを利用します
C#
1namespace forudp //namespaceは本体に合わせて要修正 2{ 3 public class UDP 4 { 5 private UdpClient udpForSend; //送信用クライアント 6 private string remoteHost = "localhost";//送信先のIPアドレス 7 private int remotePort;//送信先のポート 8 9 private UdpClient udpForReceive; //受信用クライアント 10 public string rcvMsg = "ini";//受信メッセージ格納用 11 public float rcvMsg_xyz = 0; 12 13 private System.Threading.Thread rcvThread; //受信用スレッド 14 15 public List<float> rcv_float_arr = new List<float>(); 16 17 public UDP() 18 { 19 } 20 21 public bool init(int port_snd, int port_to, int port_rcv) //UDP設定(送受信用ポートを開きつつ受信用スレッドを生成) 22 { 23 try 24 { 25 udpForSend = new UdpClient(port_snd); //送信用ポート 26 remotePort = port_to; //送信先ポート 27 udpForReceive = new UdpClient(port_rcv); //受信用ポート 28 rcvThread = new System.Threading.Thread(new System.Threading.ThreadStart(receive_xyz)); //受信スレッド生成 29 return true; 30 } 31 catch 32 { 33 return false; 34 } 35 } 36 37 public void send_xyz_arr(float[] sendMsg) //float配列を送信用ポートから送信先ポートに送信 38 { 39 try 40 { 41 42 43 byte[] sendBytes = new byte[sendMsg.Length]; 44 byte[] rv = new byte[4*sendBytes.Length]; 45 for (int i = 0; i < sendMsg.Length; i++) 46 47 { 48 sendBytes = BitConverter.GetBytes(sendMsg[i]); 49 System.Buffer.BlockCopy(sendBytes, 0, rv, i*4, sendBytes.Length); 50 } 51 //Debug.Log(sendBytes); 52 //Debug.Log(rv); 53 udpForSend.Send(rv, rv.Length, remoteHost, remotePort); 54 //udpForSend.Send(sendBytes, sendBytes.Length, remoteHost, remotePort); 55 } 56 catch { } 57 } 58 59 public void receive_xyz() //受信スレッドで実行される関数 60 { 61 IPEndPoint remoteEP = null;//任意の送信元からのデータを受信 62 while (true) 63 { 64 try 65 { 66 byte[] rcvBytes = udpForReceive.Receive(ref remoteEP); 67 for (int i = 0; i < rcvBytes.Length; i++) 68 { 69 var a = BitConverter.ToSingle(rcvBytes, i * 4); 70 rcv_float_arr.Add(a); 71 } 72 } 73 catch { } 74 } 75 } 76 77 public void start_receive() //受信スレッド開始 78 { 79 try 80 { 81 rcvThread.Start(); 82 } 83 catch { } 84 } 85 86 public void stop_receive() //受信スレッドを停止 87 { 88 try 89 { 90 rcvThread.Interrupt(); 91 } 92 catch { } 93 } 94 95 public void end() //送受信用ポートを閉じつつ受信用スレッドも廃止 96 { 97 try 98 { 99 udpForReceive.Close(); 100 udpForSend.Close(); 101 rcvThread.Abort(); 102 } 103 catch { } 104 } 105 } 106}
この上のプログラムは動作しました。
しかし データを送信するプログラムと受信するプログラムを分けると値が受け取れません。
この場合udp_recのbになにも入っていないでデバッグで何も表示されません。
C#
1public class udp_send : MonoBehaviour 2{ 3 private UDP commUDP = new UDP(); 4 // Start is called before the first frame update 5 void Start() 6 { 7 //commUDP.init(int型の送信用ポート番号, int型の送信先ポート番号, int型の受信用ポート番号); 8 commUDP.init(40001, 40002, 40002); 9 } 10 11 // Update is called once per frame 12 void Update() 13 { 14 float[] av = { 1f, 2f ,3f,4f,5f,6f,7f, 8f, 9f, 10f, 11f, 12f, 13f , 14f,15f, 16f, 17f }; 15 commUDP.send_xyz_arr(av); 16 commUDP.start_receive(); 17 } 18} 19
受信用
C#
1public class udp_rec : MonoBehaviour 2{ 3 private UDP commUDP = new UDP(); 4 5 //commUDP.init(int型の送信用ポート番号, int型の送信先ポート番号, int型の受信用ポート番号); 6 7 // Start is called before the first frame update 8 void Start() 9 { 10 commUDP.init(40001, 40002, 40002); 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 18 commUDP.start_receive(); 19 var b = commUDP.rcv_float_arr; 20 foreach (int i in b) 21 { 22 Debug.Log(i); 23 } 24 } 25}
発生している問題・エラーメッセージ
送受信がまとまっているときのプログラムは動作するのに、
送信と受信を分けるとうまく動作しない
試したこと
udp_recのUpdate()のなかに
float[] av = { 1f, 2f ,3f,4f,5f,6f,7f, 8f, 9f, 10f, 11f, 12f, 13f , 14f,15f, 16f, 17f }; commUDP.send_xyz_arr(av); commUDP.start_receive();
これをいれるとうまく動作したことから、ポート番号が重複していることはないと思っています
(sendとrecでポート番号を二重に使っていない)
ほかにどこに問題があると考えられますか?
補足情報(FW/ツールのバージョンなど)
windows unity 2018 3.14f
C#
回答1件
あなたの回答
tips
プレビュー