socket connection failure

olzha bala picture olzha bala · Sep 20, 2015 · Viewed 15.8k times · Source

I am beginner in socket programming and reading Linux Network Programming book. I decided to implement client-server connection as shown in the book. Server program is run on Ubuntu 14.04 machine and client code is run from Mac machine. The server code is the following

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>

const char message[] = "hello, world\n";

int main()
{
  int sock = 0;
  int port = 0;
  sock = socket(AF_INET, SOCK_STREAM, 0);

  if (sock == -1)
    fprintf(stderr, "failed\n");
  else
    printf("connection is establisshed\n");

  struct sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY );
  server.sin_port = 3500;

  int status = bind(sock, (struct sockaddr*) &server, sizeof(server));
  if (status == 0)
    printf("connection completed\n");
  else
    printf("problem is encountered\n");

  status = listen(sock, 5);
  if (status == 0)
    printf("app is ready to work\n");
  else
  {
    printf("connection is failed\n");
    return 0;
  }

  while (1)
  {
    struct sockaddr_in client = { 0 };
    int sclient = 0;
    int len = sizeof(client);
    int childSocket = accept(sock, (struct sockaddr*) &client, &len);
    if (childSocket == -1)
    {
      printf("cannot accept connection\n");
      close(sock);
      break;
    }

    write(childSocket, message, strlen(message));

    close(childSocket);
  }

  return 0;
}

As for client side i wrote the following code

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>

int main(int argc, char* argv[])
{
  int sock = 0;
  int port = 0;
  struct sockaddr_in servaddr;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  int status = 0;
  char buffer[256] = "";
  if (sock == -1)
  {
    printf("could not establish connection\n");
    exit(1);
  }

  port = 3500;
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = inet_addr(argv[1]);
  servaddr.sin_port = htons(port);
  status = connect(sock, (struct sockaddr*) &servaddr, sizeof(servaddr));
  if (status == 0)
    printf("connection is established successfully\n");
  else
  {
    printf("could not run the app\n");
    exit(1);
  }

  status = read(sock, buffer, sizeof(buffer));
  if (status > 0)
    printf("%d: %s", status, buffer);

  close(sock);

  return 0;
}

To get IP address of client machine I run ifconfig from terminal an get inet_addr 192.168.1.165 value. Now when I pass that address string as command line argument I get message that app is not running message. There is problem with address that I got, as I understand. So what is the problem? Thanks in advance

Answer

alk picture alk · Sep 20, 2015

Most probably the server does not listen on the port you are assuming, that is 3500.

To fix this, change this line:

server.sin_port=3500

to be

server.sin_port = htons(3500);

(To monitor which process is listing on which address:port you might like to use the netstat command line tool. In your case probably using the options -a -p -n )


Also on recent systems accept() expects a pointer to socklen_t as last parameter, so change this

int len=sizeof(client);

to be

socklen_t len = sizeof client; /* sizeof is an operator, not a function¨*/