永久链接: http://liuzongming1988.iteye.com/blog/1922747
(1)BYTE TO STRING
(2)STRING TO BYTE
(3)THREAD
(4)HANDLE
- packagecom.FJICC.lzm;
- importjava.util.ArrayList;
- importjava.util.Enumeration;
- importjava.io.BufferedReader;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.InputStreamReader;
- importjava.io.OutputStream;
- importjava.net.InetAddress;
- importjava.net.NetworkInterface;
- importjava.net.Socket;
- importjava.net.ServerSocket;
- importandroid.app.Activity;
- importandroid.app.AlertDialog;
- importandroid.app.Dialog;
- importandroid.content.Context;
- importandroid.content.DialogInterface;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.view.View;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.TextView;
- publicclasstcp_serverextendsActivity{
- privateButtonserverStart_btn;
- privateButtonserverStop_btn;
- privateTextViewreceivedata_tv;
- privateButtonsetport_btn;
- privateEditTextsenddata_et;
- privateButtonsend_btn;
- publicintPORT=8080;
- publicHandlermHandler;
- protectedstaticfinalintGUINOTIFIER=0x1234;
- @Override
- publicvoidonBackPressed(){
- //TODOAuto-generatedmethodstub
- super.onBackPressed();
- Intenti=newIntent();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity(i);
- finish();
- }
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tcpserver_main);
- serverStart_btn=(Button)findViewById(R.id.btnStart);
- serverStop_btn=(Button)findViewById(R.id.btnStop);
- setport_btn=(Button)findViewById(R.id.btnSet);
- send_btn=(Button)findViewById(R.id.btnSend);
- senddata_et=(EditText)findViewById(R.id.et_send);
- receivedata_tv=(TextView)findViewById(R.id.tv_receive);
- serverStart_btn.setOnClickListener(newButton.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- serverStart_btn.setEnabled(false);
- setport_btn.setEnabled(false);
- serverStop_btn.setEnabled(true);
- newThread()
- {
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- super.run();
- ServerSocketserverSocket=null;
- try{
- //创建ServerSocket对象监听PORT端口
- serverSocket=newServerSocket(PORT);
- //接收tcp连接返回socket对象
- Socketsocket=serverSocket.accept();
- //获得输入流
- InputStreaminputStream=socket.getInputStream();
- ///////////////////////////////////////////////////////////////////////////////////////
- //获得输出流
- OutputStreamoutputStream=socket.getOutputStream();
- byte[]byteBuffer=newbyte[1024];
- inttemp=0;
- Strings;
- //读取接收到的数据
- while((temp=inputStream.read(byteBuffer))!=-1)
- {
- outputStream.write(byteBuffer,0,temp);
- //将byte转为string
- //String(byte[],int,int)使用平台的缺省字符编码方式转换指定的字节子数组生成一新的String
- s=newString(byteBuffer,0,temp);
- //将string转byte
- //byte[]bs=str.getBytes();
- //定义一个message的变量m
- Messagem=newMessage();
- //消息的标记GUINOTIFIER在前面定义的
- m.what=tcp_server.GUINOTIFIER;
- //将要传送的数据传递给m.obj
- m.obj=s;
- //传送消息
- tcp_server.this.mHandler.sendMessage(m);
- }
- //System.out.println(newString(byteBuffer,0,temp));
- outputStream.flush();
- socket.close();
- serverSocket.close();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- }.start();
- }
- });
- //创建handler
- mHandler=newHandler(){
- publicvoidhandleMessage(Messagemsg){
- switch(msg.what){//得到Handle的通知了这个时候你可以做相应的操作
- casetcp_server.GUINOTIFIER://tcp_server是Activity的类名
- //清空textView
- receivedata_tv.setText("");
- //设置textView显示内容
- receivedata_tv.setText(msg.obj.toString());
- break;
- }
- super.handleMessage(msg);
- }
- };
- //结束TCP服务器
- serverStop_btn.setOnClickListener(newButton.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- serverStart_btn.setEnabled(true);
- setport_btn.setEnabled(true);
- serverStop_btn.setEnabled(false);
- Intenti=newIntent();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity(i);
- finish();
- }});
- }
- }
- tcpserver_main.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btnStart"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="开启服务"/>
- <Button
- android:id="@+id/btnStop"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="关闭服务"/>
- <Button
- android:id="@+id/btnSet"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="端口设置"/>
- </LinearLayout>
- <TextView
- android:id="@+id/tv_receive"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.70"
- android:text="TextView"/>
- <EditText
- android:id="@+id/et_send"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="textMultiLine">
- <requestFocus/>
- </EditText>
- <Button
- android:id="@+id/btnSend"
- style="?android:attr/buttonStyleSmall"
- android:layout_width="148dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="发送"/>
- </LinearLayout>
- <uses-permissionandroid:name="android.permission.INTERNET"/>
相关推荐
TCP Server-Client Library库,方便使用,适合Win32 (XP/Vista/7/8), Win64, OSX, iOS and Android系统。
TCP Client/Server for Android
android TCP server 和TCP client通信源码 手机成功与串口WIFI模块ST-MW-08S通信。
本Demo主要实现了安卓(Android)TCP 客户端(Client)和服务器(Server)Demo的Socket通讯。以及对接硬件的项目数据在十六进制&&byte&&int的转换处理。 要注意BufferedReader的readLine()方法的阻塞问题: 读取socket输入...
android tcp协议 手机与手机之间通信 在路由器下测试没问题(非对称的网络) 其他的移动网络没试 就是一个思路
可以局域网连接电脑pc端的 tcpserver端口 并发送消息
Android TCP demo client server send receive,Android 4.4.4测试通过。
Android TCP服务器精简实用代码,适用于99%的android studio环境,同时集成一个网络调试工具,方便大家调试。
Android TCP/IP传送数据,可以和任意一台支持TCP/IP的Client进行数据交互。期中还有一些获取本机参数的代码可以无视...
Android TCP客户端源码程序,发送和接收都是好用的,已经测试过。你只需要改一下server的ip地址和端口号
使用客户端SSL / TLS支持对Android,iOS和macOS的本机TCP套接字API进行React。 它允许您创建TCP客户端和服务器套接字,以模仿Node的API功能(有关更多信息,请参见可用的 )。 目录 SSL客户端 原料药 TcpSocket ...
把Server和Client的功能集中到一个Activity,不过这个主要是提供思维,我相信,会对你有所帮助的。
Android 端的TCP Server应用,含简单的数据收发,代码从网上抄的,将工程上传方便使用。
Android手机TCP通信源代码 功能:手机作为客户端与服务器进行TCP通信的最简洁代码
ezyfox-server-android-client 用于ezyfox服务器的android客户端 概要 用于ezyfox服务器的android客户端 文献资料 程式码范例 1.创建一个TCP客户端 val clients = EzyClients .getInstance() val client = clients....
Connectivity using socket programing. TCP server in python and android client
Android Web服务器(FireFly) 适用于Java的Android的简单和小型TCP / IP Web服务器这是独立的,多线程的,几乎没有依赖性! Java中的http服务器,并在Android中使用示例。 支持的功能支持基本的GET,POST请求渲染...
HttpFilter 均采用字典树(Tried Tree)实现日志隔离,开发者可以使用自己的日志库core 模块只依赖 netty-all,不依赖其他第三方库最新版本模块最新版本android-server-coreandroid-server-converter-gson下载安装...
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...
在Native中实现Server的创建和Client的访问。 由一个apk实现,使用127.0.0.1本地监听访问。