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 |
package oh.my.girl import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; public class SocketClient extends Activity { Button button1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String host = "192.168.0.2"; int port = 8080; Socket socket = null; OutputStream os = null; OutputStreamWriter osw = null; try { socket = new Socket(host, port); os = socket.getOutputStream(); osw = new OutputStreamWriter(os); } catch (IOException e) { } String message = "I love you"; try { osw.write(message, 0, message.length()); osw.flush(); } catch (IOException e) { } TextView tv = (TextView) findViewById(R.id.text); tv.setText(message); try { socket.close(); } catch (IOException e) { } } } |