android TCP客户端通过socket与服务器通信
2017-03-09 22:07
579 查看
一次偶然的机会,接触一个android的项目,目前学习android已经一个月了,项目也已经差不多实现了基本的功能了。
从这个小项目中选择一些自认为比较虐心的,分享一下!
相信大多数刚接触android的爱好者来说都会从网上下载一些电子书学习,socket网络编程的资料也不少,尤其书中提供很多android TCP socket通信的实例,但还是花了一段时间才把这个小问题解决了。
需要注意的是android4.0版本
a146
以上者,其socket通信都不能直接放在活动的主线程中,需要另起一个线程进行数据通信;另外,需要获得网络权限<uses-permission android:name="android.permission.INTERNET" />;还有就是要细心加耐心吧!
下面是安卓模拟器的android客户端和计算机服务器通信的demo:
下面是手机客户端通过无线路由和计算机服务器通信的图片:
程序源码:
package com.example.tcpsocketactivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
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;
import android.widget.Toast;
public class TCPSocketActivity extends Activity {
public static final String TAG = TCPSocketActivity.class.getSimpleName();
/* 服务器地址 */
private String host_ip = null;
/* 服务器端口 */
private int host_port = 0;
private Button btnConnect;
private TextView tev_recv;
private Button btnSend;
private EditText editSend;
private EditText hostIP;
private EditText hostPort;
private Socket socket;
private PrintStream output;
private Context context;
private InputStream inputStream=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tcp_socket_activity);
context = this;
initView();
btnConnect.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
host_ip = hostIP.getText().toString();
host_port = Integer.parseInt(hostPort.getText().toString());
new Thread(new ConnectThread()).start();
}
});
btnSend.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new SendThread(editSend.getText().toString())).start();
}
});
}
private void toastText(String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
public void handleException(Exception e, String prefix) {
e.printStackTrace();
toastText(prefix + e.toString());
}
public void initView() {
tev_recv = (TextView)findViewById(R.id.tev_recv);
btnConnect = (Button) findViewById(R.id.btnConnect);
btnSend = (Button) findViewById(R.id.btnSend);
editSend = (EditText) findViewById(R.id.sendMsg);
hostIP = (EditText) findViewById(R.id.hostIP);
hostPort = (EditText) findViewById(R.id.hostPort);
}
private void closeSocket() {
try {
output.close();
socket.close();
} catch (IOException e) {
handleException(e, "close exception: ");
}
}
Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if (0x123 == msg.what) {
toastText("连接成功!");
}
}
};
/* 连接socket线程 */
public class ConnectThread implements Runnable {
@Override
public void run()
{
// TODO Auto-generated method stub
Message msg = Message.obtain();
try {
if (null == socket || socket.isClosed())
{
socket = new Socket();
socket.connect(new InetSocketAddress(host_ip,host_port),5000);
output = new PrintStream(socket.getOutputStream(), true, "utf-8");
inputStream = socket.getInputStream();
}
msg.what = 0x123;
handler.sendMessage(msg);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
byte[] buffer = new byte[1024];
final StringBuilder sb = new StringBuilder();
try
{
while(true)
{
int readSize = inputStream.read(buffer);
if(readSize > 0)
{
sb.append(new String(buffer,0,readSize));
runOnUiThread(new Runnable()
{
public void run()
{
tev_recv.setText(sb.toString());
}
});
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*发送信息线程*/
public class SendThread implements Runnable {
String msg;
public SendThread(String msg) {
super();
this.msg = msg;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
output.print(msg);
} catch (Exception e) {
e.printStackTrace();
}
//closeSocket();
}
}
}
AndroidManifest.xml:
layout代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/hostIP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:hint="服务器ip"
android:singleLine="true"
android:inputType="text"
android:text="192.168.0.168" />
<EditText
android:id="@+id/hostPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:hint="端口"
android:singleLine="true"
android:inputType="number"
android:text="8080" />
<Button
android:id="@+id/btnConnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="5dip"
android:text="connect" />
<TextView
android:id="@+id/tev_recv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/sendMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:hint="需要发送的内容"
android:inputType="text"
android:text="Hello,my name is Client" />
<Button
android:id="@+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_gravity="center_vertical|center_horizontal"
android:text="send" />
</LinearLayout>
从这个小项目中选择一些自认为比较虐心的,分享一下!
相信大多数刚接触android的爱好者来说都会从网上下载一些电子书学习,socket网络编程的资料也不少,尤其书中提供很多android TCP socket通信的实例,但还是花了一段时间才把这个小问题解决了。
需要注意的是android4.0版本
a146
以上者,其socket通信都不能直接放在活动的主线程中,需要另起一个线程进行数据通信;另外,需要获得网络权限<uses-permission android:name="android.permission.INTERNET" />;还有就是要细心加耐心吧!
下面是安卓模拟器的android客户端和计算机服务器通信的demo:
下面是手机客户端通过无线路由和计算机服务器通信的图片:
程序源码:
package com.example.tcpsocketactivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
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;
import android.widget.Toast;
public class TCPSocketActivity extends Activity {
public static final String TAG = TCPSocketActivity.class.getSimpleName();
/* 服务器地址 */
private String host_ip = null;
/* 服务器端口 */
private int host_port = 0;
private Button btnConnect;
private TextView tev_recv;
private Button btnSend;
private EditText editSend;
private EditText hostIP;
private EditText hostPort;
private Socket socket;
private PrintStream output;
private Context context;
private InputStream inputStream=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tcp_socket_activity);
context = this;
initView();
btnConnect.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
host_ip = hostIP.getText().toString();
host_port = Integer.parseInt(hostPort.getText().toString());
new Thread(new ConnectThread()).start();
}
});
btnSend.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new SendThread(editSend.getText().toString())).start();
}
});
}
private void toastText(String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
public void handleException(Exception e, String prefix) {
e.printStackTrace();
toastText(prefix + e.toString());
}
public void initView() {
tev_recv = (TextView)findViewById(R.id.tev_recv);
btnConnect = (Button) findViewById(R.id.btnConnect);
btnSend = (Button) findViewById(R.id.btnSend);
editSend = (EditText) findViewById(R.id.sendMsg);
hostIP = (EditText) findViewById(R.id.hostIP);
hostPort = (EditText) findViewById(R.id.hostPort);
}
private void closeSocket() {
try {
output.close();
socket.close();
} catch (IOException e) {
handleException(e, "close exception: ");
}
}
Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
if (0x123 == msg.what) {
toastText("连接成功!");
}
}
};
/* 连接socket线程 */
public class ConnectThread implements Runnable {
@Override
public void run()
{
// TODO Auto-generated method stub
Message msg = Message.obtain();
try {
if (null == socket || socket.isClosed())
{
socket = new Socket();
socket.connect(new InetSocketAddress(host_ip,host_port),5000);
output = new PrintStream(socket.getOutputStream(), true, "utf-8");
inputStream = socket.getInputStream();
}
msg.what = 0x123;
handler.sendMessage(msg);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
byte[] buffer = new byte[1024];
final StringBuilder sb = new StringBuilder();
try
{
while(true)
{
int readSize = inputStream.read(buffer);
if(readSize > 0)
{
sb.append(new String(buffer,0,readSize));
runOnUiThread(new Runnable()
{
public void run()
{
tev_recv.setText(sb.toString());
}
});
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*发送信息线程*/
public class SendThread implements Runnable {
String msg;
public SendThread(String msg) {
super();
this.msg = msg;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
output.print(msg);
} catch (Exception e) {
e.printStackTrace();
}
//closeSocket();
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tcpsocketactivity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".TCPSocketActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
layout代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/hostIP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:hint="服务器ip"
android:singleLine="true"
android:inputType="text"
android:text="192.168.0.168" />
<EditText
android:id="@+id/hostPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:hint="端口"
android:singleLine="true"
android:inputType="number"
android:text="8080" />
<Button
android:id="@+id/btnConnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="5dip"
android:text="connect" />
<TextView
android:id="@+id/tev_recv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/sendMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:hint="需要发送的内容"
android:inputType="text"
android:text="Hello,my name is Client" />
<Button
android:id="@+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_gravity="center_vertical|center_horizontal"
android:text="send" />
</LinearLayout>
相关文章推荐
- java 通过 socket 实现 服务器和客户端的通信 TCP
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Android客户端通过socket与服务器通信
- java 通过 socket 实现 服务器和客户端的通信 TCP
- Android客户端通过socket与服务器通信
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Android客户端通过socket与服务器通信
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通) .
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Android客户端通过socket与服务器通信
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)