Answer the question
In order to leave comments, you need to log in
(Still unresolved) Socket client. How to constantly monitor whether there is data from the server?
There is a client:
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
public class LoginActivity extends AppCompatActivity {
Button buttonEnter;
TextView textViewHeader;
EditText editTextLogin;
Handler handler;
Socket client = null;
DataOutputStream dataToServerStream = null;
DataInputStream dataFromServerStream = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
buttonEnter = (Button) findViewById(R.id.buttonEnter);
textViewHeader = (TextView) findViewById(R.id.textViewHeader);
editTextLogin = (EditText) findViewById(R.id.editTextLogin);
//вынос подключения к серверу в отдельный поток
class ConnectToServer extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
//подключение к серверу
try {
client = new Socket("193.106.169.249", 1605);
//чтение данных с сервера
/*dataFromServerStream = new DataInputStream(client.getInputStream());
String r="";
byte[] readBuffer=new byte[5*1024];
int read=dataFromServerStream.read(readBuffer);
if(read!=-1)
{
byte[] readData=new byte[read];
System.arraycopy(readBuffer,0,readData,0,read);
try{
r = new String(readData, "UTF-8");
}catch(UnsupportedEncodingException e){}
}*/
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("Got an IOException: " + e.getMessage());
}
return null;
}
}
//запуск подключения к серверу
final ConnectToServer setConnectToServer = new ConnectToServer();
setConnectToServer.execute();
//обработчик нажатий
View.OnClickListener OnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.buttonEnter:
//отправка данных на сервер
try {
String str;
dataToServerStream = new DataOutputStream(client.getOutputStream());
str = editTextLogin.getText().toString();
byte[] buf = str.getBytes("UTF-8");
dataToServerStream.write(buf, 0, buf.length);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
};
buttonEnter.setOnClickListener(OnClickListener);
}
}
dataFromServerStream = new DataInputStream(client.getInputStream());
String r="";
byte[] readBuffer=new byte[5*1024];
int read=dataFromServerStream.read(readBuffer);
if(read!=-1)
{
byte[] readData=new byte[read];
System.arraycopy(readBuffer,0,readData,0,read);
try{
r = new String(readData, "UTF-8");
}catch(UnsupportedEncodingException e){}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question