免费注册 查看新帖 |

Chinaunix

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

[C++] PHP与C++通信遇到一个问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-29 09:51 |只看该作者 |倒序浏览
php端的代码如下:


  1. <?php
  2.     $sh = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  3.     if (socket_connect($sh, '127.0.0.1', 27015)) {
  4.         echo "Socket connected correctly<br>";
  5.     }
  6.     $buf = 'Test Message';
  7.     $len = strlen($buf);
  8.     if (socket_send($sh, $buf, $len, 0x1) !== false) {
  9.         echo "Message sent correctly";
  10.     }
  11.     socket_close($sh);
  12. ?>
复制代码


C++的服务端:


  1. #include <windows.h>
  2. #include <iostream.h>
  3. #include <Winsock2.h>

  4. #define NO_FLAGS_SET 0

  5. #define PORT (u_short) 27015
  6. #define MAXBUFLEN 1024

  7. INT main(VOID)
  8. {
  9.   WSADATA Data;
  10.   SOCKADDR_IN serverSockAddr;
  11.   SOCKADDR_IN clientSockAddr;
  12.   SOCKET serverSocket;
  13.   SOCKET clientSocket;
  14.   int addrLen=sizeof(SOCKADDR_IN);
  15.   int status;
  16.   int numrcv;
  17.   char buffer[MAXBUFLEN] = "\0";

  18.   /* initialize the Windows Socket DLL */
  19.   status=WSAStartup(MAKEWORD(2, 1), &Data);
  20.   if (status != 0)
  21.     cerr << "ERROR: WSAStartup unsuccessful" << endl;

  22.   /* zero the sockaddr_in structure */
  23.   memset(&serverSockAddr, 0, sizeof(serverSockAddr));
  24.   /* specify the port portion of the address */
  25.   serverSockAddr.sin_port=htons(PORT);
  26.   /* specify the address family as Internet */
  27.   serverSockAddr.sin_family=AF_INET;
  28.   /* specify that the address does not matter */
  29.   serverSockAddr.sin_addr.s_addr=htonl(INADDR_ANY);

  30.   /* create a socket */
  31.   serverSocket=socket(AF_INET, SOCK_STREAM, 0);
  32.   if (serverSocket == INVALID_SOCKET)
  33.     cerr << "ERROR: socket unsuccessful" << endl;

  34.   /* associate the socket with the address */
  35.   status=bind(serverSocket, (LPSOCKADDR) &serverSockAddr, sizeof(serverSockAddr));
  36.   if (status == SOCKET_ERROR)
  37.     cerr << "ERROR: bind unsuccessful" << endl;

  38.   /* allow the socket to take connections */
  39.   status=listen(serverSocket, 1);
  40.   if (status == SOCKET_ERROR)
  41.     cerr << "ERROR: listen unsuccessful" << endl;

  42.   /* accept the connection request when one
  43.      is received */
  44.   clientSocket=accept(serverSocket, (LPSOCKADDR) &clientSockAddr, &addrLen);

  45.   cout << "Got the connection..." << endl;

  46.   while(1)
  47.   {
  48.     numrcv=recv(clientSocket, buffer, MAXBUFLEN, NO_FLAGS_SET);
  49.     if ((numrcv == 0) || (numrcv == SOCKET_ERROR))
  50.     {
  51.       cout << "Connection terminated." << endl;
  52.       status=closesocket(clientSocket);
  53.       if (status == SOCKET_ERROR)
  54.         cerr << "ERROR: closesocket unsuccessful" << endl;
  55.       status=WSACleanup();
  56.       if (status == SOCKET_ERROR)
  57.         cerr << "ERROR: WSACleanup unsuccessful" << endl;
  58.       return(1);
  59.     }
  60.     cout << buffer << endl;
  61.   } /* while */
  62. }
复制代码

现在遇到的问题是:PHP送出的字符串“Test Message”,服务器端显示的为“Test Messagq”,最后一位出错了。我的编译环境为MinGW。请大虾们指点。

论坛徽章:
0
2 [报告]
发表于 2007-01-29 11:44 |只看该作者
可以参考下这个示例, "CGI 与后台程序用 socket 进行消息通信"
http://www.eybuild.com/develop/download.htm#sendmsg
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP