Results 1 to 4 of 4

Thread: Linux Socket Programming

  1. #1
    Join Date
    Jun 2009
    Posts
    50

    Linux Socket Programming

    I studied networking from classes and I have a project which should be done Linux in C, and programming! My project is as follows:

    "Write a program for implementation of banking services. The services include:
    - Display of account balance (value and debit / credit)
    - Display the account name (full name, address, account number)

    The operation of these services is a client / server which allows a networking application. The listening port of the server is 1444. The server can process more operations in parallel. In one transaction the client may request the completion of the 2 services or a single. The communication between server and client must consider an application service identified. On the server, a process of call management must be active, it allows to trace all calls and service requests made. This process must be able to view the content of his newspaper on request. To simplify the programming work structures corresponding to counts of bank information is stored in main memory. The service requests are made from the bank account number.

    1) Describe the mode of operation of this application by specifying the communication tools used. Specify, for each tool the reasons for your choice.
    2) Write all programs to achieve all of these services. Programs must be commented for easy reading. "

    Is there someone who can help me with this?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Linux Socket Programming

    Here are several examples of simplistic communication via socket. All these programs are the same: the client to enter a string and the server sends it to the poster.

    Example: communication across a socket file - It only works if the client and server are on the same machine
    The server

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <stdio.h>
    #include <signal.h>
    #include <errno.h>
     
    extern const char* const sys_errlist[];
     
    #define NAME_SOCKET ("socket_file" )
    #define SZ_BUF (256)
     
    main(
        int argc,
        char *argv[])
    {
        int sk_creat;
        int sk_dialog;                            
        int pid;                                    
        int sz_read;                                       
     
        char buf[SZ_BUF];                                    
     
        struct sockaddr_un adr_server;   
        signal(SIGCHLD, SIG_IGN);
        unlink(NAME_SOCKET);
        if ((sk_creat=socket(AF_UNIX, SOCK_STREAM, 0)) == (-1))
        {
            fprintf(stderr, "ligne %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
            exit(errno);
        }
        memset(&adr_server, 0, sizeof(struct sockaddr_un));
        adr_server.sun_len=strlen(NAME_SOCKET) + 1;
        adr_server.sun_family=AF_UNIX;
        memcpy(&(adr_server.sun_path), NAME_SOCKET, strlen(NAME_SOCKET));
     
        if (bind(sk_creat, &adr_server, sizeof(struct sockaddr_un)) == (-1))
        {
            fprintf(stderr, "ligne %u - bind() - %s\n", __LINE__, sys_errlist[errno]);
            exit(errno);
        }
     
        listen(sk_creat, 1);
        fputc('\n', stdout);
        while (1)
        {
            printf("ppid=%u, pid=%u\tAwaiting entry...", getppid(), getpid());
            fflush(stdout);
            if ((sk_dialog=accept(sk_creat, NULL, NULL)) == (-1))
            {
                fprintf(stderr, "line %u - accept() - %s\n", __LINE__, sys_errlist[errno]);
                exit(errno);
            }
            fputs("Entry issued\n", stdout);
            
            switch (pid=fork())
            {
                case (-1): 
                    close(sk_creat);
                    close(sk_dialog);
                    fprintf(stderr, "line %u - fork() - %s\n", __LINE__, sys_errlist[errno]);
                    exit(errno);
     
                case 0: 
                    close(sk_creat);
                    while ((sz_read=read(sk_dialog, buf, SZ_BUF)) > 0)
                    {
                        printf("\n\tppid=%u, pid=%u\tThe server has read '%s'%s\n", getppid(), getpid(), buf, strcmp(buf, "EOT" ) != 0 ?"" :"=> End call" );
     
                        if (strcmp(buf, "EOT" ) == 0)
                            break;
                    }
                    if (sz_read == (-1))
                    {
                        close(sk_dialog);
                        fprintf(stderr, "line %u - read() - %s\n", __LINE__, sys_errlist[errno]);
                        exit(errno);
                    }
     
                    close(sk_dialog);
                    exit(0);
     
                default: 
                    close(sk_dialog);
            }
        }
        close(sk_creat);
    }
    The client

    Code:
    #include <sys/types.h>        
    #include <sys/socket.h>                       
    #include <sys/un.h>                            
    #include <stdio.h>                             
    #include <string.h>                           
    #include <errno.h>                                     
     
    extern const char* const sys_errlist[];      
     
    #define NAME_SOCKET ("socket_file" )    
    #define SZ_BUF (256)          
     
    main(
        int argc,                             
        char *argv[])                             
    {
        int sk_connect;                         
        int sk_dialog;                            
     
        char buf[SZ_BUF];                          
        char *pt;                               
     
        struct sockaddr_un adr_server;         
     
        memset(&adr_server, 0, sizeof(struct sockaddr_un));
        adr_server.sun_len=strlen(NAME_SOCKET) + 1;
        adr_server.sun_family=AF_UNIX;
        memcpy(&adr_server.sun_path, NAME_SOCKET, strlen(NAME_SOCKET));
     
        fputc('\n', stdout);
        do
        {
            if ((sk_dialog=socket(AF_UNIX, SOCK_STREAM, 0)) == (-1))
            {
                fprintf(stderr, "line %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
                exit(errno);
            }
     
            if ((sk_connect=connect(sk_dialog, &adr_server, sizeof(struct sockaddr_un))) == (-1))
            {
                fprintf(stderr, "line %u - connect() - %s\n", __LINE__, sys_errlist[errno]);
                sleep(5);
            }
        }
        while (sk_connect == (-1));
        printf("Connection successful\n" );
     
        do
        {
            fputs("Enter string (EOT finally) :", stdout); fflush(stdout);
            fflush(stdin); fgets(buf, SZ_BUF, stdin);
     
            if ((pt=strchr(buf, '\n')) != NULL)
                *pt='\0';
    
            if (write(sk_dialog, buf, strlen(buf) + 1) == (-1))
                fprintf(stderr, "line %u - write(%s) - %s\n", __LINE__, buf, sys_errlist[errno]);
        }
        while (strcmp(buf, "EOT" ) != 0);
     
        close(sk_dialog);
    }
    Example 2: tcp communication.

    The server
    Code:
    #include <sys/types.h>          
    #include <sys/socket.h>                       
    #include <sys/param.h>                     
    #include <netinet/in.h>                     
    #include <arpa/inet.h>                      
    #include <signal.h>                         
    #include <stdio.h>                          
    #include <netdb.h>                          
    #include <errno.h>                         
     
    extern const char* const sys_errlist[];       
     
    #define SERVICE_LABEL ("foo" )      
    #define SERVICE_PROTOCOL ("tcp" )       
    #define SZ_BUF (256)          
     
    main(
        int argc,           
        char *argv[])    
    { 
        ushort i;              
        ushort j;         
     
        int sk_creat;      
        int sk_dialog;       
        int pid;              
        int len_adr;            
        int sz_read;         
     
        char buf[SZ_BUF];   
        char hostname[MAXHOSTNAMELEN + 1];   
     
        struct sockaddr_in adr_server;    
        struct sockaddr_in adr_client;   
        struct hostent *host_info;      
        struct servent *service_info; 
     
        char *adr_ascii;   
     
        signal(SIGCHLD, SIG_IGN);
     
        if (gethostname(hostname, MAXHOSTNAMELEN) != 0)
        {
            fprintf(stderr, "line %u - gethostname(%s) - %s\n", __LINE__, hostname, sys_errlist[errno]);
            exit(errno);
        }
        printf("gethostname='%s'\n", hostname);
    
        if ((service_info=getservbyname(SERVICE_LABEL, SERVICE_PROTOCOL)) == NULL)
        {
            fprintf(stderr, "line %u - getservbyname(%s, %s) - %s\n", __LINE__, SERVICE_LABEL, SERVICE_PROTOCOL, sys_errlist[errno]);
            exit(errno);
        }
        fputc('\n', stdout);
        printf("service_name='%s'\n", service_info->s_name);
        for (i=0; service_info->s_aliases[i] != NULL; i++)
            printf("service_s_alias[%hu]='%s'\n", i, service_info->s_aliases[i]);
        printf("service_port=%u\n", ntohs(service_info->s_port));
        printf("service_protocol='%s'\n", service_info->s_proto);
    
        if ((sk_creat=socket(AF_INET, SOCK_STREAM, 0)) == (-1))
        {
            fprintf(stderr, "line %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
            exit(errno);
        }
        printf("Socket created\n" );
    
        memset(&adr_server, 0, sizeof(struct sockaddr_in));
        adr_server.sin_family=AF_INET;
        adr_server.sin_port=service_info->s_port;
        adr_server.sin_addr.s_addr=INADDR_ANY;
    
        if (bind(sk_creat, &adr_server, sizeof(struct sockaddr_in)) == (-1))
        {
            fprintf(stderr, "line %u - bind() - %s\n", __LINE__, sys_errlist[errno]);
            exit(errno);
        }
        printf("Socket connected to the network\n" );
    
        listen(sk_creat, 1);
     
        fputc('\n', stdout);
        while (1)
        {
            printf("ppid=%u, pid=%u\tAwaiting entry..", getppid(), getpid());
            fflush(stdout);
     
            len_adr=sizeof(struct sockaddr_in);
            if ((sk_dialog=accept(sk_creat, &adr_client, &len_adr)) == (-1))
            {
                fprintf(stderr, "line %u - accept() - %s\n", __LINE__, sys_errlist[errno]);
                exit(errno);
            }
     
            fputs("Entry issued ", stdout);
     
            if ((adr_ascii=inet_ntoa(adr_client.sin_addr)) > (char*)0)
            {
                printf("(adr=%s", adr_ascii);
     
                if ((host_info=gethostbyaddr((char*)&adr_client.sin_addr.s_addr, sizeof(struct in_addr), AF_INET)) != NULL)
                    printf(" - %s)\n", host_info->h_name);
                else
                {
                    fputs("- ???)\n", stdout);
                    fprintf(stderr, "line %u - gethostbyaddr() - %s\n", __LINE__, sys_errlist[errno]);
                }
            }
            else
            {
                fputs("(adr=???)\n", stdout);
                fprintf(stderr, "line %u - inet_ntoa() - %s\n", __LINE__, sys_errlist[errno]);
            }
            
            switch (pid=fork())
            {
                case (-1): 
                    close(sk_creat);
                    close(sk_dialog);
                    fprintf(stderr, "line %u - fork() - %s\n", __LINE__, sys_errlist[errno]); 
                    exit(errno);
     
                case 0: 
                    close(sk_creat);
                    while ((sz_read=read(sk_dialog, buf, SZ_BUF)) > 0)
                    {
                        printf("\n\tppid=%u, pid=%u\tThe server has read '%s'%s\n", getppid(), getpid(), buf, strcmp(buf, "EOT" ) != 0 ?"" :"=> End call" );
    
                        if (strcmp(buf, "EOT" ) == 0)
                            break;
                    }
                    if (sz_read == (-1))
                    {
                        close(sk_dialog);
                        fprintf(stderr, "line %u - read() - %s\n", __LINE__, sys_errlist[errno]);
                        exit(errno);
                    }
    
                    close(sk_dialog);
                    exit(0);
     
                default: 
                    close(sk_dialog);
            }
        }
        close(sk_creat);
    }
    The client

    Code:
    #include <sys/types.h>     
    #include <sys/socket.h>      
    #include <sys/param.h>               
    #include <netinet/in.h>    
    #include <stdio.h>       
    #include <string.h>    
    #include <netdb.h>    
    #include <errno.h>    
     
    extern const char* const sys_errlist[];   
     
    #define SERVER_DEFAULT ("localhost" )  
    #define SERVICE_LABEL ("foo" )   
    #define SERVICE_PROTOCOL ("tcp" )    
    #define SZ_BUF (256) 
     
    main(
        int argc,  
        char *argv[])  
    { 
        ushort i;  
        ushort j;   
     
        int sk_connect;    
        int sk_dialog;   
     
        char buf[SZ_BUF];   
        char hostname[MAXHOSTNAMELEN + 1];    
        char *server;    
        char *pt;          
     
        struct sockaddr_in adr_server;  
        struct hostent *host_info;     
        struct servent *service_info;    
     
        if (argc > 1)
            server=argv[1];
        else
        {
            server=SERVER_DEFAULT;
            printf("No argument for %s - Using %s\n", *argv, SERVER_DEFAULT);
        }
        if (gethostname(hostname, MAXHOSTNAMELEN) != 0)
        {
            fprintf(stderr, "line %u - gethostname(%s) - %s\n", __LINE__, hostname, sys_errlist[errno]);
            exit(errno);
        }
        printf("gethostname='%s'\n", hostname);
    
        if ((host_info=gethostbyname(server)) == NULL)
        {
            fprintf(stderr, "line %u - gethostbyname(%s) - %s\n", __LINE__, server, sys_errlist[errno]);
            exit(errno);
        }
        fputc('\n', stdout);
        printf("host_info.h_name='%s'\n", host_info->h_name);
        for (i=0; host_info->h_aliases[i] != NULL; i++)
            printf("host_info.h_alias[%hu]='%s'\n", i, host_info->h_aliases[i]);
        printf("host_info.h_addrtype=%u\n", host_info->h_addrtype);
        printf("host_info.h_length=%u\n", host_info->h_length);
        for (i=0; host_info->h_addr_list[i] != NULL; i++)
        {
            printf("host_info.h_addr_list[%hu]=", i);
            for (j=0; j < host_info->h_length; j++)
                printf("%hu ", (unsigned char)host_info->h_addr_list[i][j]);
            fputc('\n', stdout);
        }
     
        if ((service_info=getservbyname(SERVICE_LABEL, SERVICE_PROTOCOL)) ==NULL)
        {
            fprintf(stderr, "line %u - getservbyname(%s, %s) - %s\n", __LINE__, SERVICE_LABEL, SERVICE_PROTOCOL, sys_errlist[errno]);
            exit(errno);
        }
        fputc('\n', stdout);
        printf("service_name='%s'\n", service_info->s_name);
        for (i=0; service_info->s_aliases[i] != NULL; i++)
            printf("service_s_alias[%hu]='%s'\n", i, service_info->s_aliases[i]);
        printf("service_port=%u\n", ntohs(service_info->s_port));
        printf("service_protocol='%s'\n", service_info->s_proto);
    
        memset(&adr_server, 0, sizeof(struct sockaddr_in));
        adr_server.sin_len=host_info->h_length;
        adr_server.sin_family=AF_INET;
        adr_server.sin_port=service_info->s_port;
        memcpy(&adr_server.sin_addr.s_addr, host_info->h_addr, host_info->h_length);
    
        fputc('\n', stdout);
        do {
            if ((sk_dialog=socket(AF_INET, SOCK_STREAM, 0)) == (-1))
            {
                fprintf(stderr, "line %u - socket() - %s\n", __LINE__, sys_errlist[errno]);
                exit(errno);
            }
    
            if ((sk_connect=connect(sk_dialog, &adr_server, sizeof(struct sockaddr_in))) == (-1))
            {
                fprintf(stderr, "line %u - connect() - %s\n", __LINE__, sys_errlist[errno]);
                sleep(5);
            }
        } while (sk_connect == (-1));
        printf("Connection successful\n" );
        do {
            fputs("Enter string (EOT finally) :", stdout); fflush(stdout);
            fflush(stdin); fgets(buf, SZ_BUF, stdin);
            if ((pt=strchr(buf, '\n')) != NULL)
                *pt='\0';
            if (write(sk_dialog, buf, strlen(buf) + 1) == (-1))
                fprintf(stderr, "line %u - write(%s) - %s\n", __LINE__, buf, sys_errlist[errno]);
        } while (strcmp(buf, "EOT" ) != 0);
        close(sk_dialog);
        exit(0);
    }

  3. #3
    Join Date
    Jun 2009
    Posts
    50

    Re: Linux Socket Programming

    Superb! This is what I needed to start my project. So I chose the socket server/client TCP. But when I compile I get errors. I corrected by adding an include in stdlib.h but still a lot of errors in the hand:

    "server.c: In function 'main':
    server.c: 82: warning: passing argument 2 of 'bind' from incompatible pointer type
    server.c: 101: warning: passing argument 2 of 'accept' from incompatible pointer type
    server.c: 120:34 : warning: trigraph?) ignored, use-trigraphs to enable
    server.c: 126:33: warning: trigraph?) ignored, use-trigraphs to enable
    / tmp / ccuGFSZS.o: In function `main ':
    server.c.text 0 x5e): warning: `sys_errlist 'is deprecated, use` strerror' or `strerror_r 'instead"

    And for the client, I have the same type of error:
    "Client.cs: In function 'main':
    Client.cs: 52: warning: incompatible implicit declaration of built-in function 'exit'
    Client.cs: 60: warning: incompatible implicit declaration of built-in function 'exit'
    Client.cs: 80: warning: incompatible implicit declaration of built-in function 'exit'
    Client.cs: 91: error: 'struct sockaddr' has no member named 'sin_len'
    Client.cs: 103: warning: incompatible implicit declaration of built-in function 'exit'
    Client.cs: 107: warning: passing argument 2 of 'connect' from incompatible pointer type
    Client.cs: 132: warning: incompatible implicit declaration of built-in function 'exit'

    Can someone help me?

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Linux Socket Programming

    1) according to the exit, you must include stdlib.h
    2) sys_errlist became impaired. Must replace sys_errlist [errno] by strerror (errno)
    3) sin_len disappeared from the structure sockaddr_in => no worries, wherever I am referring you delete it

    When the errors on accept and bind, because these functions are functions that accept input pointers to simple "struct sockaddr". This approach allows to use the bind and accept with unix socket (using a struct sockaddr_un) or inet socket (using a struct sockaddr_in). So at the time, the compiler said nothing. Today, it is more boring then need to explain that you know what you do when your name and bind () and accept (), casts the pointer you passed in "struct sockaddr *"

    The error of trigraphs, I can not see what it is.

Similar Threads

  1. Replies: 6
    Last Post: 22-07-2011, 10:24 PM
  2. Help Understanding Linux Programming
    By Jamna in forum Operating Systems
    Replies: 5
    Last Post: 28-05-2011, 10:43 AM
  3. Replies: 3
    Last Post: 10-01-2011, 06:49 AM
  4. Socket programming: Is any new Programming Language?
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 11:13 AM
  5. Linux or Windows Host for these multiple programming languages
    By Rock Villa in forum Software Development
    Replies: 3
    Last Post: 16-03-2009, 05:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,936,455.20090 seconds with 17 queries