新建两个不同的工程 Socket 工程中; SocketActivity.java package zlj.Socket; 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 SocketActivity extends Activity { /** Called when the activity is first created. */ private Button startButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startButton = (Button)findViewById(R.id.startListener); startButton.setOnClickListener(new StartSocketListener()); } class StartSocketListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub new ServerThread().start(); } } class ServerThread extends Thread{ //TCPSocket /* public void run() { // TODO Auto-generated method stub ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4567); Socket socket = serverSocket.accept(); InputStream inputStream = socket.getInputStream(); byte buffer [] = new byte[1024*4]; int temp = 0; while((temp = inputStream.read(buffer)) != -1) { System.out.println(new String(buffer,0,temp)); } } catch (IOException e) { e.printStackTrace(); } finally { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }*/ //UDPSocket public void run() { try { DatagramSocket socket = new DatagramSocket(4567); byte data [] =new byte[1024]; DatagramPacket packet = new DatagramPacket(data,data.length); socket.receive(packet); // System.out.println(packet.getLength());//这样打后面是乱码补充 String result = new String(packet.getData(),packet.getOffset(),packet.getLength()); System.out.println("result-->" + result); } catch (Exception e) { e.printStackTrace(); } } } } main.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/startListener" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始监听"/> </LinearLayout> Client 工程中 TCPClient.java package zlj.Client; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class TCPClient { public static void main(String[] args) { // TODO Auto-generated method stub try { Socket socket = new Socket("192.168.1.104",4567); InputStream inputStream = new FileInputStream("f://file/word.txt"); OutputStream outputStream = socket.getOutputStream(); byte buffer [] = new byte[4*1024]; int temp = 0; while((temp = inputStream.read(buffer)) != -1) { outputStream.write(buffer,0,temp); } outputStream.flush(); } catch (Exception e) { e.printStackTrace(); } } } UDPClient.java package zlj.Client; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPClient { public static void main(String[] args) { // TODO Auto-generated method stub try { DatagramSocket socket = new DatagramSocket(4567); InetAddress serverAddress = InetAddress.getByName("192.168.1.104"); String str = "hello"; byte data [] = str.getBytes(); DatagramPacket packet = new DatagramPacket(data,data.length,serverAddress,4567); socket.send(packet); } catch (Exception e) { e.printStackTrace(); } } }