Android客户端与电脑服务器端
来源:互联网 发布:js math 编辑:程序博客网 时间:2024/05/01 09:00
电脑服务器端
import java.io.*;import java.net.ServerSocket;import java.net.Socket;import java.util.*;public class MyServer {public static void main(String[] args) throws Exception{ServerSocket server=new ServerSocket(1000);Socket clink=server.accept();PrintStream out=new PrintStream(clink.getOutputStream());BufferedReader in=new BufferedReader(new InputStreamReader(clink.getInputStream()));StringBuffer bu=new StringBuffer();bu.append("Andorid: ");bu.append(in.readLine());out.println(bu);in.close();out.close();clink.close();server.close();}}
Andorid客户端
第一步:main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/send" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="连接ServerSocket程序" /> <TextView android:id="@+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="等待程序的连接" /></LinearLayout>
第二步:编写JAVA 类
package com.android.main;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;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;import android.widget.TextView;public class ActivityMain extends Activity {private Button button;private TextView textView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button=(Button)this.findViewById(R.id.send); textView=(TextView)this.findViewById(R.id.info); button.setOnClickListener(new SendOnclickListener()); } class SendOnclickListener implements OnClickListener { public void onClick(View v) {try{Socket clink=new Socket("10.10.104.57",1000);PrintStream out=new PrintStream(clink.getOutputStream());BufferedReader in=new BufferedReader(new InputStreamReader(clink.getInputStream()));out.println("发送信息给ServerSocket");ActivityMain.this.textView.setText(in.readLine());in.close();out.close();clink.close();}catch(Exception e){}} }}
第三步:添加Internet权限
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.main" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET"/><!-- 添加Internet权限 --> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".ActivityMain" 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></manifest>
- Android客户端与电脑服务器端
- Android客户端与服务器端交换之客户端
- android 客户端与web服务器端通信
- android客户端与c#服务器端的通信
- Android客户端与服务器端(struts2)通信
- android文件上传客户端与服务器端代码
- MINA框架----------android客户端与服务器端
- Android客户端与服务器端交互方式总结
- android客户端与服务器端的简单交互
- Android客户端与服务器端简单交互
- Android客户端与服务器端数据同步
- Android 实现蓝牙客户端与服务器端通信
- Android客户端与服务器端数据库同步
- android客户端与服务器端socket通信
- Android客户端与Web服务器端Http通信
- Android 客户端与服务器端时间校准
- Android客户端与服务器端数据同步
- Android客户端与服务器端数据同步
- Guava缓存器源码分析——CacheBuilder
- Oracle 自动备份 windows 任务计划, 创建表空间,打开1521端口,安装错误解决
- 简单设置固定大小的窗口activity
- 线程和进程的区别
- Java解析CSV文件
- Android客户端与电脑服务器端
- 【代码】android 程序代码混淆 proguard 脚本 proguard.cfg
- centos6.4 owncloud配置
- ubuntu12.04 误删/var/lib/dpkg
- Guava缓存器源码分析——LocalCache
- 如何在spring中读取properties配置文件里面的信息
- iOS 单例模式2
- windows phone 8中如何在App.xaml中使用ResourceDictionary
- 贪吃蛇小游戏(Linux Demo)