码迷,mamicode.com
首页 > 移动开发 > 详细

Android Client 与 C# Server 的Socket通信

时间:2014-08-25 12:01:24      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:android   style   color   os   io   数据   ar   art   代码   

C# Socket Server

 

1.建立C# SocketServer

 

       

 1 private void ServerStart()
 2          {
 3              //创建IPEndPoint
 4              IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
 5              //创建Socket实例
 6              server_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 7              //绑定Socket与IPEndPoint
 8              server_Socket.Bind(ipep);
 9              //设置Socket收听模式
10              server_Socket.Listen(10);
11  
12             while (true)
13              {
14                  try
15                  {
16                      //接受Andorid信息
17                      client_Socket = server_Socket.Accept();
18                      client_Thread = new Thread(new ThreadStart(ReceiveAndroidData));
19                      client_Thread.Start();
20                  }
21                  catch (Exception ex)
22                  {
23                      MessageBox.Show("start error: " + ex.Message);
24                  }
25               
26             }
27          }

 

 

2.开启一个Theard,执行

       

1 server_Thread = new Thread(new ThreadStart(ServerStart));
2          server_Thread.Start();

 

 

3.介绍方法

 

       

private void ReceiveAndroidData()
         {
             bool keepalive = true;
             Socket socketclient = client_Socket;
             Byte[] buffer = new Byte[1024];
 
            //根据收听到的客户端套接字向客户端发送信息
             IPEndPoint clientep = (IPEndPoint)socketclient.RemoteEndPoint;
             lstServer.Items.Add("Andorid Client:" + clientep.Address + ""+clientep.Port);
             string str = "connect server----- ";
             byte[] data = new byte[1024];
             data = Encoding.ASCII.GetBytes(str);
             socketclient.Send(data, data.Length, SocketFlags.None);
 
            while (keepalive)
             {
                 //在套接字上接收客户端发送的信息
                 int buffer_lenght = 0;
                 try
                 {
                     buffer_lenght = socketclient.Available;
 
                    socketclient.Receive(buffer, 0, buffer_lenght, SocketFlags.None);
                     if (buffer_lenght == 0)
                         continue;
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("receive error:" + ex.Message);
                     return;
                 }
                 clientep = (IPEndPoint)socketclient.RemoteEndPoint;
                 string strAndroid_CMD = System.Text.Encoding.ASCII.GetString(buffer).Substring(0, buffer_lenght);
 
                 //对根据strAndroid_CMD到数据库中查询值
                 

 

 

Android Client:

1. OnCreate方法中执行

 

 

try {
   Socket socket = new Socket("10.0.2.2", 1234);

    
   PrintWriter out = new PrintWriter(new BufferedWriter(
     new OutputStreamWriter(socket.getOutputStream())), true);
   out.println(cameraid);
  } catch (UnknownHostException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 

代码很就简单

==================================

需要注意的是:

1.添加internet访问权限:<uses-permission android:name="android.permission.INTERNET" />。

2.127.0.0.1 IP被模拟器占用了,而本身PC的IP为10.0.2.2,如果本机测试的时候需要注意这一点。


Android Client 与 C# Server 的Socket通信

标签:android   style   color   os   io   数据   ar   art   代码   

原文地址:http://my.oschina.net/bv10000/blog/306228

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!