UDP를 이용한 클라이언트/서버 네트워크 프로그램에서 servLen=sizeof(servLen)의 사용이 적절한지 문의드립니다.

pr4nkst3r의 이미지

아래 27_3.c, 27_4.c 는 각각 UDP 서버와 UDP클라이언트입니다.

클라이언트에서 보낸 데이터를 서버에서 받아 출력하고 받은 데이터를 그대로 클라이언트에게 보내면 클라이언트 역시 받은 내용을 출력하는 기능을 하는 프로그램입니다. 이러한 일을 반복 실행하고 클라이언트에서 quit를 입력받으면 통신이 종료하게 됩니다. 통신이 종료되면 서버는 또다른 요청을 기다립니다.

그런데 UDP 클라이언트 27_4.c 밑에서부터 15번째 줄에 있는 servLen=sizeof(servLen)의 사용이 적절
한가(맞는가)의문이 듭니다. 초보자를 위한 Linux&Unix C프로그래밍이란 책에 나와있는 예제소스인데
제 생각엔 servLen=sizeof(servAddr) 라고 해야 맞는거 같습니다. 그런데 servLen=sizeof(servLen) 이라고 해도 위 단락에서 말한 기능대로 제대로 작동됩니다. (물론 servLen=sizeof(servAddr)해도 됩니다.) recvfrom 함수보면 마지막 인수인 socklen_t *fromlen 이 한번에 수신할수 있는 최대 데이터의 크기라고 알고있습니다. servLen=sizeof(servAddr) 이 아니라 servLen=sizeof(servLen)해도 동작되는 이유와 오류가 발생할 가능성은 없는지에 대해서도 알려주십시오. (책 소스에 이상이 있는건지 알고 싶습니다.)

-------------------------------실행결과--------------------------
$27_3
Received: C

Received: Java

Received: Programming

$27_4 127.0.0.1
Input sending message ==> C
Received: C

Input sending message ==> Java
Received: Java

Input sending message ==> Programming
Received: Programming

Input sending message ==> quit
$

-------------------------------27_3.c---------------------------------

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 7777 /* 포트 번호 */
#define BUFSIZE 1024

main()
{
int sockfd;
struct sockaddr_in servAddr;
struct sockaddr_in clntAddr;
char recvBuffer[BUFSIZE];
int clntLen;
int recvLen;

/* 인터넷으로 연결된 프로세스들 간에 통신을 하고 UDP 방법을 이용하는 소켓을 생성 */
if((sockfd=socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("sock failed");
exit(1);
}

/* servAddr를 0으로 초기화 */
memset(&servAddr, 0, sizeof(servAddr));
/* servAddr에 IP 주소와 포트 번호를 저장 */
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(PORT);

/* sockfd 소켓에 주소 정보 연결 */
if(bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) {
perror("bind failed");
exit(1);
}

/* 무한 반복 */
while(1) {
clntLen = sizeof(clntAddr);
/* sockfd 소켓으로 들어오는 데이터를 받아 recvBuffer에 저장하고
클라이언트 주소 정보를 clntAddr에 저장 */
if((recvLen=recvfrom(sockfd, recvBuffer, BUFSIZE-1, 0, (struct sockaddr*)&clntAddr, &clntLen)) == -1) {
perror("recvfrom failed");
exit(1);
}
recvBuffer[recvLen] = '\0';
/* 받은 데이터를 출력 */
printf("Recevied: %s\n", recvBuffer);

/* 받은 데이터를 클라이언트에게 보냄 */
if(sendto(sockfd, recvBuffer, recvLen, 0, (struct sockaddr*)&clntAddr, sizeof(clntAddr)) != recvLen) {
perror("sendto failed");
exit(1);
}
}
}

----------------------------------27_4.c-------------------------

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 7777 /* 서버의 포트 번호 */
#define BUFSIZE 1024

/* argv[1]은 수와 점 표기의 IP 주소 */
main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in servAddr;
char sendBuffer[BUFSIZE], recvBuffer[BUFSIZE];
int recvLen, servLen;

if(argc != 2) {
fprintf(stderr, "Usage: %s IP_address\n", argv[0]);
exit(1);
}

/* 소켓 생성 */
if((sockfd=socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("sock failed");
exit(1);
}

memset(&servAddr, 0, sizeof(servAddr));
/* servAddr에 IP 주소와 포트 번호를 저장 */
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(argv[1]);
servAddr.sin_port = htons(PORT);

/* quit를 입력 받을 때까지 반복 */
while(1) {
printf("Input sending message ==> ");
fgets(sendBuffer, BUFSIZE, stdin);
if(!strncmp(sendBuffer, "quit", 4))
break;

/* sockfd 소켓을 통해 servAddr을 주소로 갖는 서버에게 데이터를 보냄 */
if(sendto(sockfd, sendBuffer, strlen(sendBuffer), 0, (struct sockaddr*)&servAddr, sizeof(servAddr)) != strlen(sendBuffer)) {
perror("sendto failed");
exit(1);
}

servLen = sizeof(servLen);
/* sockfd 소켓으로 들어오는 데이터를 받아 recvBuffer에 저장 */
if((recvLen=recvfrom(sockfd, recvBuffer, BUFSIZE-1, 0, (struct sockaddr*)&servAddr, &servLen)) != strlen(sendBuffer)) {
perror("recvfrom failed");
exit(1);
}

recvBuffer[recvLen] = '\0';
/* 받은 데이터를 출력 */
printf("Recevied: %s\n", recvBuffer);
}
close(sockfd);
exit(0);
}

댓글 달기

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