freedom_wings5
freedom_wings5
2016-03-05 12:31

c++,socket,没启动客户端,服务端显示已有204.204.204.204的ip连接了

  • c++
  • 编程
  • socket
  • 网络编程
 #include "stdafx.h"
#include "winsock2.h"
#include "iostream"
#pragma comment (lib,"ws2_32")

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    WSADATA wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);
    cout << "开始创建套接字\n";
    SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    struct sockaddr_in  saddrin;
    saddrin.sin_family = AF_INET;
    saddrin.sin_addr.S_un.S_addr = inet_addr("INADDR_ANY");
    saddrin.sin_port = htons(9100);

    cout << "开始绑定\n";
    bind(s, (sockaddr *)&saddrin, sizeof(saddrin));

    cout << "开始监听\n";
    listen(s, SOMAXCONN);



    struct sockaddr_in sClient;
    int num = sizeof(sClient);
    SOCKET client=  accept(s, (sockaddr *)&sClient, &num);
    if (client)
    {
        cout << "已接受来自客户端的连接\n";
        cout << sClient.sin_port<<'\n';

        cout<< inet_ntoa(sClient.sin_addr)<<'\n';

        char msg[MAXBYTE] = { 0 };

        recv(client, msg, sizeof(msg), 0);
        cout << msg;

        //char msgReply[MAXBYTE ] = { 0 };
        strcpy_s(msg, "this is a reply for the test\n");
        send(client, msg, strlen(msg) + sizeof(char), 0);
        cout << "已发送消息\n";


    }
    else
    {
        cout << "无客户端的连接";
    }
    system("PAUSE");
    return 0;
}


图片说明
我的客户端还没启动,只启动了服务端,这个204.204.204.204就连接了,然后就返回了?这个ip是怎么回事?怎么解决?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答