![]() |
| | #1 |
| Prying open my third eye. Join Date: Jun 2005
Posts: 45
| Client/server won't connect server main() Code: int main(int argc, char *argv[])
{
int listenfd, connfd;
pid_t childp;
socklen_t clientlen;
struct sockaddr_in server, client;
if (argc != 2) {
printf("USAGE: <port>\n");
exit(1);
}
/* create initial socket */
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
/* set socket info */
memset(&server, '\0', sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(atoi(argv[1]));
/* binds */
Bind(listenfd, (struct sockaddr *) &server, sizeof(server));
/* and listen */
Listen(listenfd, 5);
printf("Listening on port %d...\n", atoi(argv[1]));
/* signal handler */
signal(SIGCHLD, sig_chld);
/* child process loop */
for ( ; ; ) {
clientlen = sizeof(client);
printf("!");
if ((connfd = accept(listenfd, (struct sockaddr *) &client, &clientlen)) < 0) {
if (errno == EINTR)
continue;
else
perror("accept error");
}
if ((childp = fork()) == 0) { /* child process */
close(listenfd);
str_echo(connfd);
exit(0);
} else if (childp == -1)
perror("fork");
close(connfd);
}
}
Code: main(int argc, char *argv[])
{
int i, sockfd;
struct sockaddr_in server;
if (argc != 3) {
printf("USAGE: <ip address> <port> <# of connections>\n");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
memset(&server, '\0', sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_aton(argv[1]);
printf("Attempting connection to %s\n",argv[1]);
if (connect(sockfd, (struct sockaddr *) &server, sizeof(server)) == -1) {
perror("connect");
exit(1);
}
str_cli(stdin, sockfd);
exit(0);
}
EDIT: Well I found the problem: server.sin_addr.s_addr = inet_aton(argv[1]); this for some reason is setting an incorrect IP address. I was told to try inet_pton instead, which I am going to do now, but I am still curious why when argv[1] is "127.0.0.1" after fetching and printing the value for server.sin_addr.s_addr I get 1.0.0.0 EDIT AGAIN: Well after using inet_pton everything works, but I am still stumped as to why inet_aton didn't work. I used it in the exact same way in a pervious app with no problems. Feel free to deliver any insight on the issue.
__________________ "So you're one of those condescending UNIX computer users?" "Here's a nickel, kid. Get yourself a better computer." Last edited by Lateralus; 06-15-2005 at 09:19 AM. |
| Lateralus is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Non-blocking connect()? | pobri19 | Networking/Device Communication | 9 | 04-22-2009 03:40 PM |
| connect timeout | X PaYnE X | Networking/Device Communication | 8 | 05-14-2005 09:30 PM |
| async Client/Server app, accept() stalls? | JaWiB | Networking/Device Communication | 14 | 01-31-2005 05:59 PM |
| Client timed-out once on connect(), can never connect() again | registering | Networking/Device Communication | 6 | 10-28-2003 03:46 PM |
| MySQL Connect from Outside Computer | juschillin | Windows Programming | 0 | 09-27-2002 08:02 AM |