- if ((s.sin_addr.s_addr = inet_addr(host)) == -1)
- {
- hp = gethostbyname(host);
- if (!hp)
- fatal("Host not found");
- s.sin_addr = *(struct in_addr *)(hp->h_addr_list[0]);
- }
- }
- else
- s.sin_addr.s_addr = inet_addr("127.0.0.1");
-
- s.sin_port = htons(port);
- s.sin_family = AF_INET;
- sfd = socket(PF_INET, SOCK_STREAM, 0);
- if (sfd < 0)
- fatal("Out of file descriptors");
- if ((tcpproto = getprotobyname("tcp")) != NULL)
- setsockopt(sfd, tcpproto->p_proto, TCP_NODELAY, (char *)&on, sizeof(int));
-
- if (connect(sfd, (struct sockaddr *)&s, sizeof(s)) < 0)
- {
- if (!host)
- {
- printf("No xtris server on localhost - starting up one ...\n");
- startserver();
- for (i=0; i<6; i++)
- {
- u_sleep(500000);
- close(sfd);
- sfd = socket(PF_INET, SOCK_STREAM, 0);
- if (sfd < 0)
- fatal("Out of file descriptors");
- setsockopt(sfd, tcpproto->p_proto, TCP_NODELAY, (char *)&on, sizeof(int));
- if (connect(sfd, (struct sockaddr *)&s, sizeof(s)) >= 0)
- break;
- }
- if (i==6)
- fatal("Can't connect to server");
- }
- else
- fatal("Can't connect to server");