Socket SocketServer编程可以说是比较底层的编程方式,对于其他的高级协议都是建立在此基础上,而且Socket 编程是跨平台的,可以在异构语音之间进行通信。
Socket 网络连接模型
Socket编程实例:
创建一个java 类作为服务器,android 应用程序作为客户端
服务器端代码:
- package com.server;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.text.SimpleDateFormat;
- public class MyServer {
- private static int count=0;
- public static void main(String[]args){
- try {
- //实例化服务器套接字 设置端口号8888
- ServerSocket server=new ServerSocket(8888);
- while(true){
- //连接编号设置
- count=count+1;
- //时间格式
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
- //实例化客户端
- Socket client=server.accept();
- //实例化时间 以及 id
- System.out.println(count+":"+sdf.format(System.currentTimeMillis()));
- //获取输出流
- OutputStream out=client.getOutputStream();
- //输出字符串
- String msg="Hello,Android!";
- //写字符串
- out.write(msg.getBytes());
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
android 客户端代码:
res/layout/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/rev"
- android:text="开始接收"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:id="@+id/receiver"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Is Reveiving...." />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.client"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="10" />
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".MyClientActivity"
- 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>
MyClientActivity .java 实现客户端 activity
- package com.client;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- 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 MyClientActivity extends Activity {
- /** Called when the activity is first created. */
- private Button rev=null;
- private TextView revtext=null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- rev=(Button)findViewById(R.id.rev);
- revtext=(TextView)findViewById(R.id.receiver);
- rev.setOnClickListener(new receiverlistenr());
- }
- class receiverlistenr implements OnClickListener{
- public void onClick(View v) {
- // TODO Auto-generated method stub
- try {
- //实例化Socket
- Socket socket=new Socket("169.254.202.149",8888);
- //获得输入流
- InputStream in=socket.getInputStream();
- //缓冲区
- byte[] buffer=new byte[in.available()];
- //读取缓冲区
- in.read(buffer);
- //转换字符串
- String msg=new String(buffer);
- //设置文本框的字符串
- revtext.setText(msg);
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- Socket("169.254.202.149",8888);
开始--附件--字符提示命令--ipconfig
运行结果:
1.启动服务器等待监听
2.运行android客户端程序
3
http://blog.csdn.net/forsta/article/details/7424290