Click here to Skip to main content
15,886,101 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings,

I want to send a string from actionscript to java trough a socket.
this is my java code
Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


public class Main
{
  public static void main(String[] args){
    try{
      // 8080ポートを使ってサーバソケットを作成
      ServerSocket server = new ServerSocket(8080);
      
      System.out.println("1サーバソケット作ってflashの接続待ち");

      
      // Flashからの接続待ち
      Socket socket = server.accept();
      
      System.out.println("2接続成功しました");

      // 出力ストリームを作成
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

      
      System.out.println("3出力ストリーム作成しました");
      
      while (true){
        System.out.println("4最初のwhile文突入");
        String str = "";
        
        System.out.println("5これから読み込みます");
        // 一文字読み込む(Flashから転送されるまでここで止まる
        String c = in.readLine();
        
        System.out.println("6読み込みしました");

        // エラー(通信が切れた
        if(c == null){
          System.out.println("7通信が切れた");
          break;
        }

        // '.'が来るまで繰り返す
        while (c != "\0"){
          System.out.println("8最後のwhile");
          str += c;	// 文字列に結合する
          c = in.readLine();	// 次の1文字読み込む
        }

        // 送られてきたメッセージを表示
        System.out.println(str);
        
        }

      in.close();
      socket.close();
      server.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

and this is my actionscript
var socket = new XMLSocket();


// IP "localhost "のポート8080番に接続
socket.connect("localhost", 8080);
socket.send("A MESSAGE \r\n"  );


the problem is the java code hangs on in.readline();
when i close the flash program the java code continues and goes in to the following if statement.
Java
if(c == null){
          System.out.println("7通信が切れた");
          break;
        }


what i want is that the java code reads the string i send with actionscript and then prints the string.
Posted
Comments
Timberbird 13-Dec-13 7:25am    
So it doesn't receive your string at all? Here it is said that blocking until socket is closed is normal for BufferedReader, but you should at least receive the first string you send

1 solution

the answer's you need to good prepare you data-format
when try to close one socket ,flush the stream first
I don't like japanese at all
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900