简体   繁体   English

一次打印从一个套接字接收到的字符

[英]Print char received from socket one at a time

I am working on a program and I have to send one char at a time through a socket. 我正在开发一个程序,必须一次通过套接字发送一个字符。 The connection work fine and the chars are sending but when I have to print them to stdout, I cant manage to print them without a newline. 连接工作正常,字符正在发送,但是当我必须将它们打印到标准输出时,我无法在没有换行符的情况下进行打印。

for ( ; ; ) {
            nb = select(connfd+1, &read_set, NULL, NULL, NULL);
            if (nb<=0) {
                printf("Error\n");
            }else{
                if (FD_ISSET(connfd, &read_set)) {
                    char buff[2];
                    nb = read(connfd, buff, 4096);
                    if (nb < 0){
                        printf("The client disconected\n");
                        break;
                    }
                    printf("%s\n",buff); // this prints them with a new line between each char.Removing the \n will make it work only by hitting enter
                    //fputs(buff,stdout); //does the same as printf without \n
                }

One more mention: the client send chars without waiting for ENTER from stdin. 再说一遍:客户端发送字符而不必等待stdin的ENTER。

Any hints? 有什么提示吗? Thanks 谢谢

1) Don't lie to read - it tends to nearly always lead to BAD things: 1)不要撒谎来read -它往往几乎总是导致糟糕的事情:

                char buff[2];
                nb = read(connfd, buff, 4096);

This should be: char buff[2]; 应该是:char buff [2]; nb = read(connfd, buff, 1); nb = read(connfd,buff,1);

2) You need to terminate the string: 2)您需要终止字符串:

buff[1] = 0; 

2a) printf("%s", buff) will not actually display anything, because there is no newline to force the buffered data to be actually written to the screen - use fflush(stdout); 2a) printf("%s", buff)实际上不会显示任何内容,因为没有换行符可以强制将缓冲的数据实际写入屏幕-使用fflush(stdout); to force it. 强迫它。

3) Reading characters without waiting for "enter" is not possible in standard C++ (or C). 3)在标准C ++(或C)中,不等待“输入”而无需读取字符是不可能的。 May I suggest you look at the ncurses package of functions? 我可以建议您看看ncurses的函数包吗?

                printf("%s\n",buff);

The %s format specifier is only for C-style strings. %s格式说明符适用于C样式的字符串。 Since buff is not a C-style string, this is wrong. 由于buff不是C风格的字符串,所以这是错误的。 Also: 也:

                char buff[2];
                nb = read(connfd, buff, 4096);

How can you read up to 4096 characters into a buffer that's only big enough for 2? 如何将最多4096个字符读入一个仅够2个字符的缓冲区? You probably want: 您可能想要:

                char buff[4096];
                nb = read(connfd, buff, 4096);
                if (nb < 0){
                    printf("The client disconected\n");
                    break;
                }
                for (int i = 0; i < nb; ++i)
                   putchar(buff[i]);
                fflush(stdout);

You need to flush the buffer. 您需要刷新缓冲区。 After your printf call, also call fflush(stdout) . 在调用printf之后,还请调用fflush(stdout)

Also, since you are trying to print 1 character at a time, you have a few other problems: 另外,由于您尝试一次打印1个字符,因此还有其他一些问题:

char buff; // you wanted to read 1 character at a time
nb = read(connfd, &buff, 1); 
if (nb < 1)
{
    printf("The client disconected\n");
    break;
}
printf("%c", buff);
fflush(stdout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM