I have a file pict.bmp which i need to send over the LAN.There is a server which starts listening on a port.After the client establishes connection with the server,it requests the server providing a file name for the file.The server then uses the filename and sends to the client.But the problem is that the server sends it successfully using the sendfile() system call but the file does not appear in the directory in which the client is running.is there a better way to do it i.e some other system call which fulfills the same purpose.I am copy pasting the sending a receiving part here :
服务器:
while(1){
nsd = accept(sd,(struct sockaddr*)&clit,&clen);
if(nsd < 0){
eMsg("Accept error");
}
rc = recv(nsd, filename, sizeof(filename), 0);
if (rc < 0) {
eMsg("Receive error");
}
printf("Filename : %s
",filename);
filename[strlen(filename) - 1] = ;
if (strcmp(filename, "quit") == 0) {
fprintf(stderr, "quit command received, shutting down server
");
break;
}
fd = open("2bird.bmp", O_RDONLY);
printf("fd is %d
",fd);
if (fd < 0) {
eMsg("File Open error");
}
offset = 0;
rc = sendfile (nsd, fd, NULL, 1);
if (rc < 0) {
eMsg("File Send error");
fprintf(stderr,"Sending failed");
}
if (rc != stat_buf.st_size) {
eMsg("File Transfer error");
}
close(fd);
close(nsd);
客户:
bzero((char *) &serv_addr, sizeof(serv_addr));
39
40 serv_addr.sin_family = AF_INET;
41
42 bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,server->h_length);
43
44 serv_addr.sin_port = htons(atoi(argv[2]));
45
46 if (connect(sd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
47
48 eMsg("Connection error");
49
50 }
51
52 printf("Please enter the filename : ");
53
54 bzero(buffer,256);
55
56 fgets(buffer,255,stdin);
57
58 n = write(sd,buffer,strlen(buffer));
59
60 if (n < 0)
61
62 error("Writing To Socket error");
63
64
65 close(sd);
66
67 return 0;
68
69 }
70
71 void eMsg(char *M){
72
73 perror(M);