用C写的内存插槽中块 [英] Writing memory to socket in chunks in C
本文介绍了用C写的内存插槽中块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图存储器的内容写入到数据块的插座。我可以写那些比我更小的缓冲文件,但别的,我在深水中。
/ *文件的内容分配内存* /
焦炭fileContents =的malloc(sizeof的(char)的文件大小*);/ *文件读入内存* /
阅读(文件描述符,fileContents,文件大小);INT chunksWritten;/ *写内存插座? * /
如果(文件大小> MAX_BLOCK_SIZE){ 而(chunksWritten<文件大小){
//发生的事情吗?
}}其他{
chunksWritten = writen(SD,fileContents,文件大小); //这对文件< MAX_BLOCK_SIZE
}
在这里writen写到我的插座:
INT writen(INT FD,字符* buf中,INT为nbytes){
短DATA_SIZE =为nbytes;
诠释N,NW;
如果(为nbytes> MAX_BLOCK_SIZE)
回报(-3); DATA_SIZE = htons(DATA_SIZE);
如果(写(FD,(字符*)及!DATA_SIZE,1)= 1)返回(-1);
如果(写(FD,(字符*)(&放大器;!DATA_SIZE)+ 1,1)= 1)返回(-1);
/ *送*为nbytes /
为(N = 0; N<为nbytes; N + = NW){
如果((NW =写(FD,BUF + N,为nbytes - N))< = 0)
返回(NW);
}
返回(N);
}
这似乎应该是很容易的,但我在努力寻找任何很好的例子。
解决方案
/ *循环外* /
chunksWritten = 0;
INT较小;
INT R;
INT sizeRemaining =文件大小;
//字符* fileChunk =的malloc(sizeof的(char)的* MAX_BLOCK_SIZE + 1);
//的memcpy(fileChunk,fileContents,sizeof的(字符)* MAX_BLOCK_SIZE);
// R = writen(SD,fileChunk,MAX_BLOCK_SIZE);
R = writen(SD,fileContents,MAX_BLOCK_SIZE);
如果(R == - 1){
/ *处理的方式错误,适合你的程序*其余/
}
chunksWritten = chunksWritten + R;
sizeRemaining = sizeRemaining - MAX_BLOCK_SIZE;而(sizeRemaining大于0){
如果(sizeRemaining> MAX_BLOCK_SIZE){
小= MAX_BLOCK_SIZE;
}其他{
小= sizeRemaining;
}
//的memcpy(fileChunk,fileContents +的sizeof(字符)* chunksWritten,sizeof的(char)的*小);
// R = writen(SD,fileChunk,MAX_BLOCK_SIZE);
R = writen(SD,fileContents [文件大小 - sizeRemaining],小);
如果(R == - 1){
/ *处理的方式错误,适合你的程序*其余/
}
sizeRemaining = sizeRemaining - MAX_BLOCK_SIZE;
}/ *
提醒:清理fileChunk&安培; fileContents如果你不需要他们以后
* /
您当然可以返工循环,而不是倒计时组成。我能想到更好的倒计时。
编辑:提出基于注释的一些变化
I'm attempting to write memory contents to a socket in chunks. I can write files that are smaller than my buffer, but anything else and I'm in deep water.
/* allocate memory for file contents */
char fileContents = malloc(sizeof(char)*filesize);
/* read a file into memory */
read(fileDescriptor, fileContents , filesize);
int chunksWritten;
/* Write the memory to socket? */
if (filesize > MAX_BLOCK_SIZE){
while (chunksWritten < filesize){
// what goes here?
}
} else {
chunksWritten = writen(sd, fileContents, filesize); // this works for files < MAX_BLOCK_SIZE
}
writen here writes to my socket:
int writen(int fd, char *buf, int nbytes) {
short data_size = nbytes;
int n, nw;
if (nbytes > MAX_BLOCK_SIZE)
return (-3);
data_size = htons(data_size);
if (write(fd, (char *) & data_size, 1) != 1) return (-1);
if (write(fd, (char *) (&data_size) + 1, 1) != 1) return (-1);
/* send nbytes */
for (n = 0; n < nbytes; n += nw) {
if ((nw = write(fd, buf + n, nbytes - n)) <= 0)
return (nw);
}
return (n);
}
This seems like it should be quite easy, but I'm struggling to find any good examples.
解决方案
/* outside the loop */
chunksWritten = 0;
int smaller;
int r;
int sizeRemaining = filesize;
//char *fileChunk = malloc(sizeof(char)*MAX_BLOCK_SIZE+1);
//memcpy(fileChunk, fileContents, sizeof(char)*MAX_BLOCK_SIZE);
//r = writen(sd, fileChunk, MAX_BLOCK_SIZE);
r = writen(sd, fileContents, MAX_BLOCK_SIZE);
if(r==-1) {
/* deal with error in a manner that fits the rest of your program */
}
chunksWritten = chunksWritten + r;
sizeRemaining = sizeRemaining - MAX_BLOCK_SIZE;
while(sizeRemaining > 0){
if(sizeRemaining > MAX_BLOCK_SIZE){
smaller = MAX_BLOCK_SIZE;
} else {
smaller = sizeRemaining;
}
//memcpy(fileChunk, fileContents+sizeof(char)*chunksWritten, sizeof(char)*smaller);
//r = writen(sd, fileChunk, MAX_BLOCK_SIZE);
r = writen(sd, fileContents[filesize - sizeRemaining], smaller);
if(r==-1) {
/* deal with error in a manner that fits the rest of your program */
}
sizeRemaining = sizeRemaining - MAX_BLOCK_SIZE;
}
/*
Reminder: clean-up fileChunk & fileContents if you don't need them later on
*/
You certainly can rework the loop to count up instead of down. I can think better counting down.
Edit: made a few changes based on comments.
这篇关于用C写的内存插槽中块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文