Thread: help me please beginnner socket programming

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    Unhappy help me please beginnner socket programming

    I give Up

    and you R my last chance to not kill my self
    I am new at socket programming and it should named stupit
    because i search and search and what i found just coding and coding and coding

    but the question where
    where
    where
    where
    to put that code

    i have run the fedora and open a terminal
    and try to put any code
    that fedora run from virtual machine
    but no result
    what to do

    i am trying to make a client/server code
    Code:
    #include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#define SERVER_PORT 5432#define MAX_LINE 256intmain(int argc, char * argv[]){FILE *fp;struct hostent *hp;struct sockaddr_in sin;char *host;char buf[MAX_LINE];int s;int len;if (argc==2) {host = argv[1];}else {fprintf(stderr, "usage: simplex-talk host\n");exit(1);}/* translate host name into peer’s IP address */hp = gethostbyname(host);if (!hp) {fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
    exit(1);
    }/* build address data structure */bzero((char *)&sin, sizeof(sin));sin.sin_family = AF_INET;bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);sin.sin_port = htons(SERVER_PORT);/* active open */if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {perror("simplex-talk: socket");exit(1);}if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {perror("simplex-talk: connect");close(s);exit(1);}/* main loop: get and send lines of text */while (fgets(buf, sizeof(buf), stdin)) {buf[MAX_LINE-1] = ’\0’;len = strlen(buf) + 1;send(s, buf, len, 0);}
    }
    this one from client
    where to put this code
    dont tell me that i need microsoft visual studio
    because i dont have it

    but if i run it from there how the communication will happen between microsoft visual studio
    and fedora linux which is run virtually

    oh gosh
    i really got tired
    from searching for a week and no no no no result
    i hope that some one can help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you could make a much better job of pasting your code.

    Edit your post (you have about 30 minutes left), and make sure you do "copy as text" when you copy from VS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Dear Salem

    dont care about the code
    can u help me with the strategy
    what do i need to make a client/server socket

    they told me that i need an operating system
    but i get confused
    because all written in C++ or any other once
    so how to applied that
    writing the code using microsoft visual
    and run that code
    from fedora or any os??

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #define SERVER_PORT 5432
    #define MAX_LINE 256
    int
    main(int argc, char * argv[])
    {
    FILE *fp;
    struct hostent *hp;
    struct sockaddr_in sin;
    char *host;
    char buf[MAX_LINE];
    int s;
    int len;
    if (argc==2) {
    host = argv[1];
    }
    else {
    fprintf(stderr, "usage: simplex-talk host\n");
    exit(1);
    }
    /* translate host name into peer’s IP address */
    hp = gethostbyname(host);
    if (!hp) {
    fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
    exit(1);
    }
    /* build address data structure */
    bzero((char *)&sin, sizeof(sin));
    sin.sin_family = AF_INET;
    bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
    sin.sin_port = htons(SERVER_PORT);
    /* active open */
    if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("simplex-talk: socket");
    exit(1);
    }
    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("simplex-talk: connect");
    close(s);
    exit(1);
    }
    /* main loop: get and send lines of text */
    while (fgets(buf, sizeof(buf), stdin)) {
    buf[MAX_LINE-1] = ’\0’;
    len = strlen(buf) + 1;
    send(s, buf, len, 0);
    }
    }
    i donno why the code look strange

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    ???
    see my question is that i need to write a socket programming using fedora (linux) can i???

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the usual advice is to go and read beej, then try his simple client/server for yourself.
    Beej's Guide to Network Programming
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    thank you very much salem
    that gave me the sentences that i was looking for
    but there is aquistion
    now he said that he made all included examples on linux
    but what exist are for c
    does that mean i am on the wrong place if i want to make a socket programming using linux
    and if yes from where i can get a resoursfull site like this one
    that have examples with discription???
    and question 2
    is it more difficult than using c or same level??

    thank you very much
    regards
    simona

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First lets clean up that code...
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    #define SERVER_PORT 5432
    #define MAX_LINE 256
    
    int main(int argc, char * argv[])
      {
        FILE *fp;
        struct hostent *hp;
        struct sockaddr_in sin;
        char *host;
        char buf[MAX_LINE];
        int s;
        int len;
    
        if (argc==2) 
          {
             host = argv[1];
          }
        else 
          {
            fprintf(stderr, "usage: simplex-talk host\n");
            exit(1);
          }
    
        /* translate host name into peer’s IP address */
        hp = gethostbyname(host);
        if (!hp) 
         {
            fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
            exit(1);
         }
    
        /* build address data structure */
        bzero( (char *) &sin, sizeof(sin) );
        sin.sin_family = AF_INET;
        bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
        sin.sin_port = htons(SERVER_PORT);
    
        /* active open */
        if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) 
         {
           perror("simplex-talk: socket");
           exit(1);
         }
    
        if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) 
         {
           perror("simplex-talk: connect");
           close(s);
           exit(1);
         }
    
        /* main loop: get and send lines of text */
        while (fgets(buf, sizeof(buf), stdin)) 
          {
            buf[MAX_LINE-1] = ’\0’;
            len = strlen(buf) + 1;
            send(s, buf, len, 0);
          }
        return 0;
    }
    You seem not to appreciate the difference between Linux (an operating system) and C (a programming language). C runs on Linux, and is used to create programs that also run on Linux; just like the one above. Yes socket programming is entirely possible both in C and for Linux... in fact it's done all the time.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Step 1 - can you browse the web from within the virtual machine?

    Step 2 - can you compile and run this in your virtual machine.
    Code:
    #include <stdio.h>
    int main ( ) {
      printf("Hello world\n");
      return 0;
    }
    Compile and run
    Code:
    $ gcc bar.c
    $ ./a.out 
    Hello world
    Step 3 - where do you want the client/server to run (L=Linux, W=Windows)?
    Client(W) <-> Server(W)
    Client(W) <-> Server(L)
    Client(L) <-> Server(W)
    Client(L) <-> Server(L)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Taher Execuse .. do you mean that i have to use c in linux ??
    i don't have microsoft visual studio ,, but what i have is to install MVS in fedora and then use the MVS directly
    No other way because i don't have MVS

    Salem i want Client(L) <-> Server(L) and execuse me you want me to put the first code in MVS (which i dont have) and the second on in fedora terminal???

    if it is possible can any body gave me MVS i thought that fedora include the c language i wasnt knowing its importance.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why do you keep talking about MVS when you want to program on Linux?

    The default compiler for Linux based systems is GCC.
    Use Yum to get gcc (if it isn't already installed).

    > you want me to put the first code in MVS (which i dont have) and the second on in fedora terminal???
    The "hello world" program is something to prove you have the compiler and libc installed correctly.
    Open a text editor, paste in the code, save to a filename, and execute those commands in a terminal.

    TBH, if you're not already up to speed on basic editing and compiling, then writing some client/server code is going to be a lot of effort.


    If you really want to program on windows, there's nothing stopping you getting visual studio express.
    Free Developer Tools - Visual Studio 2010 Express | Microsoft Visual Studio
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by simona View Post
    Taher Execuse .. do you mean that i have to use c in linux ??
    i don't have microsoft visual studio ,, but what i have is to install MVS in fedora and then use the MVS directly
    No other way because i don't have MVS
    First of all you are not going to write a program using a windows compiler that is going to run on Linux.

    For windows you need a windows C compiler... MSVC or Pelles are good choices
    For linux you need a linux C compiler ... GCC is probably your best bet.

    If you want both windows and linux versions... you have to compile your code once on each machine.

    thought that fedora include the c language i wasnt knowing its importance.
    It probably does. Many Linux distros include a C and/or C++ compiler. Ask on the Fedora Support Forums.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with socket programming
    By moroccanplaya in forum C Programming
    Replies: 6
    Last Post: 03-30-2011, 04:18 PM
  2. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  3. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  4. C++ total beginnner needs some advice
    By n3XusSLO in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2006, 01:43 AM
  5. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM