无法打印客户端使用C中的套接字接收的代码

无法打印客户端使用C中的套接字接收的代码,c,sockets,server,client,buffer,C,Sockets,Server,Client,Buffer,我不知道为什么我不能打印保存在客户缓冲区中的信息。首先,我从服务器传输到客户端的代码是google主页的源代码。我将这段代码保存在一个html文件中,整个文件的大小是198357字节。 为此,我使用了1024字节的缓冲区,并在循环中传递了所有数据 我一直在使用prints检查服务器传递的字节数和客户端接收的字节数,两者都等于文件的字节数,因此传输做得很好。 但当我试图打印保存在缓冲区中的数据时,它开始给我带来问题 server.c #include<sys/socket.h> #in

我不知道为什么我不能打印保存在客户缓冲区中的信息。首先,我从服务器传输到客户端的代码是google主页的源代码。我将这段代码保存在一个html文件中,整个文件的大小是198357字节。 为此,我使用了1024字节的缓冲区,并在循环中传递了所有数据

我一直在使用prints检查服务器传递的字节数和客户端接收的字节数,两者都等于文件的字节数,因此传输做得很好。 但当我试图打印保存在缓冲区中的数据时,它开始给我带来问题

server.c

#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
    struct sockaddr_in serverAddress;
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr=INADDR_ANY;
    serverAddress.sin_port = htons(9999);


    int server = socket(AF_INET,SOCK_STREAM,0);
    if(server == -1)
    {
        perror("Cannot open the socket");
        exit(1);
    }

    int active = 1;
    setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &active, sizeof(active));
    if(bind(server, (void*)&serverAddress, sizeof(serverAddress)) !=0) 
    {
        perror("Fail bind");
        exit(1);    
    }
    else
    {
        printf("success\n");
    }


    printf("I'm listening\n");

    listen(server, SOMAXCONN); 

//----------------------------------------------------

    while(1)
    {
        struct sockaddr_in clientAddress;
        unsigned int addressSize;
        int client = accept(server, (struct sockaddr*)&clientAddress, &addressSize);

        int child = fork();
        if(child==0) //in case we stablished a connection we have to create a child and then transfer the file inside of it
        {
            int file = open("google.html",O_RDONLY); //open file

            if(file <0)
            {
                perror("Empty file!\n");
                return 1;
            }

            while(1) //Iterate until function read cannot read more bytes
            {
                unsigned char buffer[1024]={0};
                int read_bytes = read(file,buffer,sizeof(buffer));

                if(read_bytes==0)
                {
                    printf("Transfer!\n");
                    break;
                }
                while(read_bytes>0) //Write on the client the reading bytes
                {
                    int written_bytes;
                    written_bytes = write(client, buffer, read_bytes);
                    //printf("%d\n",written_bytes); //print to check how many bytes are passed to the client (1024)


                    read_bytes -= written_bytes; //breaks loop
                }               
            } 
        }

        close(server); 
        shutdown(server, SHUT_RDWR);
        sleep(1);
        break;

    }
    return 0;
}
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>


int main(int argc, char* argv)
{
    if(argc ==2) //you've got to received as an argument the IP of the server, i haven't implemented it yet!
    {

        int client = socket(AF_INET,SOCK_STREAM,0);
        char recvBuffer[1024];
        int n=0, s;
        if(client <0)
        {
            printf("Cannot open the socket\n");
            exit(1);
        }

        struct sockaddr_in serverAddress;
        serverAddress.sin_family = AF_INET;
        serverAddress.sin_addr.s_addr=INADDR_ANY;
        serverAddress.sin_port = htons(9999);


        if(connect(client, (void*)&serverAddress, sizeof(serverAddress)) !=0)
        {
            perror("Cannot be connected");
            return 1;
        }

        printf("Connecting!");
        char buffer2[1024];
        while((n=read(client, recvBuffer,sizeof(recvBuffer)-1))>0)
        {
            //printf("%d\n",n); //i used to check the num of bytes passed by this and everything it's correct
            recvBuffer[n] =0; 
            printf("%s",recvBuffer); //error, the transferation goes wrong here
        }


        if(n==0)
        {
            shutdown(client, SHUT_RD);
            close(client);  
        }
        else if(n<0)
        {
              perror("Read error\n");
              return 1;  
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
serverAddress中的结构sockaddr_;
serverAddress.sin_family=AF_INET;
serverAddress.sin\u addr.s\u addr=INADDR\u ANY;
serverAddress.sinu port=htons(9999);
int server=socket(AF_INET,SOCK_STREAM,0);
如果(服务器==-1)
{
perror(“无法打开插座”);
出口(1);
}
int-active=1;
setsockopt(服务器、SOL_套接字、SO_REUSEADDR、&active、sizeof(active));
if(bind(server,(void*)&serverAddress,sizeof(serverAddress))!=0)
{
perror(“失败绑定”);
出口(1);
}
其他的
{
printf(“成功”\n);
}
printf(“我在听”\n);
监听(服务器,SOMAXCONN);
//----------------------------------------------------
而(1)
{
clientAddress中的结构sockaddr_;
无符号整数地址大小;
int client=accept(server,(struct sockaddr*)和clientAddress,以及addressSize);
int child=fork();
if(child==0)//如果我们建立了一个连接,我们必须创建一个子节点,然后在其中传输文件
{
int file=open(“google.html”,仅限ordu);//打开文件
if(文件0)//在客户端上写入读取字节
{
int写入的字节数;
写入字节=写入(客户端、缓冲区、读取字节);
//printf(“%d\n”,writed_bytes);//打印以检查传递给客户端的字节数(1024)
read_bytes-=writed_bytes;//中断循环
}               
} 
}
关闭(服务器);
关闭(服务器,关闭\RDWR);
睡眠(1);
打破
}
返回0;
}
客户端.c

#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
    struct sockaddr_in serverAddress;
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_addr.s_addr=INADDR_ANY;
    serverAddress.sin_port = htons(9999);


    int server = socket(AF_INET,SOCK_STREAM,0);
    if(server == -1)
    {
        perror("Cannot open the socket");
        exit(1);
    }

    int active = 1;
    setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &active, sizeof(active));
    if(bind(server, (void*)&serverAddress, sizeof(serverAddress)) !=0) 
    {
        perror("Fail bind");
        exit(1);    
    }
    else
    {
        printf("success\n");
    }


    printf("I'm listening\n");

    listen(server, SOMAXCONN); 

//----------------------------------------------------

    while(1)
    {
        struct sockaddr_in clientAddress;
        unsigned int addressSize;
        int client = accept(server, (struct sockaddr*)&clientAddress, &addressSize);

        int child = fork();
        if(child==0) //in case we stablished a connection we have to create a child and then transfer the file inside of it
        {
            int file = open("google.html",O_RDONLY); //open file

            if(file <0)
            {
                perror("Empty file!\n");
                return 1;
            }

            while(1) //Iterate until function read cannot read more bytes
            {
                unsigned char buffer[1024]={0};
                int read_bytes = read(file,buffer,sizeof(buffer));

                if(read_bytes==0)
                {
                    printf("Transfer!\n");
                    break;
                }
                while(read_bytes>0) //Write on the client the reading bytes
                {
                    int written_bytes;
                    written_bytes = write(client, buffer, read_bytes);
                    //printf("%d\n",written_bytes); //print to check how many bytes are passed to the client (1024)


                    read_bytes -= written_bytes; //breaks loop
                }               
            } 
        }

        close(server); 
        shutdown(server, SHUT_RDWR);
        sleep(1);
        break;

    }
    return 0;
}
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>


int main(int argc, char* argv)
{
    if(argc ==2) //you've got to received as an argument the IP of the server, i haven't implemented it yet!
    {

        int client = socket(AF_INET,SOCK_STREAM,0);
        char recvBuffer[1024];
        int n=0, s;
        if(client <0)
        {
            printf("Cannot open the socket\n");
            exit(1);
        }

        struct sockaddr_in serverAddress;
        serverAddress.sin_family = AF_INET;
        serverAddress.sin_addr.s_addr=INADDR_ANY;
        serverAddress.sin_port = htons(9999);


        if(connect(client, (void*)&serverAddress, sizeof(serverAddress)) !=0)
        {
            perror("Cannot be connected");
            return 1;
        }

        printf("Connecting!");
        char buffer2[1024];
        while((n=read(client, recvBuffer,sizeof(recvBuffer)-1))>0)
        {
            //printf("%d\n",n); //i used to check the num of bytes passed by this and everything it's correct
            recvBuffer[n] =0; 
            printf("%s",recvBuffer); //error, the transferation goes wrong here
        }


        if(n==0)
        {
            shutdown(client, SHUT_RD);
            close(client);  
        }
        else if(n<0)
        {
              perror("Read error\n");
              return 1;  
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv)
{
如果(argc==2)//您必须接收服务器的IP作为参数,我还没有实现它!
{
int client=socket(AF_INET,SOCK_STREAM,0);
char recvBuffer[1024];
int n=0,s;
如果(客户端0)
{
//printf(“%d\n”,n);//我过去经常检查此文件传递的字节数以及它是否正确
recvBuffer[n]=0;
printf(“%s”,recvBuffer);//错误,此处的传输出错
}
如果(n==0)
{
关机(客户端,关机);
关闭(客户);
}

否则如果(你介意告诉我们更多关于“它开始给我带来麻烦”的事吗?什么问题?打印了什么?是的,正如你所看到的,我打印了服务器传递的字节数以检查一切是否正常。所以,当我尝试打印缓冲区时,字节数也等于-1。这意味着写函数工作不正常,我不知道为什么。而且缓冲区从未打印。你是这么说的吗服务器中写入的字节数是-1?是的!这正是发生的情况!此外,如果您注释缓冲区的printf并取消注释
printf(“%d\n”,n),则在while中我用于读取client.c中的数据
您将看到传输没有问题!服务器和客户端的写入和读取字节打印得非常完美。我想我没有正确解释。对不起