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();}}}}
根据服务端信息,申请连接,发送数据:
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
- Android通信一:Socket通信
- Android Socket网络通信
- Android Socket网络通信
- android socket通信
- Android Socket网络通信
- Android Socket网络通信
- android socket通信
- android socket通信
- android socket通信
- Android Socket网络通信
- android socket通信
- Android之socket通信
- Android 之 Socket通信
- android socket通信
- Android socket 连接通信
- android socket通信问题
- Android之Socket通信
- android socket 通信例子
- Compact Composite Descriptors 紧致图像描述子
- 微信自定义分享功能
- MFC下,配置OpenGL所需文件、配置方法与注意事项
- SIMPLE Descriptors (Extended) 简单图像描述子
- C#中在WebClient中使用post发送数据实现方法
- android socket通信
- 三角函数
- cf good bye 2015
- hdu1022Train Problem I(栈-STL)
- 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel
- Android的WebView的使用
- 三款工作流引擎比较:WWF、netBPM 和 ccflow 下面将对目前比较主流的三款工作流进行介绍和比较,然后通过三款流程引擎分别设计一个较典型的流程来给大家分别演示这三款创建流程的过程.这
- windows无硬件连接时如何与虚拟机中linux通信
- 使用AndroidStudio出现的问题(1)