C#写的客户端连接 php的服务器端的小例子

C#写的客户端连接 php的服务器端的小例子

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();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值