android socket通信

来源:互联网 发布:flotherm软件免费下载 编辑:程序博客网 时间:2024/06/11 05:12

例子源码下载:http://download.csdn.net/detail/qq_26437925/9385329


服务端(java application):

创建serverSocket ,等待客户端连接

import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class MyServer {//服务器连接public static ServerSocket serverSocket;//连接public static Socket socket;//端口public static final int PORT = 8888;public static void main(String[] args) {DataInputStream dis = null;DataOutputStream dos = null;try {serverSocket = new ServerSocket(PORT);System.out.println("正在等待客户端连接...");//这里处于等待状态,如果没有客户端连接,程序不会向下执行socket = serverSocket.accept();dis = new DataInputStream(socket.getInputStream());dos = new DataOutputStream(socket.getOutputStream());//读取数据String clientStr = dis.readUTF();//写出数据dos.writeUTF(clientStr);System.out.println("----客户端已成功连接!----");//得到客户端的IPSystem.out.println("客户端的IP =" + socket.getInetAddress());//得到客户端的端口号System.out.println("客户端的端口号 =" + socket.getPort());//得到本地端口号System.out.println("本地服务器端口号=" + socket.getLocalPort());System.out.println("-----------------------");System.out.println("客户端:" + clientStr);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {//我们把流的关闭写在finally里,即使读写出现问题,我们也能正常的关闭流!try {if (dis != null)dis.close();if (dos != null)dos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}


客户端(android application):

根据服务端信息,申请连接,发送数据:

package com.himi;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {private Button btn_ok;private EditText edit;private TextView tv;//Socket用于连接服务器获取输入输出流private Socket socket;//服务器server/IP地址private final String ADDRESS = "10.18.0.232";//服务器端口private final int PORT = 8888;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.main);btn_ok = (Button) findViewById(R.id.Btn_commit);tv = (TextView) findViewById(R.id.tv);edit = (EditText) findViewById(R.id.edit);btn_ok.setOnClickListener(this);}public void onClick(View v) {if (v == btn_ok) {DataInputStream dis = null;DataOutputStream dos = null;try {//阻塞函数,正常连接后才会向下继续执行socket = new Socket(ADDRESS, PORT);dis = new DataInputStream(socket.getInputStream());dos = new DataOutputStream(socket.getOutputStream());//向服务器写数据dos.writeUTF(edit.getText().toString());String temp = "I say:";temp += edit.getText().toString();temp += "\n";temp += "Server say:";//读取服务器发来的数据temp += dis.readUTF();tv.setText(temp);} catch (IOException e) {Log.e("Himi", "Stream error!");e.printStackTrace();} finally {try {if (dis != null)dis.close();if (dos != null)dos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}



参考学习:

http://blog.csdn.net/mad1989/article/details/9147661

http://blog.csdn.net/hguisu/article/details/7445768/


0 0
原创粉丝点击