开发者

Cannot connect to socket

Hey im writing an echo client, and for some reason the connectsock function is returning an error, and returning an INVALID_SOCKET. I can't figure out why is this. Can someone tell me why

/*********************************
*   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;
}


I think that you should use

      remote.sin_port = port;

instead of what you use:

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

because at some point before in the function you already try to convert the service into a port number. If the service is specified by name, as it seems in your example, this code will always give a remote.sin_port of 0.


When you run this code, are you possibly supplying a socket that is already in use? In windows you can do

netstat -a

And take a look to make sure that socket isn't already in use.

Note: I'm not a window user's so that command might now work.


Based on @Diego's sharp eye, I also noticed a second issue. This code:

remote.sin_addr.s_addr = inet_addr(host);

is odd because earlier you had checked inet_addr and then fallen back on gethostbyname if inet_addr failed. So if you have supplied a hostname, this will cause problems as well. You need to change that to:

remote.sin_addr.s_addr = ihost;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜