Created
April 29, 2017 02:43
-
-
Save luiseduardogfranca/8f02260bdb711acbd3c651a923e6e053 to your computer and use it in GitHub Desktop.
Using Socket in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.luis.usingsocket; | |
import android.graphics.Color; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import org.w3c.dom.Text; | |
import java.io.IOException; | |
import java.net.Socket; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void buttonConnect(View view){ | |
EditText inputIP = (EditText) findViewById(R.id.field_ip); | |
EditText inputPort = (EditText) findViewById(R.id.field_port); | |
TextView text = (TextView) findViewById(R.id.text); | |
String ip = inputIP.getText().toString(); | |
String port = inputPort.getText().toString(); | |
System.out.println("Porta: " + port); | |
//convert for number if not null | |
if(!port.equals("")){ | |
int numberPort = Integer.parseInt(port); | |
boolean resp = connectServer(ip, numberPort); | |
if(resp) { | |
text.setText("Conectado"); | |
}else{ | |
text.setText("Não foi possível conectar"); | |
} | |
}else{ | |
int color = Color.parseColor("#ff5959"); | |
inputPort.setHintTextColor(color); | |
inputPort.setHint("Porta inválida"); | |
} | |
} | |
//connect witch server | |
public boolean connectServer(final String IP, final int port){ | |
Thread thread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try{ | |
//create conection | |
Socket client = new Socket(IP, port); | |
}catch (IOException IOError){ | |
IOError.printStackTrace(); | |
} | |
} | |
}); | |
thread.start(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment