echo server에서 port 입력 시 int 아닌 다른 자료형 질문

전병욱@Facebook의 이미지

echo server에 port 입력을 받으면 int 아닌 char나 다른 자료형 입력해도 연결이 되는데 int만 받게 할 수 있나요?

server입니다.

//echoserver.c

#include "libDefine.h"
#include

void signalHandler(int signals);

// 시그널 처리
void signalHandler(int signals) {
if (signals == SIGINT) {
printf("종료하시겠습니까?");
if (getchar() == 'y') {
exit(0);
}
}

if(signals == SIGPIPE) {

}

}

int main(int argc, char *argv[]) {
int serv_soc, cliSock; // server socket, client socket
int port = sizeof(struct sockaddr_in);
char msg[SIZEBUFFER]; // size buffer 배열
int socket_len;

struct sockaddr_in cli_Addr;
socklen_t cli_sd_len;
int i;

// port 설정
if (argc != 2) {
printf("Usage : %s \n", argv[0]);
exit(1);
}

signal(SIGINT, signalHandler);

//port = atoi(argv[1]);
port = argv[1];

serv_soc = serversSocket(port);

cli_sd_len = sizeof(cli_Addr);

while(1) {

printf("server 대기 중 %d\n", serv_soc);

cliSock = accept(serv_soc, (struct sockaddr*) &cli_Addr, &cli_sd_len);

if (cliSock == -1) {
errHandle((char*) "accept error");
} else {
printf("connected %d\n", cliSock);
}

while ((socket_len = read(cliSock, msg, SIZEBUFFER)) != 0) {
write(cliSock, msg, socket_len);
printf("client로 보내는 message : %d\n", write(1, msg, socket_len));
write(1, msg, socket_len);
}

write(1, msg, socket_len);

close(cliSock);
}
close(serv_soc);

return 0;
}

jangcy999의 이미지

serversSocket 함수가 어떻게 정의 되어 있는지 확인 하셔야 할듯 한데요
c 특성상 컴파일시 옵션에 모든 워닝을 잡는것으로 하지 않으면
파라메터가 뭐가 들어가던지 일단 집어 넣고 보는걸로 알고 있는데요
정의되어 있는 헤더 파일을 확인하시고 include해 보세요

익명 사용자의 이미지

port = argv[1];

이렇게 억지로 쑤셔넣고 있는데 컴파일러가 뭐라 안 하던가요?
제대로 돌 리가 없을 텐데요.

argv는 문자열로 들어오므로 이게 제대로 된 포트를 가리키는 문자열인지 아닌지는 따로 검사해줘야 합니다. 제대로 된 포트를 가리키지 않는 경우에 어떻게 해야 할지도 따로 고려해서 코딩해야 하고요.

김정균의 이미지

port 가 int 형이니, 32bit compiler 에서는 불평을 안할 듯요 ^^; 64bit compiler 에서는 익명님 말씀대로 warning 이 뜨겠죠. int 보다 point 의 자료형이 더 크니까요.

김정균의 이미지

질문 하실 때.. 코드는 <code> ~ </code> block 으로 작성 하세요.
그래야 indent 및 header 파일들이 제대로 출력이 됩니다.

<code lang="c">
#include <stdio.h>
int main (void) {
    print ("Hello World\m");
    return 0;
}
</code>

이렇게 해 주어야

#include <stdio.h>
int main (void) {
    print ("Hello World\n");
    return 0;
}

이렇게 보여 줍니다. 님처럼 그냥 넣으면 아래처럼 나와요.

#include
int main (void) {
print ("Hello World\n");
return 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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.