Home:ALL Converter>C Socket program doesn't print desired output (No Error)

C Socket program doesn't print desired output (No Error)

Ask Time:2018-06-08T21:53:44         Author:Abhishek

Json Formatter

I can't seem to print what I enter in my client. I have compiled both without any error. Can't figure out why it isn't printing the desired output.

Q. What is wrong with my code ? How can I print a string with whitespaces through the client in the server.

Here is the code for the server.

/* Server Side program */


    int sid,nid;
    struct sockaddr_in q;
    char x[100];

    int len=sizeof(struct sockaddr_in);

    sid=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    q.sin_family=PF_INET;
    q.sin_port=1600;
    q.sin_addr.s_addr=0;

    bind(sid,&q,len);

    listen(sid,20);

    while(1)
    {
            memset(x,0,sizeof(x));
            nid=accept(sid,&q,&len);
            read(nid,x,100);
            printf("%s\n",x);

    }

Here is the code for the client.

/*client side program*/


    int sid,status;
    struct sockaddr_in q;
    int len=sizeof(struct sockaddr_in);
    char x[100];

    sid=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
            q.sin_family=PF_INET;                   
            q.sin_port=1600;                        
            q.sin_addr.s_addr=0;                  
    status=connect(sid,&q,len);
    if(status==-1)
    {
            printf("Connection failure");
            exit(0);
    }

    while(1)
    {
            printf("Enter string to send : ");
            scanf("%[^\n]s",x);
            write(sid,x,strlen(x));
            if(strcmp(x,"bye")==0)
                     break;

    }

Author:Abhishek,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762216/c-socket-program-doesnt-print-desired-output-no-error
yy