Thread: Assignig unint-8 to char buffer[1024] ???

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    20

    Assignig unint-8 to char buffer[1024] ???

    Hi...
    i am working on socket programming in c

    so i define

    Code:
    int listenfd,connfd,portno,stringlen;
        socklen_t clilen;
        char buffer[1024];
        char flgBuf[8];
        uint8_t state;
        struct sockaddr_in servaddr;
        int n;
        int sockfd,fd ; //for client socket
        
        char fileName[128];
        
        
        int CmpValue;
        
    
    
    static void    err_doit(int, int, const char *, va_list);
    void str_fun(int Connfd);
    void err_msg(const char *fmt, ...); /* to use this prototype in str_fun */
    
    
    void str_fun(int Connfd)
    {
        
        
        
        if(connfd<0)
            err_msg("Error,on Accept\n");
        
            printf("waiting to provide file From the client IP:%s and port:%d\n\n",inet_ntoa(servaddr.sin_addr),ntohs(servaddr.sin_port )  );
        
            strncpy(fileName,"hello",128);
            printf("fileName is : %s \n",fileName);            
        for(;;)
        {
            
            
            //bzero(&buffer,sizeof(buffer));
            //fd= open("abc.txt",O_RDONLY);
            n=read(connfd,&buffer,sizeof(buffer));
            printf("Now In Reading....\n");
            printf("you want to download File : %s ",buffer);
            
            CmpValue=strncmp(buffer,fileName,5);
        
            if(CmpValue==0)
                {
                    printf("File exist in server.\n");
                    state=1;
                    
                    n=write(connfd,&buffer,sizeof(buffer));
                }
            else
                {
                    printf("File not exist Honey.\n");
                    state=0;
                    printf("the state : %d \n",state);
                }
    so i want to assign the state value into char buffer without error or warning ,how to accomplish that ?????

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by zmhnak View Post
    so i want to assign the state value into char buffer without error or warning ,how to accomplish that ?????
    ????
    Please show what you tried that gave you warnings or errors.
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    20

    Question

    when this code excute it does not have any error

    Code:
    printf("File exist in server.\n");
                    state=1;
                    bzero(&buffer,sizeof(buffer));
                    buffer[0]=state;
    but it prints garbage value and the code of CLient is


    Code:
    int main(int argc, char *argv[])
    {
    
        // to see if the port is provided by user or not
        if(argc<3)
            err_quit("usage %s hostname port\n",argv[0]);
        
        // to open socket descriptor
        sockfd = Socket(AF_INET, SOCK_STREAM, 0);
        
        // to convert the port# from ascii to int to deal with it as int in address structure    
        portno=atoi(argv[2]);
    
    
        // to zero the structre adress and fill it with ip address and port#
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port   = htons(portno);    
        
        Inet_pton(AF_INET, argv[1], &servaddr.sin_addr) ;
            
        // to make connect socket
        Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) ;
    //---------------------------------------------------------------------------------------------------
            
        for(;;)
        {
            //bzero(&buffer,sizeof(buffer));
            printf("Please Enter the file name you want to download : \n");
            fgets(buffer,sizeof(buffer),stdin);
            printf("\n");
            n=write(sockfd,&buffer,sizeof(buffer));  // Data request from server that send string to server    
            
            n=read(sockfd,&buffer,sizeof(buffer));           //Data replay from server that the client read the length  and so on..
            sprintf(buffer,"the state : %d \n",state);
            if(n<0)
                err_msg("ERROR Reading FROM SOCKET");
          }
    close (sockfd);
    return 0;

    and this expression in client code print the garbage sprintf(buffer,"the state : %d \n",state);

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        for(;;)
        {
            //bzero(&buffer,sizeof(buffer)); // no & here
            printf("Please Enter the file name you want to download : \n");
            fgets(buffer,sizeof(buffer),stdin);
            printf("\n");
            n=write(sockfd,&buffer,sizeof(buffer));  // no & here
            
            n=read(sockfd,&buffer,sizeof(buffer));  // no & here
    Kurt

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    20
    u mean by no & here :the error is here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignig string to array and compare it....???
    By zmhnak in forum C Programming
    Replies: 2
    Last Post: 03-20-2013, 04:27 PM
  2. Char[1024] to char
    By musicman in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2011, 06:03 AM
  3. Question abt read(fd, buffer, 1024)
    By tesla in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-12-2009, 03:52 AM
  4. declaring chars: char file[<nonconstant>] [1024];
    By jverkoey in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2003, 01:47 AM
  5. char[1024] to c++ STL string
    By dkt in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2002, 08:13 AM

Tags for this Thread