(1)BYTE TO STRING
(2)STRING TO BYTE
(3)THREAD
(4)HANDLE
- package com.FJICC.lzm;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.Socket;
- import java.net.ServerSocket;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class tcp_server extends Activity{
- private Button serverStart_btn;
- private Button serverStop_btn;
- private TextView receivedata_tv;
- private Button setport_btn;
- private EditText senddata_et;
- private Button send_btn;
- public int PORT = 8080;
- public Handler mHandler;
- protected static final int GUINOTIFIER = 0x1234;
- @Override
- public void onBackPressed() {
- // TODO Auto-generated method stub
- super.onBackPressed();
- Intent i =new Intent();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity(i);
- finish();
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- 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(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- serverStart_btn.setEnabled(false);
- setport_btn.setEnabled(false);
- serverStop_btn.setEnabled(true);
- new Thread()
- {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- super.run();
- ServerSocket serverSocket=null;
- try{
- //创建ServerSocket对象监听PORT端口
- serverSocket = new ServerSocket(PORT);
- //接收tcp连接返回socket对象
- Socket socket= serverSocket.accept();
- //获得输入流
- InputStream inputStream=socket.getInputStream();
- ///////////////////////////////////////////////////////////////////////////////////////
- //获得输出流
- OutputStream outputStream = socket.getOutputStream();
- byte []byteBuffer=new byte[1024];
- int temp = 0;
- String s;
- //读取接收到的数据
- while((temp = inputStream.read(byteBuffer))!=-1)
- {
- outputStream.write(byteBuffer, 0, temp);
- //将byte转为string
- //String(byte[], int, int)使用平台的缺省字符编码方式转换指定的字节子数组生成一新的 String
- s = new String(byteBuffer,0,temp);
- //将string转byte
- //byte[] bs = str.getBytes();
- //定义一个message的变量m
- Message m = new Message();
- //消息的标记GUINOTIFIER在前面定义的
- m.what = tcp_server.GUINOTIFIER;
- //将要传送的数据传递给 m.obj
- m.obj =s;
- //传送消息
- tcp_server.this.mHandler.sendMessage(m);
- }
- //System.out.println(new String(byteBuffer,0,temp));
- outputStream.flush();
- socket.close();
- serverSocket.close();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- }.start();
- }
- });
- //创建handler
- mHandler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {//得到Handle的通知了 这个时候你可以做相应的操作
- case tcp_server.GUINOTIFIER://tcp_server是Activity的类名
- //清空textView
- receivedata_tv.setText("");
- //设置textView显示内容
- receivedata_tv.setText(msg.obj.toString());
- break;
- }
- super.handleMessage(msg);
- }
- };
- //结束TCP服务器
- serverStop_btn.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- serverStart_btn.setEnabled(true);
- setport_btn.setEnabled(true);
- serverStop_btn.setEnabled(false);
- Intent i =new Intent();
- i.setClass(tcp_server.this,MainActivity.class);
- startActivity(i);
- finish();
- }});
- }
- }
- tcpserver_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" >
- <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-permission android:name="android.permission.INTERNET"/>
相关推荐
android TCP server 和TCP client通信源码 手机成功与串口WIFI模块ST-MW-08S通信。
TCP Server-Client Library库,方便使用,适合Win32 (XP/Vista/7/8), Win64, OSX, iOS and Android系统。
Android TCP服务器精简实用代码,适用于99%的android studio环境,同时集成一个网络调试工具,方便大家调试。
本Demo主要实现了安卓(Android)TCP 客户端(Client)和服务器(Server)Demo的Socket通讯。以及对接硬件的项目数据在十六进制&&byte&&int的转换处理。 要注意BufferedReader的readLine()方法的阻塞问题: 读取socket输入...
Android手机TCP通信源代码 功能:手机作为客户端与服务器进行TCP通信的最简洁代码
可以局域网连接电脑pc端的 tcpserver端口 并发送消息
eclipse开发安卓程序的代码,包括TCP client和TCP sever。TCP client读数据的时候貌似有点问题,读出来的数据格式需要自己确认一下。忘了哪儿搞的了,分享给大家,感谢原作者。
android tcp协议 手机与手机之间通信 在路由器下测试没问题(非对称的网络) 其他的移动网络没试 就是一个思路
Android TCP/IP传送数据,可以和任意一台支持TCP/IP的Client进行数据交互。期中还有一些获取本机参数的代码可以无视...
Android TCP demo client server send receive,Android 4.4.4测试通过。
TCP Client/Server for Android
比较完整的java TCP套件字通信实现demo,类的封装比较好,可以很方便接入到自己的项目中。
Android 端的TCP Server应用,含简单的数据收发,代码从网上抄的,将工程上传方便使用。
Android TCP客户端源码程序,发送和接收都是好用的,已经测试过。你只需要改一下server的ip地址和端口号
Connectivity using socket programing. TCP server in python and android client
把Server和Client的功能集中到一个Activity,不过这个主要是提供思维,我相信,会对你有所帮助的。
这是透过Mac的Android Studio撰写的TcpServer范例 有兴趣的人可以参考看看
android TCP服务端源码,android版本:2.33,已测试通过
在Native中实现Server的创建和Client的访问。 由一个apk实现,使用127.0.0.1本地监听访问。
android的server、client与PC的server、client相互通信的源码