Hello everyone,

I'm a moderately experienced C++ programmer who has decided its finally time to learn socket programming. I want to learn by writing a simple client/server pair of apps. The client will send a simple text string across the network to the server. So far, I've only worked on the server. This is a learning exercise, so I want to keep everything as simple as possible.

As I understand it, the basic process for building a server is:

1) Create the socket with socket()
2) Bind the socket to the server's IP address with bind()
3) Then continually listen to the socket with listen()
4) If a message comes in, accept it with accept()
5) Maybe do additional stuff depending on what you want the server to do.

My server has steps 1 through 4, but I'm puzzled by two things...

(A) When I run my code (below) the code reaches the accept() statement and reports "ERROR - Can't accept client connection!" meaning it accepted but then skipped through the listen() statement. I assume I have to set up some kind of repeating loop so the server will "wait on" the listen() statement until the client sends a message. But I don't see any such loops set up in any of the online sources I've read. (Beej's Guide to Network Programming, Linux Howto's) What am I doing wrong here?

(B) Also... this item bugged me. As a troubleshooting measure, I wanted to make sure that my code was accurately reading the IP address off the TCP/IP stack. You'll see I have two cout statements which (I thought) would print out the source machine's IP address. Yet when the code runs, both cout statements report "This host's IP address is: 0". Does that mean my code is incorrectly pulling the IP? Or is the cout statement not the correct way to print out an IP to the screen?

Many thanks everyone!
-George

============================================
MY CODE
============================================

/*
server.c
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;


int main(int argc, char *argv[])
  {

  // Some variable declarations...
  int sockfd, newsockfd;
  int MyPort, A;
  socklen_t ClientLength;
  char MyBuffer[500];
  

  // Initial address creation, double-checking of uder input, etc.
  MyPort = atoi(argv[1]);
  struct sockaddr_in ServerAddress, ClientAddress;
  if (argc < 2)
    { cout<<"ERROR - you must provide a port number!\n";  exit(1); }


  // Creating the socket:
  sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0)
    { cout<<"ERROR - socket was not opened!\n";  exit(1); }


  // Set MyBuffer to all 0's
  bzero((char *) &ServerAddress, sizeof(ServerAddress));  


  // Populate the ServerAddress struct...
  ServerAddress.sin_family = AF_INET;
  ServerAddress.sin_port = htons(MyPort);
  ServerAddress.sin_addr.s_addr = INADDR_ANY;

  cout<<"This host's IP address is: "<<INADDR_ANY<<"\n";
  cout<<"This host's IP address is: "<<ServerAddress.sin_addr.s_addr<<"\n";

  // Now to bind the socket...
  if (bind(sockfd, (struct sockaddr *) &ServerAddress, sizeof(ServerAddress)) < 0)
    { cout<<"ERROR - bind() did not take!\n";  exit(1); }  

  // Now we listen to the socket...
  listen(sockfd, 10);

  // When a client contacts us...
  ClientLength = sizeof(ClientAddress);
  newsockfd = accept(sockfd, (struct sockaddr *) &ClientAddress, &ClientLength);
  if (newsockfd < 0)
    { cout<<"ERROR - Can't accept client connection!\n";  exit(1); }
  else
    cout<<"New connection made by client!\n";



  }  // end of main()

http://www.rt.com/man/listen.2.html
You ignore the return result.
You're using the wrong kind of socket.

Use perror() to print a meaningful error message when a library call fails.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.