다중 클라이언트 처리 서버

orionbelt의 이미지

#include <stdio.h>
#include <winsock2.h>
 
#pragma comment(lib, "wsock32.lib")
 
char buff[255];
int len = 0;
int val;
 
DWORD WINAPI thread(LPVOID socket)
{
	SOCKET cs = (SOCKET)socket;
	struct sockaddr_in client;
	int size = sizeof(client);
	getpeername(cs, (struct sockaddr*)&client, &size);
 
	while(1)
	{
		memset(&buff, 0, sizeof(buff));
		printf("command : ");
		if(fgets(buff, 255, stdin) == NULL)
		{
			printf("Command Failure...\n");
			continue;
		}
 
		len = strlen(buff);
		if(len == 1)
		{
			printf("Please reenter the command...\n");
			continue;
		}
		else if(buff[len-1] == '\n') buff[len-1] = '\0';
 
		if(!strcmp(buff, "dir"))
		{
			printf("adf");
			printf("Connected Client [ IP : %s / port : %d ]\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
		}
 
 
		len = strlen(buff);
		val = send(cs, buff, len, 0);
		if(val == -1)
		{
			printf("Data transmission failure\n");
			break;
		}
		else if(val == 0) break;
	}
 
	if(closesocket(cs) == 0)
	{
		printf("Client End [ IP : %s / port : %d ]\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
	}
	return 0;
}
 
int main(int argc, char *argv[])
{
	while(1)
	{
		cs = accept(s, (struct sockaddr*)&client, &size);
		if(cs == INVALID_SOCKET)
		{
			printf("Client connection failure...\n");
			closesocket(s);
			WSACleanup();
			return -1;
		}
 
		printf("Connected Client [ IP : %s / Port : %d ]\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
 
		HANDLE thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, (LPVOID)cs, 0, NULL);
		if(thread1 == NULL)
		{
			printf("Create Thread Error\n");
			break;
		}
	} //while문 종료

=============================================================================
안녕하세요 c++에서 코드를 작성하는데
구현은 서버에서 클라이언트로 명령을 보내는 형식입니다.
문제는 클라이언트가 한명 이상이면 서버에서 명령이 한번은 A에게 또 한번은 B에게 가지는것인데.
이것은 내가 원하는 A한테만 보내다가 B한테 보내고 할수 있게하는것입니다.

그리고 또 서버에서 현재 접속중인 클라이언트의 목록을 보고 싶은데 어떻게 모니터링하는 쓰레드를 만들어야할지
알려주싶시오ㅜㅜ

snswin의 이미지

각 Thread는 한 사용자에 대응합니다.
두 사용자에 대한 Thread가 각각 fgets에서 입력을 대기하면
하나의 fgets만 동작할것으로 예상됩니다.
따라서 사용자 Thread에서 fgets을 제거하고
fgets을 할 thread를 별도로 만들어서 원하는 사용자(Thead)가 해당 명령을 처리하도록 처리하면 될 것입니다.
또한 fgets용 thread에서 특정 명령을 입력 받으면 사용자 목록을 출력하도록 하면 될 것 같습니다.

-------------------------------------------
진실을 모르는 것은 어둡고 깊은 산에 혼자 있는 것과 같다.
어둡고 깊은산에 혼자 있어도 두렵지 않다면, 나는 이미 진실을 알고 있는 것이다.

orionbelt의 이미지

님이 말씀하신데로 fgets를 다른 쓰레드를 만들었습니다.
메인 쓰레드말고 thread() 쓰레드에서 새로운 쓰레드를 선언 하고 거기에다 fgets을 했습니다.

댓글 달기

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