基本服务器/客户端代码问题

时间:2016-05-03 00:19:20

标签: c++ sockets networking

我正在尝试用C ++教自己一些网络编程,但我遇到了一些我似乎无法解决的核心问题。

我有两个程序在同一个VS项目中 - 客户端和服务器。他们不交换信息,他们只是连接并告诉我什么时候建立连接。这是客户端文件 -

SERVER.CPP

#include "stdafx.h"

using namespace std;

int main()
{
    //setting up WSA
    WSADATA wsaData;
    int WSAcheck = WSAStartup(MAKEWORD(2, 2), &wsaData);

    //checking WSA
    if (WSAcheck != 0)
    {
        printf("WSA couldn't start correctly\n");
        pause();
        exit;
    }

    //setting up the socket
    SOCKET serversock = socket(AF_INET, SOCK_STREAM, 0);

    //checking socket's validity
    if (serversock == INVALID_SOCKET)
    {
        printf("Socket isn't valid: %d\n", WSAGetLastError());
        pause();
        exit;
    }

    //setting up the sockaddr_in for bind()
    sockaddr_in server_sockaddr;
    server_sockaddr.sin_family = AF_INET;
    server_sockaddr.sin_addr.s_addr = INADDR_ANY;
    server_sockaddr.sin_port = htons(25565);

    //binding the socket.
    int bindcheck = bind(serversock, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr));

    //checking to see if the bind worked, and calling the error if it doesn't
    if (bindcheck != 0)
    {
        printf("Bind failed: %d\n", WSAGetLastError());
        pause();
        exit;
    }

    //setting the socket to listen
    int listencheck = listen(serversock, 10);

    //checking to make sure the listen command was successful
    if (listencheck != 0)
    {
        printf("Listen failed: %d\n", WSAGetLastError());
        pause();
        exit;
    }

    //telling that the socket is being set to listen
    printf("Socket is ready to accept connections\n");

    //accepting any incoming connections
    SOCKET clientsocket = accept(serversock, NULL, NULL);

    //checking the socket
    if (clientsocket == INVALID_SOCKET)
    {
        printf("Connecting socket isn't valid or has timed out: %d\n", WSAGetLastError());
        pause();
        exit;
    }
    //ending the program
    printf("fin\n");
    pause();
}

CLIENT.CPP

#include "stdafx.h"

using namespace std;


int main()
{
    //setting up WSA
    WSADATA wsaData;
    int WSAcheck = WSAStartup(MAKEWORD(2, 2), &wsaData);

    //checking WSA
    if (WSAcheck != 0)
    {
        printf("WSA couldn't start correctly\n");
        pause();
        exit;
    }

    //setting up the socket
    SOCKET clientsock = socket(AF_INET, SOCK_STREAM, 0);

    //checking socket's validity
    if (clientsock == INVALID_SOCKET)
    {
        printf("Socket isn't valid: %d\n", WSAGetLastError());
        pause();
        exit;
    }

    //setting up the struct with the connection info
    sockaddr_in serverinfo;
    serverinfo.sin_family = AF_INET;
    serverinfo.sin_addr.s_addr = INADDR_ANY;
    serverinfo.sin_port = htons(25565);

    //connecting
    printf("Trying to connect\n");
    connect(clientsock, (SOCKADDR *)&serverinfo, sizeof(serverinfo));
    printf("Passed the connect function\n");
    pause();
}

STDAFX.H

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <Inaddr.h>
#include <string.h>
#include <io.h>
#include <Windef.h>
#include <Ws2tcpip.h>
#include <WinNT.h>
#include <Windows.h>
#include <tchar.h>

void pause(void);  //defined in stdafx.cpp

pause定义为getchar(),以打破代码以查看正在发生的事情。

客户端的输出是 -

尝试连接
通过了连接功能

无论服务器进程是否正在运行。

服务器的输出是

套接字已准备好接受连接

无论我测试什么,我都无法通过它。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您在客户端中出错。这很糟糕:

serverinfo.sin_addr.s_addr = INADDR_ANY;

如果它们在同一服务器上以二进制格式运行,则必须编写服务器IP地址,而不是INADDR_ANY,而不是INADDR_ANY。 使用此功能将文本IP地址转换为二进制IP地址:

INT WSAAPI InetPton(
  _In_   INT  Family,
  _In_   PCTSTR pszAddrString,
  _Out_  PVOID pAddrBuf
);

https://msdn.microsoft.com/en-us/library/windows/desktop/cc805844(v=vs.85).aspx

代码如下:

if(InetPton(AF_INET, "192.168.0.1", (void*)&serverinfo.sin_addr.s_addr) <= 0)
{
    //error
}