c sockets-cant connect to the server -


i have tried send server string , server send me string.

the problem there no connection(function connect returns socket_error)

the input argv 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 = (unsigned short)strtoul(argv[2], null, 0);//convert char* short     //config sockets     int err;     err = wsastartup(makeword(2, 0), &info);     if (err != 0)     {         printf("wsastartup faild 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 errors in socket()         {             //wsagetlasterror() 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 error %ld\n", wsagetlasterror());              }         }         //config socket         sockaddr_in clientservice;         clientservice.sin_family = af_inet;         clientservice.sin_addr.s_addr = inet_addr(argv[1]);         clientservice.sin_port = htons(a);         //ask connection         cresult = connect(s, (struct sockaddr *) &clientservice, sizeof(clientservice));         if (cresult == socket_error)         {             printf("connect function failed error: %ld\n", wsagetlasterror());             cresult = closesocket(s);             if (cresult == socket_error)             {                 printf("closesocket function faild 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 server has failed\n");                 /* close socket code*/                 cresult = closesocket(s);                 if (cresult == socket_error)                 {                     printf("closesocket function faild error %ld\n", wsagetlasterror());                 }                 /*erase wsadata code (we see later) */                 wsacleanup();                 flag = 1;             }             if (flag == 0)             {                 //gets data socket                 cresult = recv(s, str2, len + 1, 0);                 if (cresult == socket_error)                 {                     printf("recving data server has failed\n");                     /* close socket code*/                     cresult = closesocket(s);                     printf("closesocket function faild error %ld\n", wsagetlasterror());                     /*erase wsadata code (we see later) */                     wsacleanup();                     flag = 1;                 }                 else if (cresult == 0)                 {                     printf("the server closed connection\n");                     flag = 1;                 }                 if (flag == 0)                 {                     printf(" string sent server:%s\nstring recv server:%s", argv[3], str2);                     if (strcmp(argv[3], str2) == 0)                     {                         printf("the string sent server\nand string recv server same\n");                      }                     else                     {                         printf("the string sent server\nand string recv server not same\n");                     }                     cresult = closesocket(s);                     if (cresult == socket_error)                     {                         printf("closesocket function faild error %ld\n", wsagetlasterror());                     }                     wsacleanup();                  }             }           }     }     system("pause");     return (0); } 

in order, created socket:

s = socket(af_inet, sock_stream, ipproto_tcp); 

then closed it:

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

and after tried connect something:

cresult = connect(s, (struct sockaddr *) &clientservice, sizeof(clientservice)); 

consider socket deleted once closed it. must close @ end of program.

and way, should consider replace line:

if (flag = 0) 

by one:

if (flag == 0) 

if wanted check flag's value.


Comments

Popular posts from this blog

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -