ruby-c++間で通信を行っています。
問題として、rubyからまずサーバプログラム(c++)に処理のタイプを投げたいので、socket.ioをもちいて
socket.write(msg-type)
のように記述しデータを送っています。(その後各処理に必要なデータを送信している形になっています)
始め、私はmsg-type = STDIN.gets のようにruby側に記述しており、標準入力から1を記述することでプログラム間のやりとりはうまくいっていました。
そこで標準入力をやめて、msg-type = "1"
と固定するとなぜかプログラムが動きません。
デバッグしてみるとC++ では1の値は受信できていますが、なぜか処理が先に進んでいませんでした。原因が本当にわからず困っています。お力をお貸しください。
ruby
1require 'socket' 2 3 4def send_location(sock) 5 msg = "40\n" 6 print("you writed:", msg) 7 sock.write(msg) 8 result = sock.gets 9 print("return message:",result) 10 msg = "135.5\n" 11 print("you writed:", msg) 12 sock.write(msg) 13 result = sock.gets 14 print("return message:",result) 15 result = sock.gets 16 array = [] 17 while fileid = sock.gets 18 array.push(fileid.chomp) 19 end 20 return array 21end 22 23 24host = if ARGV[0] then ARGV[0] else 'localhost' end 25port = if ARGV[1] then ARGV[1] else '7777' end 26sock = TCPSocket.open(host, port) 27 28msg = "1" 29print("you writed:", msg) 30sock.write(msg) 31result = sock.gets 32print("return message:", result) 33 34if msg.chomp! == "1" then 35 array = send_location(sock) 36end 37 38array.each do |fileid| 39 puts fileid 40end 41 42sock.close 43
c
1 2int recv_type(int acc_sd) 3{ 4 char buf[256]; 5 int num = atoi(buf); 6 7 // 受信バッファの初期化 8 memset(buf, 0, sizeof(buf)); 9 10 read(acc_sd,buf,sizeof(buf)); 11 num = atoi(buf); 12 //printf("Recv type is %d\n",num); 13 write(acc_sd,buf,sizeof(buf)); 14 return num; 15} 16 17 18void recv_location(int acc_sd) 19{ 20 char buf[256]; 21 float num = atoi(buf); 22 23 // 受信バッファの初期化 24 memset(buf, 0, sizeof(buf)); 25 read(acc_sd,buf,sizeof(buf)); 26 num = atof(buf); 27 printf("Recv latitude is %f\n",num); 28 ust.user_lat = num; 29 write(acc_sd,buf,sizeof(buf)); 30 31 32 memset(buf, 0, sizeof(buf)); 33 read(acc_sd,buf,sizeof(buf)); 34 num = atof(buf); 35 printf("Recv longitude is %f\n",num); 36 ust.user_lon = num; 37 write(acc_sd,buf,sizeof(buf)); 38 39}
c++
1 //ruby側のクライアントから受けとったタイプが呼ばれることになる、下を無視すれば動く 2 int etype = recv_type(soc); 3 4 switch (etype) { 5 case 0: 6 fprintf(stderr,"Execution type is %d: image_search() called\n",etype); 7 //sleep(1); 8 (void) image_search(); 9 break; 10 case 1: 11 fprintf(stderr,"Execution type is %d: geo_search() called\n",etype); 12 (void) recv_location(soc); 13 //sleep(1); 14 (void) geo_search(soc); 15 break; 16 case 2: 17 fprintf(stderr,"Execution type is %d: time_search() called\n",etype); 18 //sleep(1); 19 (void) time_search(); 20 break; 21 case 3: 22 fprintf(stderr,"Execution type is %d: userfav_search() called\n",etype); 23 //sleep(1); 24 //未実装↓ 25 //(void) userfav_search(); 26 break; 27 default: 28 fprintf(stderr,"Undefined type of execution\n"); 29 //sleep(1); 30 break; 31 }
case 1のrecv_locationの前にprintf("AAA\n")と打っても出力されずとまってしまったことからfprintでなんらかの問題がおこったように思えますが、fprintfを消しても処理がうまくいきません... ruby側でSTDINに帰るとうまくいく理由がまったくわかりません...
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/09/09 21:32