I want to Server-Client model with FIFO's and Client get directory path but I get errors "read: Bad address" and "write: Bad address".
Client
Server's Error : "read: Bad address"
Client's Error : "write: Bad address"
可能您误用了read
和的返回值write
。成功时,它们返回正值,您将它们作为错误处理。
读取字符串的大小时也不知道。所以strlen
是不合适的。
if( (controlRead = read(fdp,pathName,sizeof(pathName)) ) <= 0)
{
// error ...
与 相同的条件write
。
传输字符串时,最好同时传输字符串长度:
写作:
void write_string(int fd, const char *string)
{
size_t len = strlen(string);
write(fd, &len, sizeof(len));
write(fd, string, len);
}
阅读:
void read_string(int fd, char *buffer, size_t size, size_t *len)
{
size_t t_len;
read(fd, &t_len, sizeof(t_len));
if (t_len > size) t_len = size;
read(fd, buffer, t_len);
if (t_len < size) buffer[t_len] = 0; // null-terminate if there is enough space
if (len) *len = t_len; // return length if wanted
}