두 개의 개별 send () 호출이 테스트를 위해 루프백 주소를 사용하는 동일한 recv () 버퍼에서 끝나는 이유에 대해 누군가가 밝힐 수 있는지 궁금합니다. 한 번 두 개의 원격 시스템으로 전환하면 대신 두 개의 recv () 호출이 필요합니다. ? 나는 wireshark 캡처를 보았지만 이것이 왜 발생하는지에 대해 이해가되지 않는 것 같습니다. 아마도 누군가가 내 코드를 비판하고 내가 잘못되고있는 곳을 말해 줄 수있을 것입니다. 서버에서 들어오는 두 개의 메시지는 클라이언트에 대한 길이가 결정되지 않습니다. 그건 그렇고 우분투에서 C를 사용하여 BSD 소켓을 사용하고 있습니다.
아래에 표시된 예에서 전체 버퍼를 구문 분석하여 두 개의 개별 메시지를 추출하는 것은 이상적인 접근 방식이 아닙니다.
-------서버 측--------
// Send greeting string and receive again until end of stream
ssize_t numBytesSent = send(clntSocket, greeting, greetingStringLen, 0);
if (numBytesSent < 0)
DieWithSystemMessage("send() failed");
//-----------------------------Generate "RANDOM" Message -----------------------
srand(time(NULL)); //seed random number from system clock
size_t randomStringLen = rand() % (RANDOMMSGSIZE-3); //generates random num
// betweeen 0 and 296
char randomMsg [RANDOMMSGSIZE] = "";
// declare and initialize allowable characteer set for the
const char charSet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (randomStringLen) {
--randomStringLen;
for (size_t i = 0; i < randomStringLen; i++) {
int p = rand() % (int) (sizeof charSet - 1);
randomMsg[i] = charSet[p];
}
randomStringLen = strlen(randomMsg);
printf("Random String Size Before newline: %d\n", (int)randomStringLen);
strcat(randomMsg,"\r\n");
}
randomStringLen = strlen(randomMsg);
printf("Random String: %s\n", randomMsg);
//-----------------------------Send "RANDOM" Message ---------------------------
// Send greeting string and receive again until end of stream
numBytesSent = send(clntSocket, randomMsg, randomStringLen, 0);
if (numBytesSent < 0)
DieWithSystemMessage("send() failed");
//------------------------------------------------------------------------------
------고객 입장에서-------
//----------------------------- Receive Server Greeting ---------------------------
char buffer[BUFSIZE] = ""; // I/O buffer
// Receive up to the buffer size (minus 1 to leave space for
// a null terminator) bytes from the sender
ssize_t numBytesRcvd = recv(sock, buffer, BUFSIZE - 1, 0);
if (numBytesRcvd < 0)
DieWithSystemMessage("recv() failed");
buffer[numBytesRcvd] = '\0'; //terminate the string after calling recv()
printf("Buffer contains: %s\n",buffer); // Print the buffer
//printf("numBytesRecv: %d\n",(int)numBytesRcvd); // Print the buffer
//------------------------ Extracts the random message from buffer ---------------------------
char *randomMsg = strstr(buffer, "\r\n"); // searches from first occurance of substring
char randomMessage [BUFSIZE] = "";
strcat(randomMessage, randomMsg+2);
int randomStringLen = strlen(randomMessage)-2;
printf("Random Message: %s\n",randomMessage); // Print the buffer
char byteSize [10];
sprintf(byteSize,"%d", randomStringLen);
printf("ByteSize = %s\n",byteSize);
//----------------------- Send the number for random bytes recieved -------------------------
size_t byteStringLen = strlen(byteSize); // Determine input length
numBytes = send(sock, byteSize, byteStringLen, 0);
if (numBytes < 0)
DieWithSystemMessage("send() failed");
else if (numBytes != byteStringLen)
DieWithUserMessage("send()", "sent unexpected number of bytes");
shutdown(sock,SHUT_WR); // further sends are disallowed yet recieves are still possible
//----------------------------------- Recieve Cookie ----------------------------------------
유닉스 시스템 recv
과 send
의 단지 특별한 경우입니다 read
및 write
그 추가 플래그를 받아들입니다. (Windows는 Winsock으로이를 에뮬레이트합니다.)
일반적으로 사실이 아니기 때문에 하나가 하나에 recv
해당 한다고 가정해서는 안됩니다 send
(단일 쓰기로 작성 되었더라도 여러 부분으로 파일을 읽을 수있는 것처럼). 대신 메시지의 길이를 알려주는 헤더로 각 "메시지"를 시작해야합니다. 별도의 메시지가 무엇인지 아는 것이 중요한 경우, 중요하지 않은 경우 일반 파일처럼 스트림을 읽어야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다