Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C recv函数行为_C_Sockets_Recv_Recvfrom - Fatal编程技术网

C recv函数行为

C recv函数行为,c,sockets,recv,recvfrom,C,Sockets,Recv,Recvfrom,这是我的两段代码: 服务器.c #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <sys/timeb.h> #include <string.h> #include <u

这是我的两段代码:

服务器.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <string.h>
#include <unistd.h>

#define BYTES_NR 64
#define MSG_NR 512

int main(int argc, char *argv[]) {
    char buf[BYTES_NR];
    int sock,length;
    struct sockaddr_in server,client;
    int rval,i;


    if(argc !=2) {
        fprintf(stderr,"Usage: %s port\n",argv[0]);
        exit(-1);
    }

    sock = socket(AF_INET,SOCK_DGRAM,0);
    if(sock<0) {
        perror("opening stream socket");
        exit(1);
    }

    server.sin_family = AF_INET;
    server.sin_addr.s_addr= INADDR_ANY;
    server.sin_port = htons(atoi(argv[1]));
    if (bind(sock,(struct sockaddr *)&server,sizeof(server))<0) {
        perror("binding stream socket");
        exit(1);
    }

    length = sizeof(server);
    if(getsockname(sock,(struct sockaddr *)&server, (socklen_t *)&length)<0){
        perror("getting socket name");
        exit(1);
    }


    printf("Socket port #%d\n",ntohs(server.sin_port));
        printf("test");
    while(1) {
        do {
        printf("test2");
            bzero(buf,sizeof(buf));
            rval = recvfrom(sock,buf,sizeof(buf), 0, (struct sockaddr *)&client, (socklen_t *)&length );

            if(rval<0)
                perror("reading stream message");
            i=0;
            if(rval==0)
                printf("Ending connection\n");
            else {
                printf("Message received: sending back\n");
                strcat(buf,"*");
                if (sendto(sock,buf,sizeof(buf),0,(struct sockaddr *)&client,sizeof(client))<0)
                    perror("writing on stream socket");
            }
        } while(rval !=0);
    }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义字节\u NR 64
#定义MSG_NR 512
int main(int argc,char*argv[]){
char buf[字节数];
内袜子,长度;
服务器、客户端中的结构sockaddr_;
int-rval,i;
如果(argc!=2){
fprintf(标准指令,“用法:%s端口\n”,argv[0]);
出口(-1);
}
sock=插座(AF_INET,sock DGRAM,0);

如果(sock这与
recv
无关。更改为:

printf("test\n");
默认情况下,标准输出是行缓冲的,所以在打印换行符之前,您不会看到任何内容

如果不想打印换行符,可以在每次打印后使用
fflush(stdout);
打印当前缓冲区。还可以使用:

setvbuf(stdout, NULL, _IONBF, BUFSIZ);

禁用输出缓冲。

strcat(buf,“*”);
您假设buf是以null结尾的,并且足够大。它不是和/或不是。它只是一个测试代码,但是我唯一的怀疑是关于行缓冲区stdout!我不在乎它是什么。它是错误的。
bzero(buf,sizeof(buf));
触发了我的感觉。还有
如果(rvalBTW'#define BYTES_NR 64#define MSG_NR 512`和
char MSG[MSG_NR][BYTES_NR];
(和其他)可能太大,无法自动存储。(“堆栈”)
setvbuf(stdout, NULL, _IONBF, BUFSIZ);