tcp 구조체 전송 질문입니다.

jean318의 이미지

안녕하세요. 제가 tcp 서버를 작성하고 있는데요..
으윽 계속 안개속이예요ㅠㅠ
문제는 서버와 클라이언트가 reqinfo라는 구조체를 주고 받는데
거기서 opcode가 1이면 서버에서 recvbuf를 읽어서
다시 클라이언트에게 전송하는 겁니다.

그런데 자꾸 recvbuf가 256만큼 읽어지지 않고 10바이트 정도 잘려서 읽어오고 맙니다.
클라이언트에서 보낸 recvbuf에 있는 내용을 다 읽으려면 어떻게 해야하나요?

ps. 저는 read가 한꺼번에 다 못읽을 경우 반복해서 읽어야 한다는 것을 보았는데요..
그냥 문자열만 주고받으면 할 수 있을거 같은데
구조체에 있는 문자열을 읽어와야 해서 더 헷갈립니다.

조언 부탁드립니다.

///Echo server
#include
#include
#include
#include
#include
#include
#include

#define BUF_SIZE 256

struct reqinfo {
int opcode;
char recvbuf[BUF_SIZE];
};

int main(int argc, char *argv[])
{
int listenfd, connfd;
struct sockaddr_in cliaddr, servaddr;
int str_len;
socklen_t addr_size;

//소켓생성
//바인드
//리슨
//accept

//receive and write
while( read(connfd, &(info),sizeof(struct reqinfo) ) > 0) //**
{
//opcode check
if(info.opcode==1){
printf("from client:%s", info.recvbuf); //**
write(connfd, &(info), sizeof(struct reqinfo));
memset(&recvbuf, 0, sizeof(recvbuf));
}else{
printf("opcode is not 1\n");
}
}
}

baboda4u의 이미지

아마 sizeof(struct reqinfo)의 길이만큼 읽어 와야 하는데 위 소스에서는

while문의 의미가 없군요....read()는 일단 읽어 온 바이트 길이를 반환하는데

"while( read(connfd, &(info),sizeof(struct reqinfo) ) > 0)"게 하시면...

한번밖에 루프를 돌지 않습니다. read()가 10만큼 읽어 오든 240만큼 읽어 오든 0보다는

큰 값이기에 read가 한번 밖에 수행이 안되어서 원하는 길이 만큼 다 읽어 오지 못하는데 문제가 있습니다.

char buf[MAXSIZE];
int read_len = 0;
int size = sizeof(struct reqinfo);

while( read_len += read(connfd, buf + read_len, size - read_len)
{
if(read_len == size)
break;
}
요렇게 하면 원하는 만큼 읽겠죠...

ps. 위소스에서 info 정의가 안보이는 군요...구조체 포인터로 보입니다만...-_-;

============================
Stay Hungry, Stay Foolish

============================
Stay Hungry, Stay Foolish

댓글 달기

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