(답변기다림)소켓통신에서 포트 두개를 열어놓을라고 하는데요..

soylove의 이미지

소켓통신에서 포트 두개를 같이 열어놓으려고 프로그램을 만들었습니다.
하나일때 잘 되는것을 확인했었고 두개를 열때는 포트번호만 다르게해서 똑같이 소켓설정하고 옵션설정하고 바인드 하고 리슨하면 될줄 알았는데.. 하나는 포트가 열리는데 나머지 하나가 안열립니다.. 제가 사용한 포트가 특별히 다른 프로그램에서 사용되는 포트가 아니라는건 이미 확인햇구요... 이래저래 찾다가
멤버셋에서 서버의 주소를 INADDR_ANY대신 ip를 직접 넣었더니 바인드가 안되네요...(그런데 이 방법이 정확히 쓴건지도 잘은 모르겠습니다.. 잔머리를 굴린방법이라)

소스 올라갑니다...; 필요하다고 생각되는 부분만....;;

/***************************************/
#define SERVICE_TCP_PORT 305
#define SERVICE_TCP_PORT2 990

memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("메세지가 올ip");
//serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //for the first_example
serv_addr.sin_port = htons(SERVICE_TCP_PORT);

memset(&serv_addr2, 0, sizeof(serv_addr2));
serv_addr2.sin_family = AF_INET;
serv_addr2.sin_addr.s_addr = inet_addr("메세지가 올 다른 ip");
//serv_addr2.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr2.sin_port = htons(SERVICE_TCP_PORT2);

if( ((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) ||
((sockfd2 = socket(AF_INET,SOCK_STREAM,0)) == -1) )
printf("error in sockfd\n");
else printf("socket ok\n");

if( (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0) ||
(setsockopt(sockfd2,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0) )
printf("setsockopt error\n");
else printf("setsockopt ok\n");

/*** 여 기 서 문 제 발 생 *****/
if((bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr)) == 0) ||
(bind(sockfd2,(struct sockaddr*)&serv_addr2, sizeof(serv_addr2)) == 0))
printf("binding is ok\n");
else
printf("bind no\n");
/*** 이 문장에서 bind no가 출력이 되서 나오더군요 *****/

if( (listen(sockfd, 8 ) == -1) || listen(sockfd2, 8 ) == -1 )
printf("listening is no\n");
else printf("listening is ok\n");

next의 이미지

305나 990번 포트 중 하나를 다른 프로그램에서 사용하고 있는것은 아닐까요?
errno 를 찍어 보시면 biding이 안되는 이유를 알 수 있을것 같은데요.

익명 사용자의 이미지

요청한 주소를 할당할수 없다는게 에러요인이라는건데 -_-;;;
이미 바인드가 안된다는 오류 자체가 이거 아닌가요?
제가 해석을 잘못한겐지...
헤더파일의 내용을 그대로 옮겨보자면
99번 에러번호는 cannot assign requested address라고 합니다. ㅜㅜ

익명 사용자의 이미지

if 문 안에서 || 연산자는 앞에 항만 참이면 뒤에 있는 항은 계산되지 않고 넘어갑니다.

어차피 하나만 참이면 되니까요.

익명 사용자의 이미지

죄송합니다. 잘 못 봤습니다.

lovemyin의 이미지

소스를 살펴보면 bind 전까지는 에러인 경우를 체크하셨는데 bind 하는 부분은 참일 때를 체크 하셨네요...
우선 코딩 하실 때 그런 일관성을 유지하시면 좋을것 같습니다.

그리고 위의 처럼 두개의 포트를 열어 listen을 하실 경우에 accept를 나중에 사용하실텐데 기본적으로 accept 함수는 블락 함수일텐데... 그 부분도 고려하셔야 될 것 같은데요...
멀티 스래도로 작성하시는 편이 좋을듯 싶습니다.

/***************************************************
* 가장 심플한 것이 가장 아름다운 것이다.
***************************************************/

댓글 달기

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