Socket通信——Android手机客户端和电脑服务器通信小例子
2014-01-05 15:56
676 查看
TCP Socket 通信 小例子:
流程:
1.电脑端服务器打开,监听端口号:9998
2.当手机连上服务器,发送 "您好,服务器!"的消息,服务器通过DataInputStream接收之后,打印出来
3.服务器接收并打印消息之后,随机发送“您好,客户端!”的消息给手机客户端
手机刚打开客户端的界面:
手机点击"连接服务器"之后,服务器收到消息:
服务器返回给客户端的消息:
电脑服务器:
Android手机客户端程序
main.xml
MainActivity.java 文件
1 | 前提:电脑端当做服务器,Android手机作为客户端,在同一个局域网内 |
1.电脑端服务器打开,监听端口号:9998
2.当手机连上服务器,发送 "您好,服务器!"的消息,服务器通过DataInputStream接收之后,打印出来
3.服务器接收并打印消息之后,随机发送“您好,客户端!”的消息给手机客户端
手机刚打开客户端的界面:
手机点击"连接服务器"之后,服务器收到消息:
服务器返回给客户端的消息:
电脑服务器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package TestSocket; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.*; import javax.net.ssl.SSLContext; public class TcpServer { /** * 电脑服务器端,监听端口号9998,接收消息之后,再回复消息 * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { while (true) { ServerSocket ss = new ServerSocket(9998);// 监听9998端口 Socket socket = ss.accept(); //等待客户端连上,并等待接收数据 DataInputStream dis = new DataInputStream(socket.getInputStream()); System.out.println(dis.readUTF()); //打印出客户端发来的数据 //回复消息给客户端 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeUTF("您好,客户端!"); ss.close();//通信完之后要关闭,不然下次会报错 dos.close(); dis.close(); } } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | < RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:tools ="http://schemas.android.com/tools" android:layout_width ="fill_parent" android:layout_height ="fill_parent" > <TextView android:id ="@+id/tv" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:layout_centerHorizontal ="true" android:layout_centerVertical ="true" android:text ="@string/hello_world" /> <Button android:id ="@+id/btnconnect" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:text = "连接服务器" /> </ RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.example.socketclient; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.app.Activity; import android.content.DialogInterface; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Menu; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.*; public class MainActivity extends Activity { TextView tv;// 用来显示服务器返回的消息 :“您好,客户端!” Handler mHandler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btnconnect);// 连接服务器的按钮 btn.setOnClickListener(listener); tv = (TextView) findViewById(R.id.tv); mHandler = new MessageHandler(); } OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { new ReceiveMessageThread().start();// 开启客户端线程 } }; /** * 异步操作,TextView显示服务器发来的消息 * * @author S.Rain * */ class MessageHandler extends Handler { @Override public void handleMessage(Message msg) { tv.setText(msg.obj.toString());//接收客户端线程发来的Message对象,用来显示 } } /** * 客户端线程。连到服务器:192.168.1.102:9998 发送消息,之后接收服务器消息,并在TextView显示 * * @author S.Rain * */ class ReceiveMessageThread extends Thread { public void run() { try { Socket socket = new Socket("192.168.1.102", 9998); DataOutputStream dos = new DataOutputStream( socket.getOutputStream()); dos.writeUTF("您好,服务器!"); DataInputStream dis = new DataInputStream( socket.getInputStream()); Message msg = mHandler.obtainMessage(); msg.obj = dis.readUTF(); mHandler.sendMessage(msg); socket.close(); dos.close(); dis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) {// 没用,工程创建时自己生成的 getMenuInflater().inflate(R.menu.main, menu); return true; } } |
相关文章推荐
- 【Android】【报错】java.net.SocketException: socket failed: EACCES (Permission denied)
- Android中自带的RSA加密算法和JAVA中的区别
- 使用adb命令连接模拟器,在5037端口未被占用情况下仍报端口错误
- Android中回调函数机制解析
- Android 游戏与应用开发最佳学习路线图
- 高仿大众点评滑动效果
- Android中的三种XML解析方式
- android缩放大图片加载
- Android当中4种常用的对话框
- Android 之 ProgressBar用法介绍
- Android 之 PendingIntent用法介绍
- Android高手进阶之自定义View,自定义属性(带进度的圆形进度条)
- Android 之 ImageSwitch图片转换器
- Android自定义属性,format详解
- Android XML文件的代码提示
- android不规则图片按钮
- Android源代码下载
- Android开发必备库
- 一种基于Android智能手机的远程视频监控的设计[图]
- Android ART机制分析