connect failed in server/client
<p>I am trying to create a server and client, but when I compile your my code, and it is sucessfullly compile, I can't connect the my client to the server, it just return connect failed. I don't know why and this is my code for the server:`</p>

<pre><code>#ifndef unix
#define Win32
#include&lt;winsocks.h&gt;
#include&lt;windows.h&gt;
#else
#define closesocket close
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;
#endif
using namespace std;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;iostream&gt;
#include &lt;deque&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/ioctl.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;errno.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;cstring&gt;
#include "deck.h"
#include "servercontrol.h"
#define TRUE        1
#define FALSE       0
#define PROTOPORT       51717            /* default protocol port number */
#define QLEN            6               /* size of request queue        */
int     visits      =   0;              /* counts client */
fd_set      active_fd_read;
fd_set      working_fd_read;
int main(int argc,char *argv[])
{
struct  hostent  *ptrh;  /* pointer to a host table entry       */
struct  protoent *ptrp;  /* pointer to a protocol table entry   */
struct  sockaddr_in sad; /* structure to hold server's address  */
struct  sockaddr_in cad; /* structure to hold client's address  */
timeval timeout;
int lobbytimer;
int     sd_server, sd_client, sd_new;         /* socket descriptors          */
int max_sd;
int     port;            /* protocol port number                */
char    string[8];
socklen_t    alen;            /* length of address                   */
int waitingtime;
int m = 0;
char    buf[1000];       /* buffer for string the server sends  */
char    cmd[100];       /* buffer for user string              */
char    msg[100];
char    args[1000];
int     min_player = 2;
int     max_player = 10;
int     numplayers = 0;
int     len;
int     rc;
bool    server_finish =false;
bool    invalid = false;
int     OPTVAL = 1;
int     block = 1;  
deque   &lt;player_data&gt;player_deque;
deck* deck_card = new deck; 
#ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101, &amp;wsaData);
#endif
memset((char *)&amp;sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET;         /* set family to Internet     */
sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address   */
srand(unsigned(time(NULL)));

/* Check command-line argument for protocol port and extract    */
/* port number if one is specified.  Otherwise, use the default */
/* port value given by constant PROTOPORT                       */
if (argc &gt; 2) {
if ((max_player = atoi(argv[2])) &lt; 2)
{
    fprintf(stdout, "There is not enough player to start the game \n");
    max_player = 2;
}
else if ((max_player = atoi(argv[2])) &gt; 10)
{
    fprintf(stdout, "there is more than limit number of player \n");
    max_player = 10;
}
if ((min_player = atoi(argv[1])) &lt; 2)
{
    fprintf(stdout, "There is not enough player to start the game \n");
    min_player = 2;
}
else if ((min_player = atoi(argv[1])) &gt; 10)
{
    fprintf(stdout, "there is more than limit number of player \n");
    min_player = 10;
}
port = PROTOPORT;
} else if (argc &gt; 1) {
if ((min_player = atoi(argv[1])) &lt; 2)
{
    fprintf(stdout, "there is not enough player to start the game \n"); 
    min_player = 2;
}
else if ((min_player = atoi(argv[1])) &gt; 10)
{
    fprintf(stdout, "there is more than limit number of player \n");
    min_player = 10;
}
fprintf(stdout," No port number is set. Change the default port number %d\n", PROTOPORT);
port = PROTOPORT;                 /* if argument specified        */

} else {
fprintf(stdout, "No min player is set. Change the default min player at: %d\n", min_player);
fprintf(stdout, "No max player is set. Change the default max player at: %d\n", max_player);
fprintf(stdout," No port number is set. Change the default port number %d\n", PROTOPORT);
    port = PROTOPORT;       /* use default port number      */
}


if (port &gt; 0)                   /* test for illegal value       */
    sad.sin_port = htons((u_short)port);
else {                          /* print error message and exit */
    fprintf(stderr,"bad port number %s\n",argv[1]);
    exit(1);
}

/* Map TCP transport protocol name to protocol number */

if ( ((int)(ptrp == getprotobyname("tcp"))) == 0) {
    fprintf(stderr, "cannot map \"tcp\" to protocol number");
    exit(1);
}

/* Create a socket */

sd_server = socket(PF_INET, SOCK_STREAM, ptrp-&gt;p_proto);
setsockopt(sd_server, SOL_SOCKET, SO_REUSEADDR, &amp;OPTVAL, sizeof(OPTVAL));
if (sd_server &lt; 0) {
    fprintf(stderr, "socket creation failed\n");
    exit(1);
}else {
fprintf(stdout,"socket is created");}

rc = ioctl(sd_server,FIONBIO,(char *)&amp;block);


/* Bind a local address to the socket */

if (bind(sd_server, (struct sockaddr *)&amp;sad, sizeof(sad)) &lt; 0) {
    fprintf(stderr,"bind failed\n");
    exit(1);
}

/* Specify size of request queue */

if (listen(sd_server, QLEN) &lt; 0) {
    fprintf(stderr,"listen failed\n");
    exit(1);
}
alen = sizeof(cad);
/* Initialize the set of active sockets. */
FD_ZERO(&amp;active_fd_read);
max_sd = sd_server;
FD_SET(sd_server, &amp;active_fd_read);

/* set the timeval for the lobby */
 timeout.tv_sec = 1;
 timeout.tv_usec = 0;
 lobbytimer = 10;

/* Main server loop - accept and handle requests */
while {1}
{ (my code for function) }
</code></pre>

<p>and here is my client code:</p>

<pre><code>#ifndef unix
#define WIN32
#include &lt;windows.h&gt;
#include &lt;winsock.h&gt;
#else
#define closesocket close
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;
#endif
#include&lt;sys/ioctl.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include "deck.h"
#include "data.h"
#define PROTOPORT       51717           /* default protocol port number */
extern  int             errno;
char    localhost[] =   "localhost";    /* default host */
using namespace std;
int main(int argc, char *argv[])
{
struct  hostent  *ptrh;  /* pointer to a host table entry       */
struct  protoent *ptrp;  /* pointer to a protocol table entry   */
struct  sockaddr_in sad; /* structure to hold an IP address     */
int     sd, client, client_ready ;            /* socket descriptor                   */
int     port;            /* protocol port number                */
char    *host;           /* pointer to host name                */
int     n;               /* number of characters read           */
char    buf[1000];       /* buffer for data from the server     */
timeval time;
fd_set  active_fd_read;
fd_set  working_fd_read;
bool    playing = true;
char     name[100];
bool    active = false;
bool com_close;
handcard client_hand;
#ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101, &amp;wsaData);
#endif
memset((char *)&amp;sad,0,sizeof(sad)); /* clear sockaddr structure */
sad.sin_family = AF_INET;         /* set family to Internet     */

/* Check command-line argument for protocol port and extract    */
/* port number if one is specified.  Otherwise, use the default */
/* port value given by constant PROTOPORT                       */
if (argc &gt; 3) {
    port = atoi(argv[3]);
    if (strcmp(argv[2],"localhost")) 
       host = localhost;
    else host = argv[2];
    strcpy(name,argv[1]);
}
if (argc &gt; 2) {   
    if (strcmp(argv[2],"localhost")) 
       host = localhost;
    else host = argv[2];      
    strcpy(name,argv[1]);        /* if protocol port specified   */
    port = PROTOPORT;   /* convert to binary            */
} else if(argc &gt; 1)
 {
    strcpy(name,argv[1]);
    host = localhost;
    port = PROTOPORT;       /* use default port number      */
} else {
    fprintf(stderr,"Useage: ./client [name] [host] [port] \n");
    exit(0);
    host = localhost;
    port = PROTOPORT;
}
if (port &gt; 0)                   /* test for legal value         */
    sad.sin_port = htons((u_short)port);
else {                          /* print error message and exit */
    fprintf(stderr,"bad port number %s\n",argv[2]);
    exit(1);
}

/* Check host argument and assign host name. */

if (argc &gt; 2) {
    host = argv[2];         /* if host argument specified   */
}

/* Convert host name to equivalent IP address and copy to sad. */

ptrh = gethostbyname(host);
if ( ((char *)ptrh) == NULL ) {
    fprintf(stderr,"invalid host: %s\n", host);
    exit(1);
}
memcpy(&amp;sad.sin_addr, ptrh-&gt;h_addr, ptrh-&gt;h_length);

/* Map TCP transport protocol name to protocol number. */

if ( (ptrp = getprotobyname("tcp")) == 0) {
    fprintf(stderr, "cannot map \"tcp\" to protocol number");
    exit(1);
}

/* Create a socket. */

sd = socket(PF_INET, SOCK_STREAM, ptrp-&gt;p_proto);
if (sd &lt; 0) {
    fprintf(stderr, "socket creation failed\n");
    exit(1);
}

/* Connect the socket to the specified server. */

if (connect(sd, (struct sockaddr *)&amp;sad, sizeof(sad)) &lt; 0) {
    fprintf(stderr,"connect failed\n");
    exit(1);
}
FD_ZERO(&amp;active_fd_read);
FD_SET(sd,&amp;active_fd_read);
FD_SET(0,&amp;active_fd_read);
time.tv_sec = 5;
time.tv_usec = 0;
join(sd,name);

while (playing)
{ (my code for function) }
closesock(sd);
exit(0);
}
</code></pre>

<p>when I run ./server 51717 and call ./client localhost 51717, it is return conect failed. please help me!!!</p>

<p>`</p>
