1.TCP:
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/buttonStart" android:layout_width="300dp" android:layout_height="wrap_content" android:text="启动服务器端监听" /> </LinearLayout>
服务器:
package idea.org; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Android_Socket extends Activity { private Button buttonStart=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart=(Button)findViewById(R.id.buttonStart); buttonStart.setOnClickListener(new buttonListener()); } class buttonListener implements OnClickListener { /* (non-Javadoc) * @see android.view.View.OnClickListener#onClick(android.view.View) */ @Override public void onClick(View arg0) { // TODO Auto-generated method stub new Thread() { /* (non-Javadoc) * @see java.lang.Thread#run() */ @Override public void run() { // TODO Auto-generated method stub ServerSocket serverSocket=null; try { //创建ServerSocket对象监听6661端口 serverSocket=new ServerSocket(6661); //接收tcp连接返回socket对象 Socket socket=serverSocket.accept(); //获得输入流 InputStream inputStream=socket.getInputStream(); byte []byteBuffer=new byte[1024]; int temp=0; //读取接收的数据 while((temp=inputStream.read(byteBuffer))!=-1) System.out.println(new String(byteBuffer,0,temp)); socket.close(); serverSocket.close(); } catch(IOException e) { e.printStackTrace(); } } }.start(); } }; }
客户端:
package idea.org; import java.io.FileInputStream; import java.io.InputStream; import java.net.Socket; public class TCPCient { public static void main(String[] args) { try { //定义Socket对象,连接指定IP和指定端口 Socket socket=new Socket("127.0.0.1",6661); InputStream inputStream=new FileInputStream("F://test.txt"); //从Socket对象获得输出流 java.io.OutputStream outputStream=socket.getOutputStream(); int temp=0; byte[]buffer=new byte[1024]; //向输出流中写要发送的数据 while((temp=inputStream.read(buffer))!=-1) { outputStream.write(buffer,0,temp); System.out.println(new String(buffer,0,temp)); } outputStream.flush(); socket.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
AndroidManifest.xml:(权限)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="idea.org" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="11" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Android_Socket" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>
2.UDP:
服务器:
package idea.org; import java.io.IOException; import java.io.InputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Android_Socket extends Activity { private Button buttonStart=null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart=(Button)findViewById(R.id.buttonStart); buttonStart.setOnClickListener(new buttonListener()); } class buttonListener implements OnClickListener { /* (non-Javadoc) * @see android.view.View.OnClickListener#onClick(android.view.View) */ @Override public void onClick(View arg0) { // TODO Auto-generated method stub new Thread() { /* (non-Javadoc) * @see java.lang.Thread#run() */ @Override public void run() { // TODO Auto-generated method stub try { //创建一个DatagramSocket对象并指定监听的端口 DatagramSocket socket=new DatagramSocket(6662); byte[]data=new byte[1024]; //创建一个空的DatagramPacket对象用来存放接收到的DatagramPacket DatagramPacket packet=new DatagramPacket(data,data.length); //使用receive方法接收客户端发送的数据 socket.receive(packet); String result=new String(packet.getData(),packet.getOffset(),packet.getLength()); System.out.println(result); } catch(IOException e) { e.printStackTrace(); } } }.start(); } }; }
客户端:
package idea.org; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPClient { public static void main(String []args) { try { //创建一个DatagramSocket对象 DatagramSocket socket=new DatagramSocket(); //创建一个表示IP地址的InetAddress对象 InetAddress serverAddress=InetAddress.getByName("192.168.0.5"); String str="Hello,world!Hello,Everyone!"; byte[]data=str.getBytes(); //创建一个用于发送的DatagramPacket对象 DatagramPacket packet=new DatagramPacket(data,data.length,serverAddress,6662); //发送数据 socket.send(packet); } catch(Exception e) { e.printStackTrace(); } } }