recv 함수 리턴 -1일 때

balgarac1의 이미지

링버퍼를 만들고 수신이 없을 경우 블럭이 되야 하는데 블럭이 되지 않고 -1을 리턴 합니다.

while (true)
	{
 
		WaitForSingleObject(hMutex, INFINITE);
		strLen = readbuffer->readBuf(readbuffer, userInfo->sock);
		if (strLen <= 0)
			break;
 
		readbuffer->getMessage(readbuffer, Buff, 100);
		ReleaseMutex(hMutex);
 
	}

strLen값을 확인해보니 -1이 됩니다. readbuffer->readBuf함수는

class ReadBuffer
{
 
	private:
 
		int count;
		int rear;
		int front;
		int bufsize;
		char *buf;
 
 
	public:
 
		ReadBuffer();
		~ReadBuffer();
 
		static ReadBuffer * readbuffer;
	    static ReadBuffer * createRbuf();
		void	destroyRbuf(ReadBuffer *rbuf);
		void 	initRbuf(ReadBuffer *rbuf, int bufsize);
		int		readBuf(ReadBuffer *rbuf, SOCKET fd);
};
 
int	ReadBuffer::readBuf(ReadBuffer *rbuf, SOCKET fd)
{
 
	int empty;
	int len, n;
 
	empty = rbuf->bufsize - rbuf->count;
	len = rbuf->bufsize - rbuf->front;
 
	if (rbuf->front + empty > rbuf->bufsize)
	{
 
		char *base1 = rbuf->buf + rbuf->front;
		char *base2 = rbuf->buf;
 
		n = recv(fd, base1, len, 0) + recv(fd, base2, empty - len, 0);
 
	}
	else
	{
 
		char *base1 = rbuf->buf + rbuf->front;
		n = recv(fd, base1, empty, 0);
		if (n < 0)
			memcpy(base1, "", 0);
		cout << "WSAgetLastError():" << WSAGetLastError() << endl;
	}
 
	if (n < 0)
		return -1;
 
	rbuf->count += n;
	rbuf->front = (rbuf->front + n) % rbuf->bufsize;
 
	return n;
}

이고 링버퍼에 2가지 경우(load pointer와 read pointer의 위치 경우의 수)에 따라 if else 문으로 나누어 놓았습니다.
위의 코드를 디버깅해보니 10014 오류가 난다고 알려주었습니다. 알아보니 메모리 참조에 문제가 있다고 파악했는데 도저히 어디에서 메모리 참조 오류가
나는지 모르겠습니다.

balgarac1의 이미지

RingBuffer는 싱글톤으로 했습니다.

ReadBuffer * ReadBuffer::createRbuf()
{
 
	if (ReadBuffer::readbuffer == NULL)
	{
		ReadBuffer::readbuffer = new ReadBuffer;
		return ReadBuffer::readbuffer;
	}
	else
	{
		return ReadBuffer::readbuffer;
	}
}
jick의 이미지

정확히 뭘 하시려는지는 모르겠지만,

(1) len 값이 정의가 안돼있네요.

(2) n = recv(fd, base1, len, 0) + recv(fd, base2, empty - len, 0);
C/C++은 statement 하나 안에 함수 호출이 여러 개 있을 때 계산 순서를 보장하지 않습니다. 즉 이 문장에서 어느 recv가 먼저 불릴지는 컴파일러 마음대로입니다.

(3) 이건 코드가 맞게 동작하느냐와는 관계 없습니다만, readBuf는 멤버 함수이니 그냥 this를 쓰면 되고 bufsize, count 등등도 전부 그냥 적으면 되는데 불필요하게 this와 같은 값을 한번 더 인자로 넘겨주고 안에서는 전부 rbuf->bufsize 등등으로 쓰고 있네요.

balgarac1의 이미지

rbuf는 this로 다 바꾸었습니다.

(1) len 값이 아니라 buf가 정의되지 않은 걸로 파악했습니다. 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.