Thread: socket programming support multiple ports

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    1

    socket programming support multiple ports

    Hello friends;

    I want to put my socket programming example of how it can support multiple ports.
    I want to make the process more requests from distributing particles to create non-blocking structure

    ports support ports defined variable.

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "unistd.h"
    #include "errno.h"
    #include "string.h"
    #include "sys/socket.h"
    #include "netinet/in.h"
    #include "arpa/inet.h"
    #include "sys/types.h"
    
    
    #define BUFFSIZE 1024
    #define SOCKET_ERROR -1
    
    
    int listenfd = 0;
    int connection = 0;
    int ports[] = { 9000,9001,9002,9003,9004,9005 };
    
    
    struct sockaddr_in httpd;
    
    
    char sendBuff[BUFFSIZE];
    char fromBuff[BUFFSIZE];
    
    
    void syslog(int type,char * msg)
    {
        // sistem hata(sı) ve bilgi mesajları
        switch(type) 
        {
            case 1 : printf("Failed to listen (%s)\n",msg);
            case 2 : printf("There are a new user connection (%s)\n",msg);
        }
    }
    
    
    int main()
    {
        listenfd = socket(AF_INET,SOCK_STREAM,0);
        
        memset(&httpd,'0',sizeof(httpd));
        memset(sendBuff,'0',sizeof(sendBuff));
    
    
        // socket ayarlarımızı yapıyoruz
        httpd.sin_family = AF_INET;
        httpd.sin_addr.s_addr = htonl(INADDR_ANY);
        httpd.sin_port = htons(ports[1]); 
    
    
        bind(listenfd,(struct sockaddr*)&httpd,sizeof(httpd));
    
    
        // bağlantı durumunu kontrol ediyoruz.
        if(listen(listenfd,10) == SOCKET_ERROR)
        {
            syslog(1,NULL);
        }
    
    
        while(1)
        {
            connection = accept(listenfd,(struct sockaddr*)NULL,NULL);
    
    
            // HTTP başlıklarını yazıyoruz
            const char * header =
            (
                "HTTP/1.0 200 OK\n"
                "Content-Type: text/html; charset=utf-8\n\n"
                "Hello World"
            );
    
    
            // HTTP başlıklarını ve html verilerini gönderiyoruz
            strcpy(sendBuff,header);
            write(connection,sendBuff,strlen(sendBuff));
    
    
            // HTTP üzerinden gelen başlıkları ve verileri alıyoruz
            read(connection,fromBuff,sizeof(fromBuff));
            puts(fromBuff);
    
    
            // uyku moduna geçiriyoruz
            shutdown(connection,SHUT_RDWR);
            close(connection);
        }
    
    
        return 0;
    }
    Last edited by bgoksu; 09-03-2013 at 06:04 AM.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    There are some problems in your code, for example:
    Code:
    #include "stdio.h"
    // should be
    #include <stdio.h>
    for your includes.

    Did you actually compile this code? And. What is your question? I don't see one....

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jim mcnamara View Post
    Code:
    #include "stdio.h"
    // should be
    #include <stdio.h>
    actually, it will work exactly as written. using <> searches only the configured include paths for the file, while "" searches all of those paths, and the current directory.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help w/ Multiple Client Socket Programming (UDP)
    By Peanutty in forum C Programming
    Replies: 2
    Last Post: 05-21-2013, 04:11 AM
  2. Listening to Multiple Ports in c
    By iwant2learn in forum C Programming
    Replies: 2
    Last Post: 07-05-2011, 11:30 AM
  3. Replies: 18
    Last Post: 12-05-2003, 12:06 PM
  4. MFC :: Multiple File Extension Support
    By SyntaxBubble in forum Windows Programming
    Replies: 0
    Last Post: 06-02-2003, 09:18 AM
  5. Multiple Ports using Sockets
    By mmondok in forum C Programming
    Replies: 1
    Last Post: 04-14-2003, 09:36 PM