달력

52025  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31


// homrun.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"
#define PORT 8844
vector<SOCKET> client_list;
SOCKET s;
//HANDLE hMutex;
//bool bcheck;

void Socket_Add();
void Socket_Run();
DWORD WINAPI ConnectThread( void* p);

int _tmain(int argc, _TCHAR* argv[])
{
 Socket_Add();
    Socket_Run();
 return 0;
}


void Socket_Add()
{
// bcheck =false;
 WSADATA wsadata;
 
 if ( WSAStartup( MAKEWORD(2,2), &wsadata) != 0 )
 {
  printf("Can't Initialize Socket !\n");
  return ;
 }
// hMutex=CreateMutex(NULL,FALSE,NULL);

 s = socket( PF_INET, SOCK_STREAM, 0);

 SOCKADDR_IN addr = { 0 };
 addr.sin_family = AF_INET;
 addr.sin_port   = htons( PORT );
 addr.sin_addr.s_addr = htonl(INADDR_ANY);
 
 if ( bind( s, (SOCKADDR*)&addr, sizeof(addr)) == -1 )
 {
  printf("Can't bind \n"); return;
 }
 if ( listen( s, 5) == -1 )
 {
  printf("Can't Listen\n"); return;
 }
 printf("클라이언트를 대기합니다. \n");
}

void Socket_Run()
{
 while ( 1 )
 {
  SOCKADDR_IN c_addr;
  int  size = sizeof(c_addr);
  
  SOCKET c_s = accept( s, (SOCKADDR*)&c_addr, &size );
  printf("클라이언트 가 접속했습니다. IP : %s\n", inet_ntoa( c_addr.sin_addr));
  client_list.push_back( c_s );
  HANDLE hThread = CreateThread( 0, 0, ConnectThread, (void*)c_s, 0, 0);
  CloseHandle( hThread );
 }
}

DWORD WINAPI ConnectThread( void* p)
{
 SOCKET s = (SOCKET)p;

 char message;
 char  SendBuf[1024] = {0};
 int a[3];
 int nRead;
 int count[3];
 count[0]=0;
 int i=0;
 char s1[1024];
 char s2[1024];

 while ( 1 )
 {
  
  nRead = recv( s, &message, sizeof(message), 0);
  if ( nRead <= 0) break;
 

  switch( message)
  {
  case '1':

   srand(time(NULL));
   
   while(1)
   {
    a[0]=rand()%10;
    a[1]=rand()%10;
    a[2]=rand()%10;
    if(a[0]!=a[1] && a[0]!=a[2] && a[1]!=a[2])
     break;
   }
   printf("%d%d%d\n",a[0],a[1],a[2]);

   ZeroMemory(&SendBuf, sizeof(SendBuf));
   
   sprintf(SendBuf,"3자리 숫자를 넣어 주세요");
   send(s, SendBuf,sizeof(SendBuf),0);


   while(1)
   {
    
    nRead=recv(s, s1,sizeof(s1),0);
    if ( nRead <= 0) break;
    printf("%s\n",s1);
    count[0]=0;
    count[1]=0;
    count[2]=0;
    for(i=0;i<3;i++)
    {
     s2[i]=(int)s1[i]-48;
     if(a[i]==s2[i])
      count[1]++;
    }
    if(a[0]==s2[1])
     count[2]++;
    if(a[0]==s2[2])
     count[2]++;
    if(a[1]==s2[0])
     count[2]++;
    if(a[1]==s2[2])
     count[2]++;
    if(a[2]==s2[0])
     count[2]++;
    if(a[2]==s2[1])
     count[2]++;

    if(count[1]==0 && count[2]==0)
     count[0]=1;

    printf("%d%d%d\n",count[0],count[1],count[2]);
    for(i=0;i<3;i++)
    {
     s2[i]=(char)count[i]+48;
    }

    send(s, s2,sizeof(s2),0);
   
    if(count[1]==3)
    {
     printf("클리어 됨\n");
     break;
    }
   }
   break;
  case '2':

   break;
  }
  if(message=='3')
  {
   break;
  }
 }

 if(message=='3')
 {
  for( i = 0; i < client_list.size(); ++i )
  {
    if(client_list[i] == s)
    {
     vector<SOCKET>::iterator p = client_list.begin();
     p = p + i;
//     closesocket(client_list[i]);
     client_list.erase( p );
     printf("클라이언트가 접속을 종료하였습니다.\n ");
     break;
    }
  }
  closesocket(s);
 }
 return 0;
}

Posted by CokeBell
|