Hello.

The question is this:
There is a java server and an Android client. Kopal client-server connections on the Internet, the transfer of certain data, etc., but did not understand how to transfer certain packets on a specific action.

Example:

A server is waiting for connections.
Go to the android application in which 3 TextView, where we entered 3 different text values ​​and the OK button. We click on the ok button - a Socket connection to the server is created, and, say, a packet is sent to the server as bytes and an opcode containing these 3 entered values, the server accepts this packet and "decomposes", putting these 3 different values ​​into different variables .

It is very important for me that it be batch processing, i.e. if I add, let's say, 1 more OK button (2), then when it is pressed, the client sends 3 values ​​to the server as well, but the server will already process them differently.

---> Ie. take the client package with opcode 0x01 and send it to the server. According to this opcode, the server does certain actions. We take a client package with opcode 0x02 - we send it to the server and the server already does other actions on this opcode ... I think I explained the principle of this issue correctly. It is advisable to leave the code samples.

  • Post / Get requests you do not like? - Kota1921
  • @ VladEv1L, are you sure that you need sockets? Usually, such tasks are solved using the RESTfull service, and, as @nekaneka writes, Post / Get and other requests. - Vladyslav Matviienko
  • @ VladEv1L, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

There is a convenient and visual way to solve your problem. The Android client creates AsyncTask and in its stream sends requests to the server. The AsyncTask lesson can be found here . And inside the doInBackground method, form a GET or POST request.

  • I do not know, but the get and post requests for me sound like in php data transfer to the site, but this is only the transfer of regular data, in my future, there will be a transmission of an update package of a character, coordinates, various checks and there will be many packages in my project. I ask about the packages because I personally saw the code of the game server. The principle is clear, you can put it in your head, but everything is more difficult to set up. After all, judge for yourself - it is easier to take and send a packet with the opcode from the client, the server will know its example what to do with it. Post and gett requests are if you create a chat) - VladEv1L
  • Or let's make it easier - just need to distribute the bytes in the client like this: Send the server bytes: 0x01 account_name The server sees 0x01 and sends its contents for processing. In processing, it sends back a packet with opcode and content. The client sends it for processing. - VladEv1L
  • So what do you Post and gett requests did not like. This is the simplest thing; you can write anything in the request body and process it on the server. Plus, it is already written before you - no need to fence bicycles. - Kota1921
  • You see, I can send these 2 lines to the server calmly: String login = GetRegLogin.getText (). ToString (); String password = GetRegpassword.getText (). ToString (); out.writeUTF (login); out.writeUTF (password); As well as the server accepts them normally: String login = null; String password = null; while (true) {login = in.readUTF (); password = in.readUTF (); System.out.println ("Client login:" + login + "Client password:" + password); But this is not the same as the gett and the post requests ... - VladEv1L
  • Well, what exactly is the problem. Why is it not that? - Kota1921