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 57 | package client.cyut; import java.io.BufferedOutputStream; import java.net.InetSocketAddress; import java.net.Socket; import android.app.Activity; import android.os.Bundle; public class AndroidSocketClientActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new SocketClient(); } } //package test; class SocketClient { private String address = "163.17.9.241";// 連線的ip private int port = 8769;// 連線的port public SocketClient() { Socket client = new Socket(); InetSocketAddress isa = new InetSocketAddress(this.address, this.port); try { client.connect(isa, 10000); BufferedOutputStream out = new BufferedOutputStream(client .getOutputStream()); // 送出字串 out.write("#Add#\n".getBytes()); out.write("name:Jack\n".getBytes()); out.write("vendor:Honda\n".getBytes()); out.write("phone:0916069155\n".getBytes()); out.write("vip:yes\n".getBytes()); out.write("status:empty\n".getBytes()); out.write("number:405\n".getBytes()); out.flush(); out.close(); out = null; client.close(); client = null; } catch (java.io.IOException e) { System.out.println("Socket連線有問題 !"); System.out.println("IOException :" + e.toString()); } } } |
Direct link: https://paste.plurk.com/show/588001