accept()사용시 에러

hongkuny의 이미지

아례와 같이 별다른 생각없이 accept함수를 사용했는데 컴파일시 에러가 남니다.

아무리 생각해 보아도 잘못한건 없어보이고요....

제가 간과하고 넘어간 부분이든지 잘못된 부분이 보이시면

부탁드립니다...


		if(FD_ISSET(pthis->m_server,&readset)){
			
			int client;
			int size = sizeof(pthis->client_addr);
			
			client =  accept(pthis->m_server,(struct sockaddr*)&pthis->client_addr,&size);
	
			pthis->m_client.push_back(client);

			cout<<client<<"가 접속 하였다"<<endl;
			
			int addrlen=16;
	
			getsockname(client,(sockaddr *)&pthis->client_addr,&addrlen);
			cout<<"ip address :"<<inet_ntoa(pthis->client_addr.sin_addr)<<endl;
			
			cout<<"현제 총 접속자 :"<<pthis->m_client.size()<<endl;
			continue;
		}		



에러.//

serversock.h: In function `void *ServerSock::thread_proc (void *)':
serversock.h:109: cannot convert `int *' to `socklen_t *' for argument
`3' to `accept (int, sockaddr *, socklen_t *)'
serversock.h:117: cannot convert `int *' to `socklen_t *' for argument
`3' to `getsockname (int, sockaddr *, socklen_t *)'
[/code]
liongo의 이미지

int size = sizeof(pthis->client_addr);

혹시 이부분을
socklen_t size = sizeof(pthis->client_addr);

로 하면 되지 않을까요?

' 형식이 내용을 규정한다. '

mach의 이미지

liongo wrote:
int size = sizeof(pthis->client_addr);

혹시 이부분을
socklen_t size = sizeof(pthis->client_addr);

로 하면 되지 않을까요?

첨언하자면, C++은 "Strong Type Checking"을 합니다.

즉, C 에서 처럼 대충(?) casting하지 않는다는 말이지요.(어쩌면 이게 C의 매력인지도......)
C++에서 cast연산자가 다수개 존재하는것도 유념하시고요.
C++답게 코딩하셔야 합니다. 적어도 타입이라도 일치시키는 훈련을 가지세요.

결론적으로, 현재 hongkuny님의 코드는 accept()문제라기 보다는 Type 문제입니다.
구체적으로는 accept()에서의 파라메터 타입검사지요.
꼭, accept()뿐만이 아니라, 임의의 함수에서도 마찬가지입니다.
즉, 특정함수사용시 man 함수명하셔서 함수 프로토타입보시고, 타입맞추어서
호출하도록하는 습관을 들이세요.
표준 함수(System Call, Library API)의 경우, 대체로 많은 시스템이 동일한 파라메터 타입을 가지지만, 특정 운영체제 포팅시에
파라메터를 검사해 보는것도 좋은 습관입니다. 다소 차이가 있을수있기때문에요.
불가피하게 캐스팅이 필요하다면, C++스타일로 캐스팅하도록하고요.
C에서 C++로 전환(?)하는 코딩하실때 첫번째 유의사항 "강한 타입 검사" 이를 명심하세요.

------------------ P.S. --------------
지식은 오픈해서 검증받아야 산지식이된다고 동네 아저씨가 그러더라.

댓글 달기

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