Code server
static int num = 0;
FILE *outfile;
void handle_client(int c)
{
    //char buf[1048576];
    char buf[1048576];
    char *lastpos;
    int size;
    outfile = fopen ("win.iso", "wb");
    while (1) {
	//printf("waiting data ...");
        size = recv(c, buf, 1048576, MSG_WAITALL);//MSG_NOSIGNAL);
        if (size == 0) {
            break;
        }
	//printf("%d Recv:%d %c %s\n",++num,size,buf[1048575],buf );
        //lastpos = strchr(buf, '\n');
        //send(c, buf, lastpos+1-buf, 0);
	fwrite(buf, size, 1, outfile);
	//break;
    }
	fclose(outfile);
}
int main()
{
    int s, c;
    int reuseaddr = 1;
    struct sockaddr_in6 addr;
    int pid;
    s = socket(AF_INET6, SOCK_STREAM, 0);
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &reuseaddr, sizeof(reuseaddr));
    addr.sin6_family = AF_INET6;
    addr.sin6_port = htons(5000);
    addr.sin6_addr = in6addr_any;
    bind(s, (struct sockaddr *)&addr, sizeof(addr));
    listen(s, 5);
    while (1) {
	printf("listening client ...\n");
        c = accept(s, NULL, NULL);
	printf("accept a connection\n");
        /*
        pid = fork();
        if (pid == -1) {
            exit(1);
        } else if (pid == 0) {
            close(s);
            handle_client(c);
            close(c);
            return 0;
        } else {
            close(c);
            waitpid(pid, NULL, 0);
        }*/
        handle_client(c);
        //close(c);
    }
}
Code client
FILE* file_;
char buffer[1024];
/*
void readfile()
{
    file_ = fopen("client.c", "rb");
    while (!feof(file_)) // to read file
    {
        // fucntion used to read the contents of file
        fread(buffer, sizeof(buffer), 1, file_);
        printf("%s\n",buffer);
    }
}
*/
void ping(int s, char *message)
{
    char buf[1024];
    strncpy(buf, message, sizeof(buf));
    send(s, buf, strlen(buf), 0);
    //recv(s, buf, 8192, 0);
    //strtok(buf, "\n");
    //puts(buf);
}
int main()
{
    int s,num=0;
    struct sockaddr_in6 addr;
    s = socket(AF_INET6, SOCK_STREAM, 0);
    addr.sin6_family = AF_INET6;
    addr.sin6_port = htons(5000);
    inet_pton(AF_INET6, "::1", &addr.sin6_addr);
    connect(s, (struct sockaddr *)&addr, sizeof(addr));
    while(1){
    	//ping(s, "xinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchaoxinchao");
		file_ = fopen("/home/dungnt/Win10_x64.iso", "rb");
		//file_ = fopen("client.c", "rb");
		    while (!feof(file_)) // to read file
		    {
			// fucntion used to read the contents of file
			fread(buffer, sizeof(buffer), 1, file_);
			//printf("%s\n",buffer);
			num++;
			ping(s,buffer);
			sleep(0.1);
			//printf("%d\n",num);
		    }
		fclose(file_);
		break;
	}
    printf("%d\n",num);
    close(s);
    return 0;
}
        
      
    
    83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?