weixin_36505189
2017-06-01 11:05
采纳率: 75%
浏览 1.3k

C++服务端发送给客户端消息,但客户端没有显示

服务端代码:
#include"iostream"
#include
#pragma comment(lib,"ws2_32")
#include
using namespace std;
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in sockaddr;
sockaddr.sin_family = PF_INET;
sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sockaddr.sin_port = htons(827);
bind(s, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR));

listen(s, 1);

SOCKADDR clientAddr;
int nSize = sizeof(SOCKADDR);
SOCKET clientSock;
clientSock = accept(s, (SOCKADDR*)&clientAddr, &nSize);

send(clientSock,
    "helllo client \r\n",
    strlen("hello client \r\n") + sizeof(char),
    NULL);

closesocket(clientSock);
closesocket(s);

WSACleanup();
return 0;

}

客户端代码:
#include"iostream"
#include
#include
#pragma comment(lib,"ws2_32")
using namespace std;
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);

SOCKET s = socket(PF_INET,SOCK_STREAM, IPPROTO_TCP);

sockaddr_in sockAddr;
sockAddr.sin_family = PF_INET;
sockAddr.sin_addr.S_un.S_addr = inet_addr("127,0,0,1");
sockAddr.sin_port = htons(827);

connect(s, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));

char szBuffer[MAXBYTE] = { 0 };

recv(s, szBuffer, MAXBYTE, NULL);

printf("szBuffer=%s\r\n", szBuffer);

closesocket(s);
WSACleanup();
return 0;

}

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • threenewbee 2017-06-01 15:50
    已采纳

    抓包工具看下,是不是防火墙阻拦了,ip地址是否正确,网络是否通?如果是内网,有没有端口映射?

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • shen_wei 2017-06-02 08:39
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题