소켓 프로그래밍 질문있습니다. Address already in use 때문에요 .

조우리의 이미지

#include
#include
#include
#include
#include
#include
#include

#define MAXBUF 256
#define TRUE 1

int main()
{
int ssock, csock; // 소켓 디스크립트 정의
int clen;
struct sockaddr_in client_addr, server_addr; // 주소 구조체 정의
char buf[MAXBUF] = "I like you!"; // 클라이언트에 보내줄 문자열

// 서버 소켓 생성
if ((ssock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("socket error : ");
exit(1);
}

// int optvalue=TRUE; 이부분은 책에 있는 예제를 쓴거구요. 안되서 주석처리를;;
// int optlen=sizeof(optvalue);
// 옵션주기
//setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &optvalue, optlen);

//여기 게시판 글 검색하다가 본걸로 써봤어요.

int nOn = 1;

if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, (char *)&nOn, sizeof(nOn)) < 0)
{
perror("setsockopt");
close(sockfd);
return -1;
}

clen = sizeof(client_addr);

// 주소 구조체에 주소 지정
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(2117);

// bind 함수를 사용하여 서버 소켓의 주소 설정
if (bind(ssock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind error : ");
exit(1);
}

// 위에서 지정한 주소로 클라이언트 접속을 기다림
if (listen(ssock, 8) < 0) {
perror("listen error : ");
exit(1);
}

while(1) {

// 클라이언트가 접속하면 접속을 허용하고 클라이언트 소켓을 생성함
csock = accept(ssock, (struct sockaddr *)&client_addr, &clen);

// 클라이언트로 buf에 있는 “I like you!" 문자열 전송
if (write(csock, buf, MAXBUF) <=0)
perror("write error : ");

// 클라이언트 소켓을 닫음
close(csock);
}

return 0;
}

============================================================
어디가 잘못 됐는지 잘 모르겠어서요.
위치가 잘못된 건가;;;;ssock소켓 생성 후 아래에 bind 함수선언 위에가 아닌가요

ㅜㅜ 웅...

익명 사용자의 이미지

man setsockopt

참고 : SO_REUSEADDR(enable/disable local address reuse)

alwaysrainy의 이미지

netstat -na 쳐보세요 ~
방금 사용하셨던 포트라면 TIME_WAIT 상태에 있을
확률이 높습니다 ~ 윗분이 써주신 옵션을 사용하세요

---------------------------------------
세계는 넓고, 할일은 많다.

---------------------------------------
세계는 넓고, 할일은 많다.

조우리의 이미지

int nOn = 1;

if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, (char *)&nOn, sizeof(nOn)) < 0)
{
perror("setsockopt");
close(sockfd);
return -1;
}

SO_REUSEADDR이 옵션 적어서 넣었어요. 위에처럼 쓰면 되는거 맞지 않나요?

조우리의 이미지

TCP가 TIME_WAIT 상태로 넘어가지 않는군요.
;;;;;;
하핫;;;;

좀 이상하네요.. 책에 있는 예제 그대로인데;;; 흠...

댓글 달기

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