免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3498 | 回复: 1
打印 上一主题 下一主题

php写的服务端和java写的android客户端通过socket通信 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-05 10:16 |只看该作者 |倒序浏览
  php写的服务端和java写的android客户端通过socket通信












php服务端的代码要在类似apache的服务器中运行,代码如下:
Php代码
  1. <?php      
  2.     //确保在连接客户端时不会超时      
  3.     set_time_limit(0);      
  4.          
  5.     //设置IP和端口号      
  6.     $address='127.0.0.1';      
  7.     $port=3333;    //调试的时候,可以多换端口来测试程序!      
  8.          
  9.     //创建一个SOCKET      
  10.     if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))<0)      
  11.     {      
  12.     echo "socket_create() 失败的原因是:".socket_strerror($sock)."<br>";      
  13.     }      
  14.          
  15.     //绑定到socket端口      
  16.     if(($ret=socket_bind($sock,$address,$port))<0)      
  17.     {      
  18.     echo "socket_bind() 失败的原因是:".socket_strerror($ret)."<br>";      
  19.     }      
  20.          
  21.     //开始监听      
  22.     if(($ret=socket_listen($sock,4))<0)      
  23.     {      
  24.     echo "socket_listen() 失败的原因是:".socket_strerror($ret)."<br>";      
  25.     }      
  26.          
  27.     do {      
  28.          if (($msgsock = socket_accept($sock)) < 0)      
  29.          {      
  30.              echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "/n";   
  31.              echo "/nThe Server is Stop……/n";   
  32.              break;      
  33.          }      
  34.          
  35.          //发到客户端      
  36.          $msg ="<font color=red>Welcome To Server!</font><br>";      
  37.          socket_write($msgsock, $msg, strlen($msg));   
  38.          socket_close($msgsock);      
  39.             
  40.          echo "/nThe Server is running……/n";   
  41.          printf("/nThe Server is running……/n");   
  42.     } while (true);      
  43.          
  44.     socket_close($sock);      
  45. ?>   

  46. <?php   
  47.     //确保在连接客户端时不会超时   
  48.     set_time_limit(0);   
  49.       
  50.     //设置IP和端口号   
  51.     $address='127.0.0.1';   
  52.     $port=3333;    //调试的时候,可以多换端口来测试程序!   
  53.       
  54.     //创建一个SOCKET   
  55.     if(($sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP))<0)   
  56.     {   
  57.     echo "socket_create() 失败的原因是:".socket_strerror($sock)."<br>";   
  58.     }   
  59.       
  60.     //绑定到socket端口   
  61.     if(($ret=socket_bind($sock,$address,$port))<0)   
  62.     {   
  63.     echo "socket_bind() 失败的原因是:".socket_strerror($ret)."<br>";   
  64.     }   
  65.       
  66.     //开始监听   
  67.     if(($ret=socket_listen($sock,4))<0)   
  68.     {   
  69.     echo "socket_listen() 失败的原因是:".socket_strerror($ret)."<br>";   
  70.     }   
  71.       
  72.     do {   
  73.                     if (($msgsock = socket_accept($sock)) < 0)   
  74.                 {   
  75.                             echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "/n";
  76.                             echo "/nThe Server is Stop……/n";
  77.                      break;   
  78.                     }   
  79.       
  80.                     //发到客户端   
  81.                     $msg ="<font color=red>Welcome To Server!</font><br>";   
  82.                     socket_write($msgsock, $msg, strlen($msg));
  83.                     socket_close($msgsock);   
  84.                     
  85.                     echo "/nThe Server is running……/n";
  86.                     printf("/nThe Server is running……/n");
  87.     } while (true);   
  88.       
  89.     socket_close($sock);   
  90. ?>  
复制代码
android客户端在同一台机器上的模拟器中运行,代码如下:
Java代码
  1. import java.io.IOException;   
  2. import java.io.InputStream;   
  3. import java.io.OutputStream;   
  4. import java.net.Socket;   
  5. import java.net.UnknownHostException;   
  6. import android.app.Activity;   
  7. import android.content.Intent;   
  8. import android.os.Bundle;   
  9.   
  10. public class SocketTest extends Activity {   
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {   
  14.         super.onCreate(savedInstanceState);   
  15.         setContentView(R.layout.main);   
  16.            
  17.         try {   
  18.                
  19.             System.out.println("准备连接");   
  20.             Socket socket = new Socket("10.0.2.2", 3333);   
  21.             System.out.println("连接上了");   
  22.                
  23.             Intent intent = new Intent();   
  24.             intent.setClass(SocketTest.this, ConnectActivity.class);   
  25.             SocketTest.this.startActivity(intent);            
  26.             InputStream inputStream = socket.getInputStream();   
  27.             byte buffer[] = new byte[1024*4];   
  28.             int temp = 0;   
  29.             String res = null;   
  30.             //从inputstream中读取客户端所发送的数据   
  31.             System.out.println("接收到服务器的信息是:");   
  32.                
  33.             while ((temp = inputStream.read(buffer)) != -1){   
  34.                 System.out.println(new String(buffer, 0, temp));   
  35.                 res += new String(buffer, 0, temp);   
  36.             }   
  37.                
  38.             System.out.println("已经结束接收信息……");   
  39.                
  40.             socket.close();   
  41.             inputStream.close();   
  42.                
  43.         } catch (UnknownHostException e) {   
  44.             e.printStackTrace();   
  45.         } catch (IOException e) {   
  46.             e.printStackTrace();   
  47.         }   
  48.            
  49.     }   
  50. }  

  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.io.OutputStream;
  54. import java.net.Socket;
  55. import java.net.UnknownHostException;
  56. import android.app.Activity;
  57. import android.content.Intent;
  58. import android.os.Bundle;

  59. public class SocketTest extends Activity {
  60.     /** Called when the activity is first created. */
  61.     @Override
  62.     public void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         setContentView(R.layout.main);
  65.         
  66.         try {
  67.                
  68.                 System.out.println("准备连接");
  69.                         Socket socket = new Socket("10.0.2.2", 3333);
  70.                         System.out.println("连接上了");
  71.                        
  72.                         Intent intent = new Intent();
  73.                         intent.setClass(SocketTest.this, ConnectActivity.class);
  74.                         SocketTest.this.startActivity(intent);                       
  75.                         InputStream inputStream = socket.getInputStream();
  76.                         byte buffer[] = new byte[1024*4];
  77.                         int temp = 0;
  78.                         String res = null;
  79.                         //从inputstream中读取客户端所发送的数据
  80.                         System.out.println("接收到服务器的信息是:");
  81.                        
  82.                         while ((temp = inputStream.read(buffer)) != -1){
  83.                                 System.out.println(new String(buffer, 0, temp));
  84.                                 res += new String(buffer, 0, temp);
  85.                         }
  86.                        
  87.                         System.out.println("已经结束接收信息……");
  88.                        
  89.                         socket.close();
  90.                         inputStream.close();
  91.                        
  92.                 } catch (UnknownHostException e) {
  93.                         e.printStackTrace();
  94.                 } catch (IOException e) {
  95.                         e.printStackTrace();
  96.                 }
  97.         
  98.     }
  99. }
复制代码
注:服务端的程序要先运行起来,然后运行客户端。这只是一个简单的socket通信。

论坛徽章:
0
2 [报告]
发表于 2011-12-20 16:22 |只看该作者
希望于楼主多多交流哦
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP