Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

client/server socket programming

  • 29-01-2002 02:32PM
    #1
    Closed Accounts Posts: 8,478 ✭✭✭


    "Create a server, which will sent a selected file to a connected client.
    The server must have access to three different files and the client will
    Indicate whether it wants to download a specific file, 2 specific files or all three.
    The downloaded file should be displayed on the client’s terminal until the user has read them."

    so i kinda remember some programming from my waterford IT says but i need some kinda refreasher :)

    any decent links/resources out there ?


Comments

  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    http://httpd.apache.org/

    Sorry, couldn't resist it. I'm weak. Sure who'd know anyway? :)

    adam


  • Registered Users, Registered Users 2 Posts: 6,265 ✭✭✭MiCr0


    c sockets are the way to go and very easy too
    quick google should give all the info u need

    http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html

    [me scans memory back to 2nd year in college]


  • Closed Accounts Posts: 5,563 ✭✭✭Typedef


    #include <stdio.h>
    #include <Stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <net/inet.h>

    #define error(x) printf("Looser Error %s\n",x); exit(0);

    int parse_command(int sock,char*command);

    int main(int argc,char**argv)
    {
    struct sockaddr_in sin;
    struct sockaddr_in linux;
    int skfd,skfd2,a;

    char command_buf[4096];
    char hello[]="Hello there you have connected to the fileserver\nThree Files are the maximum you can attempt to get\nPlease enter the filenames(onlY) incrementally";

    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_family=AF_INET;
    sin.sin_port=htons(6666);
    skfd=socket(AF_INET,SOCK_STREAM,0);

    bind(skfd,(struct sockaddr*)&sin,sizeof(sin));
    listen(skfd,5);
    a=sizeof(linux);

    skfd2=accept(skfd,(struct sockaddr*)&linux,&a);
    printf("Accepted a connection\n");l

    send(skfd2,hello,sizeof(hello),0);
    while(1)
    {
    recv(skfd2,command_buf,sizeof(command_buf),0);
    if(parse_command(skfd2,command_buf)==-1)
    break;
    };

    close(skfd);
    close(skfd2);
    return 0;
    };

    int parse_command(int sock,char*command)
    {
    FILE*lex;
    static int attempts=0;
    char stream[1];

    if(attempts>=3)
    {printf("All attempts are over\n");
    return -1;};

    lex=fopen(command,"r");
    if(lex==NULL)
    {printf("File does not exist sorry\n");
    return 0;};

    attempts++;
    while(1)
    {
    fread(stream,1,1,lex);
    send(sock,stream,sizeof(stream),0);
    if(feof(lex)!=0)
    break;
    };
    fclose(lex);
    return 0;
    };

    Something like this?


  • Registered Users, Registered Users 2 Posts: 3,282 ✭✭✭regi




  • Registered Users, Registered Users 2 Posts: 897 ✭✭✭Greenbean


    Definitely Beej's guide - tis all you need.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,265 ✭✭✭MiCr0


    any one who did comp sci in ucd knows beej's page :)


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    If you're going to run it off Windows you may want to take a look at the Winsock API.


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    no just thru unix

    TypeDef, thats WAY too complicated for my little brain, ill check back to it in 2 years when im doin my degree :)

    thanks anyway lads


Advertisement