Event-driven Model in C with Sockets

后端 未结 5 1317
攒了一身酷
攒了一身酷 2021-01-30 14:40

I am really interested in event-driven programming in C especially with sockets so I am going to dedicate some time doing my researches.

Let\'s assume that I want to bui

5条回答
  •  情歌与酒
    2021-01-30 15:36

    It's actually very platform specific as to how that works.

    If your running on a linux system it's really not to difficult though, you simply need to spawn a copy of your process using 'fork' something like the following would do the trick:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
      int server_sockfd, client_sockfd;
      int server_len, client_len;
      struct sockaddr_in server_address;
      struct sockaddr_in client_address;
    
      server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
      server_address.sin_family = AF_INET;
      server_address.sin_addr.s_addr = htonl(INADDR_ANY);
      server_Address.sin_port = htons(1234);
      server_len = sizeof(server_address);
      bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
    
      listen(server_sockfd, 5);
    
      signal(SIGCHLD, SIG_IGN);
    
      while(1)
      {
        char ch;
        printf("Server Waiting\n");
        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len)
    
        // Here's where we do the forking, if you've forked already then this will be the child running, if not then your still the parent task.
    
        if(fork() == 0)
        {
          // Do what ever the child needs to do with the connected client
          read(client_sockfd, &ch, 1);
          sleep(5); // just for show :-)
          ch++;
          write(client_sockfd, &ch, 1);
          close(client_sockfd);
          exit(0);
        }
        else
        {
          // Parent code here, close and loop for next connection
          close(client_sockfd);
        }
      }
    }
    

    You may have to fiddle with that code a little I'm not near a Linux box at the moment to do a test compile, and I've pretty much typed it from memory.

    Using fork however is the standard way to do this in C under a Linux / Unix based system.

    Under windows it's a very different story, and one which I can't quite remember all the code needed (I'm way to used to coding in C# these days) but setting up the socket is pretty much the same except you need to use the 'Winsock' API for better compatibility.

    You can (I believe anyway) still use standard berkley sockets under windows but it's full of pitfalls and holes, for windows winsock this is a good place to start:

    http://tangentsoft.net/wskfaq/

    As far as I'm aware too, if your using Winsock it has stuff in to help with the spawning and multi client, myself personally however, I usually just spin off a separate thread and copy the socket connection to that, then go back into the loop listening to my server.

提交回复
热议问题