초보 도와주실분 구합니다.

익명 사용자의 이미지

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

#define SERV_IP "220.149.128.100"
#define SERV_PORT 4202
#define BACKLOG 10

#define INIT_MSG "====================\nHello! I'm Chatting Server...\nPlease, LOG-IN!\n===================\n"
#define USER1_ID "user1"
#define USER1_PW "passwd1"
#define USER2_ID "user2"
#define USER2_PW "passwd2"

#define MAX_SOCK 5
#define BUFSIZE 512

#define EXIT "/exit"

int GetMax(int);

int client_s[MAX_SOCK];
int num = 0;

int main(void)
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
unsigned sin_size;
int rcv_byte;
int val = 1;
char buf[512];
char id[20];
char pw[20];
char msg[512];
char recvBuffer[512];

fd_set read_fds;
int maxfdp1;
int i, j, n;

sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
perror("Server-socket() error lol!");
exit(1);
}
else printf("Server-socket() sockfd is OK...\n");

bzero((char *)&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERV_PORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

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

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("Server-bind() error lol!");
exit(1);
}
else printf("Server-bind() is OK...\n");

if (listen(sockfd, BACKLOG) == -1)
{
perror("listen() error lol!");
exit(1);
}
else printf("listen() is OK...\n\n");

maxfdp1 = sockfd + 1;
printf("====================\nHello! I'm Chatting Server...\nPlease, LOG-IN!\n====================\n");

while(1)
{
msg[0] = '\0';
recvBuffer[0] = '\0';

FD_ZERO(&read_fds);
FD_SET(sockfd, &read_fds);
FD_SET(0, &read_fds);

for(i=0; i maxfdp1 = GetMax(sockfd) + 1;

if (select(maxfdp1, &read_fds, (fd_set *)0, (fd_set *)0, NULL) == -1)
{
printf("Server-select() error lol!");
exit(1);
}


if (FD_ISSET(sockfd, &read_fds))
{
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1)
{
printf("Server-accept error lol! \n");
exit(1);
}

send(new_fd, INIT_MSG, strlen(INIT_MSG) + 1, 0);
rcv_byte = recv(new_fd, id, sizeof(buf), 0);

//client_s[num] = new_fd;

//n = recv(new_fd, recvBuffer, 512, 0);
//recvBuffer[n] = '\0';
//sprintf(id, "%s", recvBuffer);

if((strcmp(id, USER1_ID)) == 0)
{
send(new_fd, "PW : ", sizeof(pw), 0);
rcv_byte = recv(new_fd, pw, sizeof(buf), 0);

/* n = recv(new_fd, recvBuffer, 512, 0);
recvBuffer[n] = '\0';
sprintf(pw, "%s", recvBuffer);*/
if ((strcmp(pw, USER1_PW)) == 0)
{
send(new_fd, "Log-in success! - Welcome to GTalk\n========== Chatting Room ==========\n", sizeof(msg), 0);
printf("Log-in success! [user1] - Welcome to GTalk\n");
}
else
{
send(new_fd, "PW error!", sizeof(msg), 0);
printf("PW error!\n==========\n");
}
}
else if((strcmp(id, USER2_ID)) == 0)
{
send(new_fd, "PW : ", sizeof(pw), 0);
rcv_byte = recv(new_fd, pw, sizeof(buf), 0);

//n = recv(new_fd, recvBuffer, 512, 0);
// recvBuffer[n] = '\0';
// sprintf(pw, "%s", recvBuffer);
if ((strcmp(pw, USER2_PW)) == 0)
{
send(new_fd, "Log-in success! - Welcome to GTalk\n========== Chatting Room ==========\n", sizeof(msg), 0);
printf("Log-in success! [user2] - Welcome to GTalk\n");
printf("========== Chatting Room ==========\n");
}
else
{
send(new_fd, "PW error!", sizeof(msg), 0);
printf("PW error!\n==========\n");
}
}
else
{
send(new_fd, "ID error!", sizeof(msg), 0);
printf("ID error!\n==========\n");
}
//client_s[num] = new_fd;
//n = recv(new_fd, recvBuffer, 512, 0);
//recvBuffer[n] = '\0';
//sprintf(id, "%s", recvBuffer);

//n = recv(new_fd, recvBuffer, 512, 0);
//recvBuffer[n] = '\0';
//sprintf(pw, "%s", recvBuffer);

getsockname(new_fd, (struct sockaddr *)&their_addr, &sin_size);

num++;
}

for(i=0; i {
if (FD_ISSET(client_s[i], &read_fds))
{
if ((n = recv(client_s[i], recvBuffer, BUFSIZE, 0)) <= 0)
{
close(client_s[i]);
if(i != num - 1)
{
client_s[i] = client_s[num - 1];
}
num--;

continue;
}
recvBuffer[n] = '\0';

if (strstr(recvBuffer, EXIT) != NULL)
{
close(client_s[i]);
if (i != num - 1)
{
client_s[i] = client_s[num - 1];
}
num--;
}

for(j = 0; j < num; j++)
{
if (i != j)
send(client_s[j], recvBuffer, n, 0);
}
printf("%s", recvBuffer);
}
}
}
}

int GetMax(int i)
{
int max = i;
int j;
for(j = 0; j {
if(client_s[j] > max) max = client_s[j];
}
return max;
}

댓글 달기

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