android 服务端 例子

本文介绍了一个基于Android平台的简单网络服务端应用实现过程。该应用通过ServerSocket监听指定端口,接受客户端连接请求,并通过Socket进行数据交换。文中详细展示了如何在Android应用中设置监听端口、处理连接请求及数据收发等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


转载:http://www.cnblogs.com/yangfengwu/p/5294921.html#3860589
思路:

serversocket对象绑定端口
serverSocket = new ServerSocket(port);

Socket对象监听连接 ,如果无连接就会处于阻塞状态,inputstream对象获取输入流
clicksSocket = serverSocket.accept();
inputstream = clicksSocket.getInputStream();

Receive_Thread对象启动接收线程
receive_Thread.start();

inputstream对象获取消息
inputstream.read(buf);

ActivityMain.java


1 import android.app.Activity; 2 import android.net.wifi.WifiInfo; 3 import android.net.wifi.WifiManager; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 import java.io.IOException; 12 import java.io.InputStream; 13 import java.io.OutputStream; 14 import java.net.ServerSocket; 15 import java.net.Socket; 16 17 18 public class MainActivity extends Activity { 19 20 ServerSocket serverSocket;//创建ServerSocket对象 21 Socket clicksSocket;//连接通道,创建Socket对象 22 Button startButton;//发送按钮 23 EditText portEditText;//端口号 24 EditText receiveEditText;//接收消息框 25 Button sendButton;//发送按钮 26 EditText sendEditText;//发送消息框 27 InputStream inputstream;//创建输入数据流 28 OutputStream outputStream;//创建输出数据流 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 /** 34 * 读一下手机wifi状态下的ip地址,只有知道它的ip才能连接它嘛 35 */ 36 Toast.makeText(MainActivity.this, getLocalIpAddress(), Toast.LENGTH_LONG).show(); 37 38 startButton = (Button) findViewById(R.id.start_button); 39 portEditText = (EditText) findViewById(R.id.port_EditText); 40 receiveEditText = (EditText) findViewById(R.id.receive_EditText); 41 sendButton = (Button) findViewById(R.id.send_button); 42 sendEditText = (EditText) findViewById(R.id.message_EditText); 43 44 startButton.setOnClickListener(startButtonListener); 45 sendButton.setOnClickListener(sendButtonListener); 46 } 47 /** 48 * 启动服务按钮监听事件 49 */ 50 private OnClickListener startButtonListener = new OnClickListener() { 51 52 @Override 53 public void onClick(View v) { 54 // TODO Auto-generated method stub 55 /** 56 * 启动服务器监听线程 57 */ 58 ServerSocket_thread serversocket_thread = new ServerSocket_thread(); 59 serversocket_thread.start(); 60 } 61 }; 62 /** 63 * 服务器监听线程 64 */ 65 class ServerSocket_thread extends Thread 66 { 67 public void run()//重写Thread的run方法 68 { 69 try 70 { 71 int port =Integer.valueOf(portEditText.getText().toString());//获取portEditText中的端口号 72 serverSocket = new ServerSocket(port);//监听port端口,这个程序的通信端口就是port了 73 } 74 catch (IOException e) 75 { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 } 79 while (true) 80 { 81 try 82 { 83 //监听连接 ,如果无连接就会处于阻塞状态,一直在这等着 84 clicksSocket = serverSocket.accept(); 85 inputstream = clicksSocket.getInputStream();// 86 //启动接收线程 87 Receive_Thread receive_Thread = new Receive_Thread(); 8889 } 90 catch (IOException e) 91 { 92 // TODO Auto-generated catch block 93 e.printStackTrace(); 94 } 95 } 96 } 97 } 98 /** 99 * 100 * 接收线程 101 * 102 */ 103 class Receive_Thread extends Thread//继承Thread 104 { 105 public void run()//重写run方法 106 { 107 while (true) 108 { 109 try 110 { 111 final byte[] buf = new byte[1024]; 112 final int len = inputstream.read(buf); 113 //更新ui 114 runOnUiThread(new Runnable() 115 { 116 public void run() 117 { 118 receiveEditText.append(new String(buf,0,len)); 119 receiveEditText.append("\n"); 120 } 121 }); 122 } 123 catch (Exception e) 124 { 125 // TODO Auto-generated catch block 126 e.printStackTrace(); 127 } 128 } 129 } 130 } 131 /** 132 * 发送消息按钮事件 133 */ 134 private OnClickListener sendButtonListener = new OnClickListener() { 135 136 public static final String TAG = "sendButtonListener"; 137 138 @Override 139 public void onClick(View v) { 140 // TODO Auto-generated method stub 141 try 142 { 143 //获取输出流 144 outputStream = clicksSocket.getOutputStream(); 145 //发送数据 146 //outputStream.write(sendEditText.getText().toString().getBytes()); 147 outputStream.write("0".getBytes()); 148 } 149 catch (Exception e) 150 { 151 // TODO Auto-generated catch block 152 e.printStackTrace(); 153 } 154 } 155 }; 156 /** 157 * 158 * 获取WIFI下ip地址 159 */ 160 private String getLocalIpAddress() { 161 WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); 162 WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 163 // 获取32位整型IP地址 164 int ipAddress = wifiInfo.getIpAddress(); 165 166 //返回整型地址转换成“*.*.*.*”地址 167 return String.format("%d.%d.%d.%d", 168 (ipAddress & 0xff), (ipAddress >> 8 & 0xff), 169 (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); 170 } 171 }

 

activity_main.xml


1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <!-- 监听端口号 --> 7 <TextView 8 android:id="@+id/port_TextView" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="监听的端口:" /> 12 <!-- 端口号 --> 13 <EditText 14 android:id="@+id/port_EditText" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="12345" 18 android:layout_below="@id/port_TextView" 19 /> 20 21 <!-- 发送的消息 --> 22 <TextView 23 android:id="@+id/message_TextView" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="发送的消息" 27 android:layout_below="@id/port_EditText"/> 28 <!-- 发送消息框 --> 29 <EditText 30 android:id="@+id/message_EditText" 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:hint="输入要发送的消息:" 34 android:layout_below="@id/message_TextView" 35 /> 36 37 <!-- 启动按钮 --> 38 <Button 39 android:id="@+id/start_button" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="启动服务" 43 android:layout_below="@id/message_EditText" 44 android:layout_alignParentLeft="true" 45 /> 46 <!-- 发送按钮 --> 47 <Button 48 android:id="@+id/send_button" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="发送消息" 52 android:layout_below="@id/message_EditText" 53 android:layout_alignParentRight="true" 54 /> 55 <!-- 接收的消息--> 56 <TextView 57 android:id="@+id/receive_TextView" 58 android:layout_width="match_parent" 59 android:layout_height="wrap_content" 60 android:layout_below="@id/start_button" 61 android:text="接收的消息:" 62 /> 63 <!-- 接收消息框 --> 64 <EditText 65 android:gravity="top" 66 android:id="@+id/receive_EditText" 67 android:layout_width="match_parent" 68 android:layout_height="match_parent" 69 android:layout_below="@id/receive_TextView" 70 /> 71 </RelativeLayout>

 

转载于:https://www.cnblogs.com/zhihaowu/p/8004992.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值