I'm just starting out in C/C++, and I want to make a simple socket server script.
It will be compiled on mac/linux and executed from terminal.

Just want a simple script that will:
- allow multiple clients to connect to the server on a specified port (they will be using telnet)
- when a client sends a message it is printed on the server screen and then echoed to all connected clients

I've read through a few tutorials and they're all over complicated. Can anyone point me in the right direction?

cheers

Recommended Answers

All 2 Replies

Can you post a link to a tutorial you found?
In C/C++, this is fairly complicated (if you are just starting out).

I saw this same type of question on different forums and I saw:

you might want to use the event loop support provided by GLib and use the related networking library GNet.

Here's how to use GNet to open a socket on port 4000, then close every connection made to it. There is a little bit of magic here as the server registers itself with the default main context as part of its creation.

I modified the second link as theirs was broken.

#include <glib.h> 
#include <gnet.h> 
 
void client_connect(GServer G_GNUC_UNUSED *server, GConn *conn, gpointer G_GNUC_UNUSED user_data){ 
  g_print("Connection from %s\n", conn->hostname); 
  gnet_conn_disconnect(conn); 
  gnet_conn_unref(conn); conn = NULL; 
} 
 
int main(void){ 
  GMainLoop *loop = g_main_loop_new(NULL, FALSE); 
  GServer *server; 
  gnet_init(); 
  server = gnet_server_new(NULL, 4000, client_connect, NULL); 
  g_main_loop_run(loop); 
  g_main_loop_unref(loop); loop = NULL; 
  return 0; 
}

Sure thing, here you go ->
http://www.linuxhowtos.org/C_C++/socket.htm

Literally the first one that came up on google, but easily the most concise I've found so far.
I'll probably just sit down for a few hours and try and mod it to my needs.

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
     bzero(buffer,256);
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n",buffer);
     n = write(newsockfd,"I got your message",18);
     if (n < 0) error("ERROR writing to socket");
     close(newsockfd);
     close(sockfd);
     return 0; 
}
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.