socket programming problem (network not reachable) 
Author Message
 socket programming problem (network not reachable)

hi,
I have a small client/server programs using
unix socket. Client program returns "network
is unreachable" error when it calls connect().
Programs are short and straightforward (just
copied from the book 'unix network programming'
by R. Stevens), and there seems no network
problem in the computer lab. why it's "not
reachable"? I'm even running both programs in
the same machine.
computers in the lab are sun ultrasparc's
with sunOS5.6 installed. I'll really appreciate
for help.
thanks,
-lhee

--------------------------------
server program:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>

#define PORT 6192
#define HOSTADDR "host.syr.edu"

void main(int argc, char **argv)
{
  int sockfd, newsockfd, clien, buf;
  struct sockaddr_in serv_addr, cli_addr;

  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    printf("socket error\n");
  else printf("socket done\n");

  bzero ((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(PORT);

  if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <
0)
    printf("bind error\n");
  else printf("bind done\n");

  if (listen(sockfd, 5) < 0)
    printf("listen error\n");
  else printf("listen done\n");

  clien = sizeof(cli_addr);
  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clien);

  if (newsockfd < 0)
    printf("accept error\n");
  else printf("accept done\n");

  for (;;) {
    read(sockfd, &buf, sizeof(int));
    printf("data in\n");
  }

Quote:
}

------------------------------
client program:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>

#define PORT 6192
#define HOSTADDR "host.syr.edu"

void main(int argc, char **argv)
{
  int sockfd, buf;
  struct sockaddr_in serv_addr;

  bzero ((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(HOSTADDR);
  serv_addr.sin_port = htons(PORT);

  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    printf("socket error\n");

  if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
< 0)
    perror("connect error");

  for (;;) {
    scanf("%d", &buf);
    write(sockfd, &buf, sizeof(int));
  }

Quote:
}

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Wed, 24 Jul 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. socket programming problem (network not reachable)

2. Windows Sockets Network Programming

3. sample code for simple network socket programming

4. network programming:COM,Socket,TCPIP?

5. Socket Program(Host Order/Network Order)

6. Meda Control to network program - does not execute

7. Socket programming: Using IP_ADD_MEMBERSHIP is not defined???

8. Problem concerns with network programming.

9. Problems initiating network programming

10. Error: network not present or not started

11. HELP -- Socket programming problem in Linux

12. compilation of sockets program problem

 

 
Powered by phpBB® Forum Software