![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| read and write from a file | rinku | Shell Programming and Scripting | 2 | 01-10-2008 10:22 PM |
| read/write socket error | gio | High Level Programming | 2 | 04-16-2007 08:52 PM |
| sed to read and write to very same file | 435 Gavea | Shell Programming and Scripting | 5 | 06-29-2006 08:04 PM |
| popening for read and write | szzz | High Level Programming | 1 | 11-18-2003 09:05 AM |
| read, write & STDOUT_FILENO.... | M3xican | High Level Programming | 2 | 07-17-2002 01:41 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#8
|
|||
|
|||
|
Thanku blowtorch,
Ye, I think that's a way. But why cannot we use 2 write() in a row? why that could crash read() on client? Do anybody know this? Chees, Elton |
| Forum Sponsor | ||
|
|
|
#9
|
||||
|
||||
|
I do not mean to say that two writes in a row would crash a client. I'm just trying to say that this would be a better way. If there was any error in the read on the client side, the server would be notified of this and would resend the buffer.
Regarding the client side crash, you need to post the code, or atleast the part where the crash is occurring, so that someone could take a look at it. Also, if posting code, please remember to use the code tags. Not using those makes it very difficult to read the code. |
|
#10
|
|||
|
|||
|
Hi guys,
This my problem prone code: On server side: . . . Code:
void reply(int s, SizedBuffer p, char *type)
{
SizedBuffer sBuf;
char *r;
char l[10], whole[10];
int dataStart;
sprintf(l,"%d",p.size);
dataStart = strlen(header1) + strlen(l) + strlen(header2) + strlen(type);
r = (char *) calloc(dataStart + p.size + 1, sizeof(char));
strcat(r,header1);
strcat(r,l);
strcat(r,header2);
strcat(r,type);
bcopy(p.buffer, r+dataStart, p.size);
/*Write file size to client.*/
sprintf(whole, "%d", dataStart + p.size);
whole[strlen(whole)] = '\0';
write(s, whole, strlen(whole));
/*problem happens here...*/
write(s, r, dataStart + p.size);
free(r);
}
.
.
. . Code:
while(1)
{
memset(buf,'\0', BUFFER_SIZE);
if((newSock = acceptConnection(sp->sock)) == 0)
exit(-1);
nread = Read(newSock, buf, BUFFER_SIZE);
if((c = makeClientSocket((sp->count), ipAddr)) == 0)
exit(-1);
cSock = c->socket;
Write(cSock, buf, BUFFER_SIZE);
memset(buf,'\0', BUFFER_SIZE);
Read(cSock, buf, 10);
fLen = atoi(buf);
printf("fLen is :%d\n", fLen);
Write(cSock, buf, 10);
char * file = malloc((fLen)*sizeof(char));
/*problem happens here: I lose some bytes of the buffer.*/
recv(cSock, file, fLen, MSG_WAITALL);
printf("==> %s\n", file);
write(newSock, file, fLen);
close(newSock);
free(file);
}
. hope you guys can figure it out REGARDS, Elton |
|
#11
|
|||
|
|||
|
That code is missing a great deal, like, what header1, header2, whole, and type is.
|
|
#12
|
|||
|
|||
|
Code:
r = (char *) calloc(dataStart + p.size + 1, sizeof(char));
r[0]='\0';
strcat(r,header1);
A more proper way to do this would be Code:
strcpy(r,header1); Or perhaps even get rid of that mess of strcats -- wasteful, it re-measures the length of the string every time -- with a sprintf: Code:
sprintf(r,"%s%s%s%s",header1,l,header2,type); Code:
sprintf(whole, "%d", dataStart + p.size);
whole[strlen(whole)] = '\0';
Also, this: Code:
Read(cSock, buf, 10); My suggestion? Throw all that code out, learn how strings work in C, start over. It's not just a program bug, it's a design that can't work. Last edited by Corona688; 08-31-2006 at 12:02 PM. |
|||
| Google The UNIX and Linux Forums |