Server端

package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server implements Runnable { public static final String SERVERIP = "10.0.2.2"; public static final int SERVERPORT = 51706; public void run() { try { System.out.println("S: Connecting..."); ServerSocket serverSocket = new ServerSocket(SERVERPORT); while (true) { //等待接受客户端请求 Socket client = serverSocket.accept(); System.out.println("S: Receiving..."); try { //接受客户端信息 BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream())); Socket socket = null; //发送消息 PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(client.getOutputStream())),true); //设置返回信息 out.println("my name is server"); out.flush(); String str = in.readLine(); File file = new File ("C://file//android.txt"); FileOutputStream fops = new FileOutputStream(file); byte [] b = str.getBytes();; for ( int i = 0 ; i < b.length-1; i++ ) { fops.write(b[i]); System.out.println(b.toString()); } System.out.println("S: Received: '" + str + "'"); } catch (Exception e) { System.out.println("S: Error"); e.printStackTrace(); } finally { client.close(); System.out.println("S: Done."); } } } catch (Exception e) { System.out.println("S: Error"); e.printStackTrace(); } } public static void main(String [] args ) { Thread desktopServerThread = new Thread(new Server()); desktopServerThread.start(); } }

Client端

package com.lde; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Socket_Android extends Activity { private TextView mTextView = null; private EditText mEditText = null; private TextView tx1 = null; private Button mbutton = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mbutton = ( Button ) findViewById(R.id.Button01); mEditText = ( EditText ) findViewById( R.string.EditText01); mTextView = ( TextView ) findViewById ( R.string.TextView01); tx1 = ( TextView ) findViewById ( R.id.tx); mbutton.setOnClickListener(new OnClickListener() { public void onClick(View v) { setTitle("abcdefg"); Socket socket = null; try { // 指定Server的IP地址 InetAddress serverAddr = InetAddress.getByName("10.0.2.2");// TCPServer.SERVERIP Log.d("TCP", "C: Connecting..."); // 应用Server的IP和端口建立Socket对象 socket = new Socket(serverAddr, 51706); String message = "---Test_Socket_Android---"; Log.d("TCP", "C: Sending: '" + message + "'"); // 将信息通过这个对象来发送给Server PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true); //接受服务器信息 BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); //得到服务器信息 String msg = in.readLine(); //在页面上进行显示 tx1.setText(msg); out.println(msg); out.flush(); } catch (Exception e) { Log.e("TCP", "S: Error", e); } finally { try { // ServerSocket serverSocket = new ServerSocket(51706); // Socket client = serverSocket.accept(); // BufferedReader in = new BufferedReader( // new InputStreamReader(client.getInputStream())); // // String str = in.readLine(); // tx1.setText("123"); // System.out.println("S: Received: '" + str + "'"); // // System.out.println("S: Received: '" + str + "'"); socket.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } ); } // public void a () throws IOException // { // //指定Server的IP地址 // InetAddress serverAddr = // InetAddress.getByName("110.149");//TCPServer.SERVERIP // Log.d("TCP", "C: Connecting..."); // // //应用Server的IP和端口建立Socket对象 // Socket socket = new Socket(serverAddr, 51706); // String message = "AndroidRes,Where is my Pig (Android)?"; // try { // // Log.d("TCP", "C: Sending: '" + message + "'"); // // //将信息通过这个对象来发送给Server // PrintWriter out = new PrintWriter( new BufferedWriter( new // OutputStreamWriter(socket.getOutputStream())),true); // // out.println(message); // } catch(Exception e) { // // Log.e("TCP", "S: Error", e); // } finally { // // socket.close(); // } // } }

R.java

/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.lde; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int Button01=0x7f050000; public static final int tx=0x7f050001; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int Button01=0x7f040003; public static final int EditText01=0x7f040004; public static final int TextView01=0x7f040002; public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }

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" /> --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/TextView01" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/EditText01" /> <Button android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Button01" /> <TextView android:id="@+id/tx" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>

string.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Socket_Android!</string> <string name="app_name">Socket_Android</string> <string name="TextView01">这里显示接受到服务器发来的信息</string> <string name="Button01">测试连接</string> <string name="EditText01">请输入内容</string> </resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lde" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="5" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Socket_Android" 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>

更多相关文章

  1. Android获取设备信息(利用反射)
  2. 在服务器上使用grandle打包android源码
  3. Android intent 传递数组对象序列化
  4. Android带参数链接请求服务器
  5. android 获取手机位置信息
  6. Android自动化工具Monkeyrunner使用(六) —— 根据ID查找对象
  7. Android如何将第三方信息应用设置为默认信息应用?
  8. Android 签名信息读取

随机推荐

  1. Android(安卓)APK安装过程及原理详解
  2. Android学习——基础组件
  3. Android(安卓)LCD(三):Samsung LCD接口篇
  4. 使用Eclipse开发Android
  5. android init.rc详解
  6. Android(安卓)带有弹出收缩动画的扇形菜
  7. 从Android中Activity之间的通信说开来
  8. android——wifi系统架构
  9. [Android] AsyncTask使用实例---加载网络
  10. Android(安卓)音频源码目录