ilnick
ilnick

Reputation: 11

kryonet client, send message to server without open a new connection

I'm saying i'm not a programmer but a guy who has been learning to program with java for a while. I hope to find the solution to my problem here. I'm trying to program my home automation system and remote control and to do this, I chose to use Kryonet. My problem is that every time I send the data to the server, the client opens a new connection. It's been 3 weeks since googlo and I try to figure out how to do it but with no results. Every help is seriously appreciated. This is my code. Thank you. This code work in my home network. Sorry for my english...

public class MainActivity extends AppCompatActivity {

Button button;
String IP = "";
EditText editText;
TextView textView;
EditText editText3;
public static String msg_response;
public static String msg_request;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Handler handler = new MyHandler();

    button = (Button) findViewById(R.id.button);
    editText = (EditText) findViewById(R.id.editText);
    editText3 = (EditText) findViewById(R.id.editText3);
    textView = (TextView) findViewById(R.id.textView);


    int MY_PERMISSIONS_REQUEST_INTERNET = 1;
    int MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE = 1;
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.INTERNET},
            MY_PERMISSIONS_REQUEST_INTERNET);
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_NETWORK_STATE},
            MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE);
    int MY_PERMISSIONS_REQUEST_ACCESS_WIFY_STATE = 1;
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_WIFI_STATE},
            MY_PERMISSIONS_REQUEST_ACCESS_WIFY_STATE);


    textView.setText(msg_response);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                msg_request = valueOf(editText3.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            MyThread myThread = new MyThread(handler);
            myThread.start();
        }
    });
}

 private class MyHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        Bundle bundle = msg.getData();
        if (bundle.containsKey("msg da server")) {
            String msgin = bundle.getString("msg da server");
            textView.setText(msgin);
        }
    }
}

class MyThread extends Thread {

    private Handler handler;

    public MyThread(Handler handler) {
        this.handler = handler;
    }


    public void run() {


        System.out.println("MyThread running");
        Client client = new Client();
        client.start();
        Kryo kryoClient = client.getKryo();
        kryoClient.register(SampleRequest.class);
        kryoClient.register(SampleResponse.class);

        try {
            client.connect(5000, "192.168.0.101", 54555, 54666);

        } catch (IOException e) {
            e.printStackTrace();

        }


        client.addListener(new Listener() {
            public void received(Connection connection, Object object) {
                if (object instanceof SampleResponse) {
                    SampleResponse response = (SampleResponse) object;
                    System.out.println(response.text);
                    msg_response = response.text.toString();
                    invia_activity(msg_response);
                }
            }
        });

        SampleRequest request = new SampleRequest();
        request.text = msg_request;

        client.sendTCP(request);
    }

    private void invia_activity(String invia) {
        Message msg = handler.obtainMessage();
        Bundle b = new Bundle();
        b.putString("msg da server", "" + invia);
        msg.setData(b);
        handler.sendMessage(msg);
    }
}

}

Upvotes: 1

Views: 561

Answers (1)

Gandalf1783
Gandalf1783

Reputation: 11

I dont have an direct solution, but i have an tutorial for it. I used the same one. So there the connections keeps open, and you can send as many packets as you need. Its without audio, but the code works well. After that you can experiment with the code. It works fine for me. This is the tutorial I hope i can help you with this. EDIT: Maybe you can make an

public static Connection conn;

and you could use that object again and again as your connection to the server.

Upvotes: 0

Related Questions