C++ 无法连接到套接字

C++ 无法连接到套接字,c++,windows,sockets,C++,Windows,Sockets,嘿,我正在编写一个echo客户端,由于某种原因,connectsock函数正在返回一个错误,并返回一个无效的_套接字。我不明白为什么会这样。有人能告诉我为什么吗 /********************************* * echo1.cpp * * * * Echo client - version 1 * *****************************

嘿,我正在编写一个echo客户端,由于某种原因,connectsock函数正在返回一个错误,并返回一个无效的_套接字。我不明白为什么会这样。有人能告诉我为什么吗

/*********************************
*   echo1.cpp                    *
*                                *
*   Echo client - version 1      *
**********************************/
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <string>
#include <iostream>
using namespace std;

const int  MAXBUF  = 128;
const int  MAXNAME = 80;

SOCKET connectsock (char*, char*, char*);

int main (int argc, char **argv) 
{
    char msg[MAXBUF] = "";
    char buf[MAXBUF] = "";
    char host[MAXNAME] = "";
    char service[MAXNAME] = "";
    int  len;
    SOCKET s;

    /* Must always initialize the Windows Sockets TCP/IP library */
    WORD wVersion = 0x0202;
    WSADATA wsaData;
    int iResult = WSAStartup( wVersion, &wsaData);
    if (iResult != 0) 
    {
        cout << "Unable to initialize Windows Socket library." << endl;
        return 0;
    }


    string hostaddress = argv[1];

    if(argv[2] == NULL)
    {
        strcpy_s(service, MAXNAME, "7");
    }
    else
    {
        strcpy_s(service, MAXNAME, argv[2]);
    }

    strcpy_s( host, MAXNAME, hostaddress.c_str() );



    s = connectsock(host, service, "tcp");
    if (s != INVALID_SOCKET) 
    {

        cout <<"Message?";
        cin.getline(msg, MAXBUF);


        // Now, let's try to send the message to the remote machine


        len = send(s, msg, (int) strlen(msg), 0);
        if (len > 0) 
        {
            cout << "Number of bytes sent: " << len << endl;
            cout << endl;
        } 
        else 
        {
            cout << "Unable to send message to remote machine." << endl;
            return 0;
        }

        // Message was sent.  Is there a reply from the remote machine?
        len = recv(s, buf, sizeof(buf), 0);
        if (len > 0) 
        {
            cout << "Received message: ";
            for (int i=0; i<len; i++) cout << buf[i];
            cout << endl;
            cout << "Number of bytes received: " << len << endl << endl;
        }
        cout << endl << endl;

        /* Close the socket (and thus the connection) */
        shutdown(s,1);
        closesocket(s);
    }
    WSACleanup();
}


/*------------------------------------------------------------------
* connectsock - allocate & connect a remote socket using TCP or UDP
*------------------------------------------------------------------
*/
SOCKET connectsock (char *host, char *service, char *protocol)
{   
    struct  hostent *phe;   
    struct  servent *pse;
    short   port;   
    int     ihost;  


    port = htons( (u_short) atoi(service)); // 1st try to convert string to integer
    if (port == 0)
    {                       // if that doesn't work, call service function
        pse = getservbyname(service,NULL);
        if (pse) 
        {
            port = pse->s_port;
        }
        else
        {
            cout << "Invalid service request." << endl;
            return INVALID_SOCKET;
        }
    }
/*
    cout << "Service: " << service << endl;
    cout << "Port:    " << htons(port) << endl;
*/


    ihost = inet_addr(host);    
    if (ihost == INADDR_NONE) 
    {   
        phe = gethostbyname(host);
        if (phe)
        {
            memmove(&ihost, phe->h_addr, phe->h_length);
        }
        else
        {
            cout << "Invalid Host." << endl;
            return INVALID_SOCKET;
        }
    }
/*
    cout << "Network address for hostname:         " << host << endl;
    cout << "32-bit address in Network Byte Order: " << ihost << endl;
    cout << "32-bit address in Host Byte Order:    " << ntohl(ihost) << endl;

    struct in_addr in;
    in.S_un.S_addr = ihost;
    cout << "Address as dotted decimal:            " << inet_ntoa(in) << endl;

*/

    struct sockaddr_in remote;  
    SOCKET s;                   

    if (_stricmp(protocol, "tcp") == 0) 
    {   
        s = socket(AF_INET, SOCK_STREAM, 0);

        if (s < 0 || s == INVALID_SOCKET)
        {
            cout << "Cannot create socket" << endl;
            return INVALID_SOCKET;
        }


        memset(&remote, 0, sizeof(remote));
        remote.sin_family = AF_INET;
        remote.sin_port = htons( (u_short) atoi(service));
        remote.sin_addr.s_addr = inet_addr(host);


        int status = connect(s, (LPSOCKADDR) &remote, sizeof(SOCKADDR));

        if (status == SOCKET_ERROR) 
        {
            cout << "Remote host/service not found - or connection refused" << endl;
            return INVALID_SOCKET;
        }
    }

    else if (_stricmp(protocol,"udp") == 0) 
    {   
        s = socket(AF_INET, SOCK_DGRAM,  0);
        if (s < 0 || s == INVALID_SOCKET) 
        {
            cout << "Cannot create socket" << endl;
            return INVALID_SOCKET;
        }
    }
    else 
    {                                   // Unknown or unsupported protocol request
        cout << "Invalid Protocol" << endl;
        return INVALID_SOCKET;
    }

    return s;
}
/*********************************
*echo1.cpp*
*                                *
*Echo客户端-版本1*
**********************************/
#包括
#pragma注释(lib,“ws2_32.lib”)
#包括
#包括
使用名称空间std;
常量int MAXBUF=128;
常量int MAXNAME=80;
套接字连接sock(char*,char*,char*);
int main(int argc,字符**argv)
{
char msg[MAXBUF]=“”;
char buf[MAXBUF]=“”;
字符主机[MAXNAME]=“”;
字符服务[MAXNAME]=“”;
内伦;
插座;
/*必须始终初始化Windows套接字TCP/IP库*/
字wVersion=0x0202;
WSADATA WSADATA;
int-iResult=WSAStartup(wVersion和wsaData);
如果(iResult!=0)
{

cout当您运行此代码时,您是否提供了一个已经在使用的套接字?在windows中,您可以这样做

netstat -a
并查看以确保该套接字尚未使用


注意:我不是windows用户,因此命令现在可以工作。

当您运行此代码时,是否可能提供已在使用的套接字?在windows中,您可以这样做

netstat -a
并查看以确保该套接字尚未使用


注意:我不是窗口用户,因此命令现在可以工作。

我认为您应该使用

      remote.sin_port = port;
而不是您使用的:

      remote.sin_port = htons( (u_short) atoi(service));

因为在函数之前的某个时刻,您已经尝试将服务转换为端口号。如果服务是按名称指定的,就像在您的示例中一样,此代码将始终提供一个0的remote.sinu端口。

我认为您应该使用

      remote.sin_port = port;
而不是您使用的:

      remote.sin_port = htons( (u_short) atoi(service));

因为在函数之前的某个时刻,您已经尝试将服务转换为端口号。如果服务是按名称指定的,正如您的示例中所示,此代码将始终提供0的remote.sinu端口。

基于@Diego敏锐的眼光,我还注意到了第二个问题。此代码:

remote.sin_addr.s_addr = inet_addr(host);
这很奇怪,因为之前您检查了inet_addr,如果inet_addr失败,则返回gethostbyname。因此,如果您提供了主机名,这也会导致问题。您需要将其更改为:

remote.sin_addr.s_addr = ihost;

基于@Diego敏锐的眼睛,我还注意到了第二个问题。此代码:

remote.sin_addr.s_addr = inet_addr(host);
这很奇怪,因为之前您检查了inet_addr,如果inet_addr失败,则返回gethostbyname。因此,如果您提供了主机名,这也会导致问题。您需要将其更改为:

remote.sin_addr.s_addr = ihost;

显然,
connectsock()
函数会返回
INVALID\u SOCKET
,原因有很多。调用该函数时会打印什么?到底是什么失败了?是连接、gethostbyname还是其他原因?是-哪个cout正在触发。然后添加GetLastError()以查找win32错误代码Its this line int status=connect(s,(LPSOCKADDR)&remote,sizeof(SOCKADDR));它返回一个无效的\u sockett函数打印远程主机/服务未找到
connectsock()
函数可以返回
无效的\u SOCKET
原因很多。调用该函数时会打印什么?到底是什么失败了?是连接、gethostbyname还是其他原因?是-哪个cout正在触发。然后添加GetLastError()以查找win32错误代码它的此行int status=connect(s,(LPSOCKADDR)&remote,sizeof(SOCKADDR));它返回一个无效的\u sockett函数打印remoteHost/Service not foundsysinternals tcpview是一个相当于netstatsysinternals tcpview的漂亮gui。tcpview是一个相当于netstatGood catch的漂亮gui。也就是说,他实际上应该执行
remote.sinu\u port=port;
,因为端口已经处于网络顺序。捕捉良好。也就是说,他实际上应该执行
remote.sinu port=port;
,因为该端口已处于网络顺序。我尝试使用ihost切换它,但它仍会产生相同的结果我尝试使用ihost切换它,但它仍会产生相同的结果