c sockets-cant connect to the server

83 views Asked by At

I have tried to send the server a string and the server will send me back that string.

The problem is that there is no connection(function connect returns SOCKET_ERROR)

The input argv is fine

argv[1] = "54.152.161.133" (server ip)
argv[2] = "6713" (port)
argv[3] = string

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<winsock2.h>
#include<windows.h>
#include<string.h>

//#define DEST_IP   "54.152.161.133"
//#define DEST_PORT 6713
// SOCKET WSAAPI socket(int af, int type, int protocol);
//int connect(SOCKET s, const struct sockaddr* addr, int numBytes);
//int send(SOCKET s, const char* buf,int len,int flags);
//int recv(SOCKET s, char* buf, int len, int flags);
//int closesocket(SOCKET s);
//int WSACleanup();
int main(int argc, char** argv)
{
    int cResult, flag = 0, len,s;
    char str2[10];
    WSADATA info;
    struct sockaddr_in clientService;
    unsigned short a = (unsigned short)strtoul(argv[2], NULL, 0);//convert from char* to short
    //config sockets
    int err;
    err = WSAStartup(MAKEWORD(2, 0), &info);
    if (err != 0)
    {
        printf("WSAStartup faild with error: %d\n", err);
        flag = 1;
    }
    //make empty socket
    if (flag == 0)
    {
        s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (s == INVALID_SOCKET)//Constant used for errors in socket()
        {
            //WSAGetLastError() get error code
            printf("Error creating socket = %d\n", WSAGetLastError());
        }
        else
        {
            printf("Socket function succeeded\n");
            /*close socket code*/
            cResult = closesocket(s);
            if (cResult == SOCKET_ERROR)
            {
                printf("Closesocket function faild with error %ld\n", WSAGetLastError());

            }
        }
        //config the socket
        sockaddr_in clientService;
        clientService.sin_family = AF_INET;
        clientService.sin_addr.s_addr = inet_addr(argv[1]);
        clientService.sin_port = htons(a);
        //ask for connection
        cResult = connect(s, (struct sockaddr *) &clientService, sizeof(clientService));
        if (cResult == SOCKET_ERROR)
        {
            printf("Connect function failed with error: %ld\n", WSAGetLastError());
            cResult = closesocket(s);
            if (cResult == SOCKET_ERROR)
            {
                printf("Closesocket function faild with error %ld\n", WSAGetLastError());
            }
            /*Erase WSADATA code*/
            WSACleanup();
            flag = 1;
        }
        if (flag = 0)
        {
            //send info throw socket
            len = strlen(argv[3]);
            cResult = send(s, *(argv + 3), len + 1, 0);
            if (cResult == SOCKET_ERROR)
            {
                printf("Sending data to the server has failed\n");
                /* Close socket code*/
                cResult = closesocket(s);
                if (cResult == SOCKET_ERROR)
                {
                    printf("Closesocket function faild with error %ld\n", WSAGetLastError());
                }
                /*Erase WSADATA code (we will see later) */
                WSACleanup();
                flag = 1;
            }
            if (flag == 0)
            {
                //gets data from socket
                cResult = recv(s, str2, len + 1, 0);
                if (cResult == SOCKET_ERROR)
                {
                    printf("Recving data from the server has failed\n");
                    /* Close socket code*/
                    cResult = closesocket(s);
                    printf("Closesocket function faild with error %ld\n", WSAGetLastError());
                    /*Erase WSADATA code (we will see later) */
                    WSACleanup();
                    flag = 1;
                }
                else if (cResult == 0)
                {
                    printf("The server closed the connection\n");
                    flag = 1;
                }
                if (flag == 0)
                {
                    printf(" string that sent to the server:%s\nstring that recv from the server:%s", argv[3], str2);
                    if (strcmp(argv[3], str2) == 0)
                    {
                        printf("The string that sent to the server\nand the string that recv from the server are the same\n");

                    }
                    else
                    {
                        printf("The string that sent to the server\nand the string that recv from the server are not the same\n");
                    }
                    cResult = closesocket(s);
                    if (cResult == SOCKET_ERROR)
                    {
                        printf("Closesocket function faild with error %ld\n", WSAGetLastError());
                    }
                    WSACleanup();

                }
            }


        }
    }
    system("PAUSE");
    return (0);
}
2

There are 2 answers

0
Aracthor On BEST ANSWER

In order, you created the socket:

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

Then you closed it:

/*close socket code*/
cResult = closesocket(s);

And after you tried to connect it to something:

cResult = connect(s, (struct sockaddr *) &clientService, sizeof(clientService));

Consider a socket as deleted once you closed it. You must close it only at the very end of your program.

And by the way, you should consider replace this line:

if (flag = 0)

By this one:

if (flag == 0)

If you just wanted to check the flag's value.

0
Diego On

Your socket is closed at the time of the connect call.