select 채팅서버 질문입니다.

njk0308의 이미지

지금 하고있는데 잘 안풀리네요 ㅠㅠ
select 사용 하고 있는데 ㅠ 왜안되는건지 잘 모르겠어요 ㅠㅠㅠㅠ
read부분의 printf가 안뜨는걸 보니 그쪽으로 안가는거 같기도 하구요 ㅠ
계속 타임아웃만 나옵니다 ㅠㅠ

도와주세요 ㅠㅠ

#include

#include

#include

#include

#include

#include

#include

#include

#define BUF_SIZE 100

void error_handling(char *message);

int main(int argc, char *argv[])

{

int serv_sock, clnt_sock;
struct sockaddr_in serv_addr, clnt_adr;
struct timeval timeout;
fd_set reads, cpy_reads;

socklen_t adr_sz;
int fd_max, str_len, fd_num, i;
char buf[BUF_SIZE];
int sel;

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

}

serv_sock=socket(PF_INET,SOCK_STREAM,0);

serv_addr.sin_family=AF_INET;

serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port=htons(atoi(argv[1]));

if(bind(serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)))

error_handling("Bind() Error");

if(listen(serv_sock, 5)==-1)

error_handling("Listen() Error");

printf("Welcome to MyChat \n");

FD_ZERO(&reads);

FD_SET(serv_sock,&reads);

fd_max = serv_sock;

while(1){
cpy_reads=reads;
timeout.tv_sec=5;
timeout.tv_usec=5000;

fd_num=select(fd_max+1, &cpy_reads, 0, 0, &timeout);
if(fd_num==-1)
{
error_handling("select() error");
break;
}
else if(fd_num==0)
{
puts("time out!");
}
else
{
for(i = 0; i {
if(FD_ISSET(i,&cpy_reads))
{
if(i==serv_sock)
{
adr_sz=sizeof(clnt_adr);
clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_adr, &adr_sz);
FD_SET(clnt_sock, &reads);
printf("accept......!!!!");
}

else
{
str_len=read(clnt_sock, buf, BUF_SIZE);
printf("read...!!");

if(str_len >0)
{
buf[str_len] =0;
printf("From clnt : %s",buf);
}
else
{
fgets(buf, BUF_SIZE, stdin);
write(clnt_sock, buf, BUF_SIZE);
}
}
}
}
}

}
close(serv_sock);
return 0;

}

void error_handling(char *message)

{

fputs(message,stderr);

fputc('\n',stderr);

exit(1);

}

nthroot의 이미지

두가지가 빠졌어요..

하나는 select 에서 리턴이 되면 해당 소켓이 메인소켓이면(if) accept를 호출하셔야 해요.
연결된 소켓이면 채팅 내용을 read 하면 되겠죠.
그리고 select 에서 사용하는 fdset 은 FDSET으로 매번 다시 설정을 하셔야 해요.

------식은이 처------
길이 끝나는 저기엔 아무 것도 없어요. 희망이고 나발이고 아무 것도 없어.

njk0308의 이미지

이게 그냥 붙여넣기 했더니
옆으로 갔던 애들이 안보였네요 ㅠㅠㅠ 다시한번만 봐주세요 ㅠㅠ 죄송합니당 ㅠㅠ

댓글 달기

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