UDP 소켓 프로그래밍 질문

pcw919의 이미지

하나의 프로그램으로 채팅하는것을 구현하고있는데요.

제가 어떤식으로 구현을 하였나 하면,

2개의 클래스를 선언하고 하나는 서버(bind) thread를 이용, 계속해서 유저가 말을 쓸수 있는게 만들었구요. (문자열받아 send)
나머지 하나의 클래스는 (connect)후, 지속적으로 recv만 하게 해놨습니다.

그러니 총 2개의 소켓이 이용되고있습니다.(받는,보내는)^^초짜라; 이방법이 생각나서요

그럼 문제점이 무었이냐하면, 문자열을 받아 send를 한후에 recv 쪽 함수가 3번돌아버리네요...
while(1)
{
strLen = recv(SocketS, messageS, BUFSIZE-1, 0);
messageS[strLen] = 0;
printf("message from Friend : %s \n", messageS);
}
recv쪽입니다... 이래서 뒤에
ZeroMemory로 메세지초기화도 해보았는데, 이상하게 이 while문이 3번이나 돌아버리면서 초기화된 (뒤에 ZeroMemory존재할때) messageS를
recv 받았던 그때로 되돌려버리네요...(예를들면 hello-> 출력 -> ZeroMemory-> 다시 hello로 부활 -> 출력 -> ZeroMemory 이런식으루요)

도무지 왜이런지 모르겠네요.
왜이런것일까요?

//c++ / winsock2 입니다.

chadr의 이미지

3번을 돌수도 있고 4번을 돌수도 있고 심지어 입력하신 문자열 길이만큼 반복될수도 있습니다.
UDP는 신뢰성이 없는 프로토콜입니다. 중복검사도 안해주며 순서도 보장하지 않으며 데이터가 정상적인
데이터라는 것도 보장 안해줍니다.

만약에 로컬에서 테스트 하시는 경우라면 다른 부분을 찾아봐야겠지만 만약에 실제 원격 네트워크에 연결되어있는
상태에서 테스트 하신거라면 충분히 발생할 수 있는 상황입니다.

로컬에서 테스트 하시는 경우라면 거의 문제가 발생하지 않습니다. recv가 리턴하는 값을 한번 확인해보세요.
recv가 리턴하는 값은 서버에서 send함수가 리턴하는 값과 같아야합니다. UDP는 TCP와 같이 쪼개들어오는 경우는
없으므로 확인 해보셔야 할 것 같습니다.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

pcw919의 이미지

잘몰라서 드리는 질문인데요.
recv함수의 4번째 파라미터 nflag 가 0일경우, queue 에 넣었다가 삭제시키면서 1회만 recv 받고
다음 recv를 기다려야 하는것 아닌가요?
send 함수쪽에서는 분명 send를 한후 다시 유저의 문자열입력코드쪽에서 대기하게 되는데,

저 위 본문의 recv함수쪽은 recv를 3번 받은마냥 돌아버리니...

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.