1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
package com.example.planar;
import java.io.IOException;
import java.net.Socket;
import android.os.AsyncTask;
import android.widget.Toast;
public class ClientSocket extends AsyncTask <String, Void, String> {
@Override
protected String doInBackground(String... params) {
Socket comm = null;
String returnString = null;
// recupere la chaine du port a utiliser et la transforme en int
int port = Integer.parseInt(params[1]);
if (port== 0) {
Toast.makeText(this, "please select a socket port", Toast.LENGTH_LONG).show();
}
try
{
comm = new Socket(params[0], port);
comm.getOutputStream().write(params[2].getBytes());
}
catch(Exception e)
{
returnString ="Exception " + e.toString();
}
finally
{
if (comm!=null)
try {
comm.close();
} catch (IOException e) {
}
}
return returnString ;
}
} |
Partager