TCP/IP 통신에서 send(), recv()에서 구조체를 받고,던지는 방법

익명 사용자의 이미지

TCP/IP 통신에서 메시지를 전달할 때 구조체로 전달하려고 합니다.
구조체 안에는 char 배열 한개와 int로 선언된 몇개의 변수가 있습니다.
이것을 send()로 통해서 보내고, 다시 recv()로 받으려고 합니다.
소스는 아래와 같습니다.
구조체에 내용을 담고 그것을 char * 형으로 넘긴다음에,
받은때는 char 배열에 담은 다음에 다시 구조체 형으로 바꾸어서
사용하려고 하는데, 메시지 잘 도착하는데 데이타가 0 으로 나오내요.
고수님덜 좋은 예제나 아래의 소스를 보시고 수정할곳이 있으면
답 메일 부탁드립니다. 감사합니다.

pstSendMsg 전달하려는 전체 메시지 입니다. 헤더와 데이타 포함.
pstVoiceMsg 실제 데이타 입니다.

구조체 내용
typedef struct {
_CT_UMS_MSG_HDR stMsgHdr;
char sMsgBody[1024];
} _CT_UMS_MSG;

typedef struct {
int nCall_Id;
int nMsgName;
int nMsgLength;
int nSender;
int nReceiver;
int nReserved1;
int nReserved2;
} _CT_UMS_MSG_HDR;
====================================================================
메시지 전달
====================================================================
_CT_UMS_MSG *pstSendMsg;
_VoiceCallStart_Req *pstVoiceMsg;
// 중간 생략 //

pstSendMsg = (_CT_UMS_MSG *)malloc(sizeof(_CT_UMS_MSG));
if(pstSendMsg == NULL){
lprintf(LOG_LEVEL_FULL_MESSAGE, "ERROR Memory
Allocation\n");
free(pstSendMsg);
return;
}
pstVoiceMsg = (_VoiceCallStart_Req *)malloc(sizeof
(_VoiceCallStart_Req));
if(pstVoiceMsg == NULL){
lprintf(LOG_LEVEL_FULL_MESSAGE, "ERROR Memory
Allocation\n");
free(pstVoiceMsg);
return;
}
memset(pstSendMsg, 0x00, sizeof(_CT_UMS_MSG));
memset(pstVoiceMsg, 0x00, sizeof(_VoiceCallStart_Req));

lnMsglen = sizeof(_CT_UMS_MSG) + sizeof
(M_VoiceCallStart_Req);
pstSendMsg->nCall_Id = 1;
pstSendMsg->nMsgName = M_VoiceCallStart_Req; // 헥사 값으로
정의 되어 있음.
pstSendMsg->nMsgLength = lnMsglen;
pstSendMsg->nSender = IVR; // int 값으로 정의 되어 있음
pstSendMsg->nReceiver = SVR; // int 값으로 정의 되어 있음

strncpy(pstVoiceMsg->sVoiceDirName, lsDirName, strlen
(lsDirName));
strncpy(pstVoiceMsg->sVoiceFileName, lsFileName, strlen
(lsFileName));
strncpy(pstVoiceMsg->sVoiceOutDirName, lsOutDirName, strlen
(lsOutDirName));
memcpy((_VoiceCallStart_Req *)pstSendMsg->sMsgBody,
pstVoiceMsg, sizeof(_VoiceCallStart_Req));

lprintf(LOG_LEVEL_FULL_MESSAGE,"Message Name %x\n",
pstSendMsg->nMsgName);
lprintf(LOG_LEVEL_FULL_MESSAGE,"Message Length %d\n",
lnMsglen);

if(send(lnCsockfd, (char *)pstSendMsg, lnMsglen, 0) <= 0) {
lprintf
(LOG_LEVEL_FULL_MESSAGE, "M_VoiceCallStart_Req Sending Error\n");
CloseTCP(lnCsockfd);
sleep(1);
if((lnCsockfd = ConnectTCP(UMS_CLI_IP,
UMS_SVR_PORT)) < 0) {
lprintf(LOG_LEVEL_FULL_MESSAGE, "Open listen
socket error\n");
exit(-1);
}
}
else
lprintf(LOG_LEVEL_FULL_MESSAGE, "Message Send
Success\n");

====================================================================

====================================================================
메시지 받음
====================================================================
char lnMsg[BUFFER+1];
_CT_UMS_MSG *pstRecvMsg;
// 중간 생략 //

if(recv(lnCsockfd, (void *)lnMsg, BUFFER, 0) > 0) {
lprintf(LOG_LEVEL_FULL_MESSAGE,"Success Message\n");
pstRecvMsg = (_CT_UMS_MSG *)lnMsg;
lprintf(LOG_LEVEL_FULL_MESSAGE,"Message Length %
d\n", strlen(lnMsg));
lprintf(LOG_LEVEL_FULL_MESSAGE,"Message Data %s\n",
lnMsg);
}
// 중간 생략 //
====================================================================

익명 사용자의 이미지


지금과 같은 구조체 자료를 전송 할때는 고려해야 할 사항이
조금 많은 편입니다.

1. system의 메모리 구조의 동일성및 size검증.
ex) x86 cpu는 int를 4,3,2,1의 순번으로 저장하지만,
risc cpu는 1,2,3,4의 순번으로 저장하므로
반드시 이들 자료의 저장 분서를 일치 시켜야 한다.
network계열의 (inet_xxx)함수들을 사용 해야 할걸요.
2. 자료를 전송할때 구초체 자료들을 pack, unpack하여 자료의
전송을 처리해야 함. 즉 자신의 독자적인 packet화 함수를
작성하여 이를 토대로 재구성할 수 있어야 합니다.

익명 사용자의 이미지

답변 감사드립니다.
앞으로도 잘 부탁드립니다.

댓글 달기

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