Thread: Send integers to server and server can read them?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    4

    Send integers to server and server can read them?

    Hi everyone!

    could you please tell me what methods to use in order the clients be able to
    send integers through socket to server and server can read them and display it on the screen ?

    Thanks!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by rahul.hbk007 View Post
    Hi everyone!

    could you please tell me what methods to use in order the clients be able to
    send integers through socket to server and server can read them and display it on the screen ?

    Thanks!
    1) There is no such thing as a "method" in C. Methods belong to OOP and C is not a OO language.

    2) For socket programming check out :
    Beej's Guide to Network Programming
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4
    Code:
    /* Client Code - Client.c */
    -----------------------------------
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h> 
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
        int x;
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
    
        printf("Please enter the index: ");
        scanf("%d",&x);
        int tmp = htonl(x);
        n=write(sockfd, &tmp, 4);
    
        //n=write(sockfd, &x, sizeof(x));
        if (n < 0) 
             error("ERROR writing to socket");
    	return 0;
    }
    
    
    
    
    
    /* Server Code - Server.c */
    -----------------------------------------
    
    #include <stdio.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(1);
    }
    
    int main(int argc, char *argv[])
    {
         int sockfd, newsockfd, portno, clilen;
         int y;
         struct sockaddr_in serv_addr, cli_addr;
         int n;
         if (argc < 2) {
             fprintf(stderr,"ERROR, no port provided\n");
             exit(1);
         }
         sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if (sockfd < 0) 
            error("ERROR opening socket");
         portno = atoi(argv[1]);
         serv_addr.sin_family = AF_INET;
         serv_addr.sin_addr.s_addr = INADDR_ANY;
         serv_addr.sin_port = htons(portno);
         if (bind(sockfd, (struct sockaddr *) &serv_addr,
                  sizeof(serv_addr)) < 0) 
                  error("ERROR on binding");
         listen(sockfd,5);
         clilen = sizeof(cli_addr);
         newsockfd = accept(sockfd, 
                     (struct sockaddr *) &cli_addr, 
                     &clilen);
         if (newsockfd < 0) 
              error("ERROR on accept");
        // n = read(newsockfd,&y,sizeof(y));
    if (n < 0) error("ERROR reading from socket");
    int ii=ntohl(y);
         printf("Here is the message: %d\n",ii);
     return 0; 
    }
    
    
    
    
    
    /*
     There is some problem with passing the integer via the socket. The server is working but it is not displaying the integer value send via socket by the client. 
    
    Please help me and if possible rectify the code .. :) 
    
    */

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You've got the right idea. You don't have to worry about endianess for the int you send if both the server and the client are on little endian machines, which probably they are (it's just the networking layer that's big endian).

    Also, you may want to use a type with a set, definite size, such as int32_t, which is guarranteed to be 4 bytes everywhere.
    Last edited by MK27; 05-01-2010 at 04:40 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4
    Code:
    /* Server Code - Server.c */
    -----------------------------------------
    
    #include <stdio.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(1);
    }
    
    int main(int argc, char *argv[])
    {
         int sockfd, newsockfd, portno, clilen;
         int y;
         struct sockaddr_in serv_addr, cli_addr;
         int n;
         if (argc < 2) {
             fprintf(stderr,"ERROR, no port provided\n");
             exit(1);
         }
         sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if (sockfd < 0) 
            error("ERROR opening socket");
         portno = atoi(argv[1]);
         serv_addr.sin_family = AF_INET;
         serv_addr.sin_addr.s_addr = INADDR_ANY;
         serv_addr.sin_port = htons(portno);
         if (bind(sockfd, (struct sockaddr *) &serv_addr,
                  sizeof(serv_addr)) < 0) 
                  error("ERROR on binding");
         listen(sockfd,5);
         clilen = sizeof(cli_addr);
         newsockfd = accept(sockfd, 
                     (struct sockaddr *) &cli_addr, 
                     &clilen);
         if (newsockfd < 0) 
              error("ERROR on accept");
         n = read(newsockfd,&y,sizeof(y));
    if (n < 0) error("ERROR reading from socket");
    int ii=ntohl(y);
         printf("Here is the message: %d\n",ii);
     return 0; 
    }
    I have modified the server code but still it has not been able to read the integer via the socket. What changes are required in my code ? Do I need to make any changes in the client code also ? Kindly advice !!
    Last edited by Salem; 05-02-2010 at 09:39 AM. Reason: Moved question outside code tags so Mk27 can see it ;)

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4
    Please can anyone help me ? Its urgent !! []

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rahul.hbk007 View Post
    Please can anyone help me ? Its urgent !! []
    Then next time try asking a question.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your client would seem to be missing a call to connect().
    See Beej, he explains all.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  3. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  4. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM