Chinaunix首页 | 论坛 | 博客
  • 博客访问: 528334
  • 博文数量: 104
  • 博客积分: 2089
  • 博客等级: 大尉
  • 技术积分: 1691
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-29 08:48
文章分类

全部博文(104)

文章存档

2015年(1)

2013年(13)

2012年(31)

2011年(59)

分类: Web开发

2013-11-24 16:59:57

php的server 端


  1. <?php
  2.  
  3. // server.php
  4. set_time_limit( 0 );
  5. ob_implicit_flush();
  6. $socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
  7. socket_bind( $socket, '127.0.0.1', 8880 );
  8. socket_listen($socket);
  9. $acpt=socket_accept($socket);
  10. echo "> Acpt!\n";
  11. while ( $acpt ) {
  12.     echo "> ";
  13.     $words= trim(fgets(STDIN));
  14.     if(strlen($words) === 0) $words = "\n";
  15.     socket_write($acpt,$words);
  16.     $hear=socket_read($acpt,1024);
  17.     echo "client>" . $hear . "\n" ;
  18.     if("bye"==$hear){
  19.         socket_shutdown($acpt);
  20.         break;
  21.     }
  22.     usleep( 1000 );
  23. }
  24. socket_close($socket);
  25. echo "> bye bye\n";
  26. ?>
以交互式方法运行:

  1. php -a server.php


C#写的客户端


  1. public class Client
  2.     {
  3.         private static byte[] result = new byte[1024];
  4.         public string serverIp = "127.0.0.1";
  5.         public int severPort = 8880;

  6.         public Client(string serverIp, int serverPort)
  7.         {
  8.             this.serverIp = serverIp;
  9.             this.severPort = serverPort;
  10.         }

  11.         public void start()
  12.         {
  13.             //设定服务器IP地址
  14.             IPAddress ip = IPAddress.Parse(serverIp);
  15.             Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  16.             try {
  17.                 clientSocket.Connect(new IPEndPoint(ip, severPort));
  18.                 Console.WriteLine("连接服务器成功");
  19.             } catch {
  20.                 Console.WriteLine("连接服务器失败");
  21.                 return;
  22.             }

  23.             int receiveLength;
  24.             while (clientSocket.Connected) {

  25.                 receiveLength = clientSocket.Receive(result);
  26.                 string sv_word = Encoding.ASCII.GetString(result, 0, receiveLength);
  27.                 Console.WriteLine("Sever> {0}", sv_word);

  28.                 if (sv_word.Trim() == "bye") break;

  29.                 Console.Write("> ");
  30.                 string words = Console.ReadLine();
  31.                 if (string.IsNullOrEmpty(words)) words = "\n";
  32.                 clientSocket.Send(Encoding.ASCII.GetBytes(words));

  33.                 if (words.Trim() == "bye") break;


  34.             }
  35.             
  36.            if(clientSocket.Connected) clientSocket.Close();
  37.            Console.WriteLine("> bye bye");
  38.             
  39.         }
  40.     }


加入引入

  1. using System.Net;
  2. using System.Net.Sockets;
  3. using System.Threading;

在某处调用

  1. new Client("127.0.0.1", 8880).start();


OK
交互吧








阅读(3485) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

wwwkljoel2013-11-30 09:14:31

首先不知道你的留言是机器自动留言还是一番感悟之后的肺腑之谈。

原创是在很多积累之后的厚积薄发,能够解人之惑,或指引方向,提供方案,娱人心情等等让人有所收获。这样的文章能够帮助人,本身就有吸引力,其他地方又不存在,当然能够博得很好的眼球关注度。

但是,能把这些东西采集汇总,同样是吸引人的,让人能够更好更系统的获取自己关注的东西。而搜索引擎算法,那是不得不采取的方法,不然怎么粗采众文。人的想法是多样的,搜索算法增么能够尽人皆美。

原创和采集同样重要,正当的排名也是一种去粗求精的手段,何非议之有?

回复 | 举报

wwwkljoel2013-11-28 22:10:41

不知道你想说什么

回复 | 举报