C#客户端与Android服务端的Socket通信
2015-08-30 15:04
483 查看
1、Android服务端:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class G3Exp extends Activity{
/** Called when the activity is first created. */
//定义侦听端口号
final int SERVER_PORT = 10086;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//开一个新的线程来侦听客户端连接及发来的信息和打开相应网站
new Thread(){
public void run(){
startServer();
}
}.start();
}
private void startServer()
{
try
{
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
//循环侦听客户端连接请求
while (true)
{
Socket client = serverSocket.accept();
try
{
//等待客户端发送打开网站的消息
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
openUrl(str);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
client.close();
}
Thread.sleep(3000);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void openUrl(String url)
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
2、C#客户端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
namespace G3Exp.Class
{
public static class clsAndroid
{
static private void Connect(String server, String message)
{
try
{
//通过SDK下面的ADB命令来通知Android开侦听10086端口
Process p = new Process();
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory+@"\tools\adb.exe";
p.StartInfo.Arguments = "forward tcp:12580 tcp:10086";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
Int32 port = 12580;
TcpClient client = new TcpClient(server, port);
//把URL发给TCP服务端
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
public static void OpenWeb(string strUrl)
{
Connect("127.0.0.1", strUrl);
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class G3Exp extends Activity{
/** Called when the activity is first created. */
//定义侦听端口号
final int SERVER_PORT = 10086;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//开一个新的线程来侦听客户端连接及发来的信息和打开相应网站
new Thread(){
public void run(){
startServer();
}
}.start();
}
private void startServer()
{
try
{
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
//循环侦听客户端连接请求
while (true)
{
Socket client = serverSocket.accept();
try
{
//等待客户端发送打开网站的消息
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
openUrl(str);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
client.close();
}
Thread.sleep(3000);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void openUrl(String url)
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
2、C#客户端:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Diagnostics;
namespace G3Exp.Class
{
public static class clsAndroid
{
static private void Connect(String server, String message)
{
try
{
//通过SDK下面的ADB命令来通知Android开侦听10086端口
Process p = new Process();
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory+@"\tools\adb.exe";
p.StartInfo.Arguments = "forward tcp:12580 tcp:10086";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
Int32 port = 12580;
TcpClient client = new TcpClient(server, port);
//把URL发给TCP服务端
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
public static void OpenWeb(string strUrl)
{
Connect("127.0.0.1", strUrl);
}
}
}
相关文章推荐
- Android--Toast使用介绍
- Android应用程序用户界面(六)
- Android帮助文档本地打开慢的解决方案
- android 中 系统日期时间的获取
- adb uninstall卸载apk文件说明
- 在 Android 中 Intent 的概念及应用
- Android中插件开发篇总结和概述
- Android基础入门教程——5.2.3 Fragment实例精讲——底部导航栏的实现(方法3)
- Android DiskLruCache完全解析,硬盘缓存的最佳方案 对源码的理解
- 嵌套listview的高度问题解决
- Android eventbus开源框架(模仿)
- Android自学笔记之 MultiAutoTextView的功能、特殊属性、用法(与AutoTextView的区别和近似)
- 自定义属性TypedArray的使用步骤
- 【Android基础】Fragment 详解之Fragment介绍
- 【Android基础】Fragment 详解之Fragment介绍
- 查看Android应用包名package和入口activity名称
- Android Binder 机制
- android播放音频文件(MediaPlayer)和录音(MediaRecorder)--录音
- android 遇到TextView 没到末尾 自动换行 可能是 全角半角混合
- Android M 新的运行时权限开发者需要知道的一切