소켓 기초프로그래밍중에 요상한 에러가...있습니다.

yakur의 이미지

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>


#define MYPORT 5002
#define saram 10

main()
{
	char buf[10];
	int sf,nf,sin_size;
	struct sockaddr_in my_addr;
	struct sockaddr_in to_addr;
	if((sf=socket(AF_INET, SOCK_STREAM, 0))==-1) {
		perror("socket");
		exit(1);
	}
	
	my_addr.sin_family=AF_INET;
	my_addr.sin_port=htons(MYPORT);
	my_addr.sin_addr.s_addr=inet_addr("210.102.139.250");
	bzero(&(my_addr.sin_zero),8);

	if(bind(sf,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1) {
		perror("bind");
		exit(1);
	}
	
	if(listen(sf,saram)==-1) {
		perror("listen");
		exit(1);
	}


	//close(0);
	while(1) {
		sin_size=sizeof(struct sockaddr_in);
		if((nf=accept(sf,(struct sockaddr *)&to_addr,&sin_size) == -1)) {
				perror("accept");
				continue;
		}

			
		
		printf("server: got connection from %s[%d]\n",
				inet_ntoa(to_addr.sin_addr), nf);
		
		if (!fork()) {
			if (send(nf,"안녕?\n",10,0)==-1)
				perror("send");

			close(nf);
			exit(0);
		}
		close(nf);
		while (waitpid(-1,NULL,WNOHANG) > 0);
	
	}
}

안녕하세요? 매일 글만 읽다 올리는건 첨이네요 헤헤
이번에 첨 소켓 프로그래밍 공부하면서 beeJ 라는 문서보고 하고 있는데요/
거기 예제를 해보니까

요렇게 코드를 써서 실행시키면 처음 접속할때
send: Socket operation on non-socket
이라고 에러가 납니다. 그러나 그 이후에 텔넷에서 접속하면 정상적으로 "안녕?"
이 출력되고요 소켓지시자 nf (accept에 연결된) 을 출력해보면 0 이 출력되는데
제생각에는 0번이 프로세스에서 표준 입력 지시자?! 라고 알고있는데요.
프로세스가 활용하는 것을 소켓에서 쓰려고 해서 이런문제가 발생하는걸까
추정하고 close(0); 을 써서 아예 0을 nf 가 쓸수있게 미리 닫아놨더니 제대로 되더라구요.
이런 방법 말고 정확히 왜그런지 그리고 어떻게 해결할수 있는지 알고 싶네요.
이렇게 처리해버리면 소켓 안에서 표준입력이 않먹어요 ㅠㅠ

그리고 환경 문제인줄 알았는데 다른 유닉스 환경에서 컴파일해도 마찬가지더군요. OTL
[/code]

mithrandir의 이미지

Quote:

if((nf=accept(sf,(struct sockaddr *)&to_addr,&sin_size) == -1)) {
perror("accept");
continue;
}

요부분은

if((nf=accept(sf,(struct sockaddr *)&to_addr,&sin_size)) == -1) {
perror("accept");
continue;
}

로 써야합니다.

언제나 삽질 - http://tisphie.net/typo/
프로그래밍 언어 개발 - http://langdev.net

yakur의 이미지

이상하다...이상하다... 하면서 어제 4시간째 헤매였던게...
소괄호 하나 문제였다니..
허탈하군요...
ㅠㅠ
답변고맙습니다 ^^

이제와 항상 영원히~

댓글 달기

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