MFC TCP 소켓 통신 채팅 프로그램 소켓값 질문드립니다.

익명 사용자의 이미지

MFC랑 winsock을 이용해서 클라이언트 및 서버와의 채팅프로그램을 만들고 있습니다.
클라이언트 소켓을 std::vector에 넣어서 사용하려고 했는데 구현중에 문제가 생겼습니다.

소켓에 저장된 클라이언트의 정보(IP, PORT)를 받아서 ListBox에 출력하고싶은데

벡터에서 특정 정보를 뽑아서 출력할 수 있는 방법이 있나요? 또, 반복문을 사용하지 않고 recv에서 받아온 내용을 출력하고 싶은데 어떻게 해야하나요?

아래는 클라이언트 Accept 코드 일부입니다.
dlg->client_addr = { 0 };
dlg->client_addr.sin_family = AF_INET;
dlg->client_addr.sin_port = htons(ServerPort_Num);
dlg->client_addr.sin_addr.s_addr = htonl(INADDR_ANY);

int size = sizeof(SOCKADDR_IN);
#endif
dlg->client_sock = accept(dlg->server_sock, (SOCKADDR*)&dlg->client_addr, &size);
if (dlg->client_sock == SOCKET_ERROR)
{
AfxMessageBox("accept() Error");
continue;
}
client_list.push_back(dlg->client_sock);

아래는 구현중 문제생긴 코드 입니다.

#if 1
#define BUFSIZE 128
static UINT ServerRecvSendFunc(void *p)
{
CServerDlg *dlg = (CServerDlg *)AfxGetApp()->m_pMainWnd;
SOCKET socket = (SOCKET)p;

char readbuffer[BUFSIZE + 1];
BYTE readbuffer__[BUFSIZE + 1];
int recvsize;

BYTE SendBuffer_TEST[BUFSIZE + 1];

while (1)
{
recvsize = recv(socket, readbuffer, BUFSIZE, 0);

if (recvsize <= 0)
{
break;
}
readbuffer[recvsize] = '\0';

CString strTemp;
CString strMsg;

char temp[60];
memset(readbuffer__, 0x00, sizeof(readbuffer__));
memcpy(&readbuffer__, &readbuffer[0], BUFSIZE);
for (int i = 0; i < recvsize; i++)
{
strTemp.Format(_T("%c"),readbuffer[i]);
strMsg += strTemp;
}
dlg->m_ListChat.AddString(strMsg);
strMsg = "";
strTemp = "";
#if 1
//log
#endif
//log
#if 1
for (int i = 0; i < client_list.size(); i++)
{
if (client_list[i] == socket)
{
int sendsize = send(client_list[i], readbuffer, strlen(readbuffer), 0);
}
}
#endif
}
#if 1

CString DisConnectInfo_str;
CString DisConnectInfo_str2;


std::vector::iterator iter = client_list.begin();
for (int i = 0; i < client_list.size(); i++)
{
if (client_list[i] == socket)
{
client_list.erase(iter);
break;
}
iter++;
}
#endif
if (socket == NULL)
{
}
closesocket(socket);
}
#endif

라스코니의 이미지

질문하시는 바를 잘 모르겠네요. 질문은 아래 두개로 보이는데 맞나요?

1) 벡터에서 특정 정보를 뽑아서 출력할 수 있는 방법이 있나요?
===> 벡터에 특정한 정보를 넣으면 되지요. 혹시 std::map 같은 구조체를 찾으시는 건 아닌가요?

2) 또, 반복문을 사용하지 않고 recv에서 받아온 내용을 출력하고 싶은데 어떻게 해야하나요?

for (int i = 0; i < recvsize; i++)
{
   strTemp.Format(_T("%c"),readbuffer[i]);
   strMsg += strTemp; 
}

이 부분인가요? strMsg = CString(readbuffer, recvsize); 이걸 원하시는 건가요?
익명 사용자의 이미지

1) 서버 accept 했을 때 client_list.push_back(dlg->client_sock)를 사용해서 client_list 라는 벡터에 client_sock의 정보들을 넣었습니다. 그럼 client_sock 값들을 나중에 또 빼서 사용해야 하는데 그 사용방법을 모르겠더라구요 예를 들어서 클라이언트가 접속을 끊었을 때 그 클라이언트의 IP, PORT등의 주소를 빼오는 등의 구현을 하고싶은데 방법을 모르는 것 입니다.
2) 말씀해주신 부분이 맞습니다. strMsg=CString(readbuffer, recvsize);로 strMsg에 값을 받고나서
dlg->m_ListChat.AddString(strMsg)하면 되는거겠네요??

익명 사용자의 이미지

위 코드랑 아래 코드랑 쓰레드가 달라서 client_sock 값을 가져오는게 힘들더라구요..

라스코니의 이미지

가장 간단하게 하나의 client IP에 하나의 socket만 부여한다고 가정하면 std::map(key, value) STL 기능을 쓰시면 될 것으로 생각됩니다. key에는 ip (4 byte) 값을 넣으시면 되고요, value에는 socket 값을 넣으시면 됩니다. 상세한 사용법은 구글링하시면 되고요.

댓글 달기

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