下面我有一个来自我的服务器代码和客户端代码的代码段。我的客户端向服务器发送一个整数,服务器成功接收,然后我向服务器发送一个长度为 str_len 的字符串。服务器中的第二次读取不起作用,它正在阻塞并且不读取任何内容。当我退出客户端时,服务器会打印出它没有读取任何内容。怎么了?
//Server code
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
unsigned int *length = new unsigned int;
memcpy(length, buffer, sizeof(int));
cout << "Length : " << *length << endl;
int len = *length + 1;
char buffIn[len+1];
bzero(buffIn,len);
//ok msg?
n = read(newsockfd,buffIn,len);
if (n < 0) error("ERROR reading from socket");
cout << "value of n" << n << endl;
printf("Received : %s\n", buffIn);
//client method
void send(string req)
{
//Send string len
unsigned int str_len = req.length();
//str_len = 3000;
write(socketFd, &str_len, sizeof(str_len));
//Send string
const char *str_req = req.c_str();
printf("%s\n",str_req);
cout << "Str len is : " << strlen(str_req) << endl;
write(socketFd, str_req, strlen(str_req) + 1);
cout << "write done " << endl;
}