윈도우 || c언어 채팅프로그램

  • Post Author:
  • Post Category:C
  • Post Comments:1 Comment

SDK로 되어있는건데 참고 하세요

서버

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <process.h>

#define BUFSIZE 100



DWORD WINAPI ClientConn(void *arg);

void SendMSG(char* message, int len);
void ErrorHandling(char *message);



int clntNumber =0;

SOCKET clntSocks[10];
HANDLE hMutex;

int main(int argc, char **argv)

{


WSADATA wsaData;
SOCKET servSock;
SOCKET clntSock;

SOCKADDR_IN servAddr;
SOCKADDR_IN clntAddr;
int clntAddrSize;

HANDLE hThread;
DWORD dwThreadID;



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


}



if(WSAStartup(MAKEWORD(2, 2), &wsaData) !=0)
 ErrorHandling ("WSAStartup() error!");

hMutex = CreateMutex(NULL, FALSE, NULL);



if(hMutex==NULL)
 ErrorHandling ("CreateMutex() error");

servSock=socket(PF_INET, SOCK_STREAM,0); /* 서버 소켓 생성 */


if(servSock==INVALID_SOCKET)
 ErrorHandling ("socket() error");

memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
servAddr.sin_port=htons(atoi(argv[1]));



if(bind(servSock, (SOCKADDR*) &servAddr, sizeof(servAddr))==SOCKET_ERROR)
 ErrorHandling("bind() error");

if(listen(servSock, 5) == SOCKET_ERROR) /* 연결 요청 대기 상태 */
 ErrorHandling("listen() error");

while(1){
 clntAddrSize=sizeof(clntAddr);

 clntSock=accept(servSock, (SOCKADDR*) & clntAddr, &clntAddrSize); /* 연결 요청 수락 */


if(clntSock==INVALID_SOCKET)
  ErrorHandling("accept() error");

 WaitForSingleObject(hMutex, INFINITE);
 clntSocks[clntNumber++]=clntSock;
 ReleaseMutex(hMutex);



printf("새로운 연결, 클라이언트 IP : %s\n",
  inet_ntoa(clntAddr.sin_addr));

 hThread = (HANDLE)_beginthreadex(NULL, 0, ClientConn,(void*)clntSock, 0, (unsigned *)&dwThreadID);



if(hThread == 0)
  ErrorHandling("쓰레드 생성 오류");

}



WSACleanup();
return 0;

}



DWORD WINAPI ClientConn(void *arg)

{


SOCKET clntSock = (SOCKET)arg;
int strLen=0;
char message[BUFSIZE];
int i;



while((strLen=recv(clntSock, message, BUFSIZE, 0)) != 0)
 SendMSG(message, strLen);

WaitForSingleObject(hMutex, INFINITE);



for(i=0; i<clntNumber; i++) {
 if(clntSock == clntSocks[i]) {
  for(; i<clntNumber-1; i++)
   clntSocks[i] = clntSocks[i+1];
  break;


}


}

clntNumber--;
ReleaseMutex(hMutex);

closesocket(clntSock);

return 0;

}


void SendMSG(char* message, int len)

{


int i;
WaitForSingleObject(hMutex, INFINITE);


for(i=0; i<clntNumber; i++) 
 send(clntSocks[i],message,len,0);
ReleaseMutex(hMutex);

}



void ErrorHandling(char *message)

{


fputs(message, stderr);
fputc('\n', stderr);
exit(1);

}

 

클라이언트

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <process.h>

#define BUFSIZE 100
#define NAMESIZE 20



DWORD WINAPI SendMSG(void *arg);
DWORD WINAPI RecvMSG(void *arg);

void ErrorHandling(char *message);

char name[NAMESIZE]="[Default]";
char message[BUFSIZE];

int main(int argc, char **argv)
{
    WSADATA wsaData;
    SOCKET sock;
    SOCKADDR_IN servAddr;

    HANDLE hThread1,hThread2;
    DWORD dwThreadID1,dwThreadID2;



    if(argc!=4) {
        printf("Usage : %s <IP> <port>\n", argv[0]);
        exit(1);
    }



    if(WSAStartup(MAKEWORD(2, 2), &wsaData) !=0)
        ErrorHandling ("WSAStartup() error!");
    sprintf(name, "[%s]", argv[3]);

    sock=socket(PF_INET, SOCK_STREAM,0); /* 서버 소켓 생성 */

    if(sock==INVALID_SOCKET)
        ErrorHandling ("socket() error");

    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family=AF_INET;
    servAddr.sin_addr.s_addr=inet_addr(argv[1]);
    servAddr.sin_port=htons(atoi(argv[2]));



    if(connect(sock, (SOCKADDR*) &servAddr, sizeof(servAddr))==SOCKET_ERROR)
        ErrorHandling("connect() error");

    hThread1 = (HANDLE)_beginthreadex(NULL, 0, SendMSG, (void*)sock, 0, (unsigned *)&dwThreadID1);

    hThread2 = (HANDLE)_beginthreadex(NULL, 0, RecvMSG, (void*)sock, 0, (unsigned *)&dwThreadID2);

    if(hThread1==0 || hThread2==0)
       ErrorHandling("쓰레드 생성 오류");

    WaitForSingleObject(hThread1, INFINITE);
    WaitForSingleObject(hThread2, INFINITE);

    closesocket(sock);
    return 0;

}



DWORD WINAPI SendMSG(void *arg)

{


SOCKET sock = (SOCKET)arg;
char nameMessage[NAMESIZE + BUFSIZE];
while(1) {
 fgets(message, BUFSIZE, stdin);
 sprintf(nameMessage,"%s %s", name, message);


if(!strcmp(message, "q\n")) {
  closesocket(sock);
  exit(0);


}

 send(sock, nameMessage, strlen(nameMessage), 0);

}

}


DWORD WINAPI RecvMSG(void *arg)



{

SOCKET sock = (SOCKET)arg;
char nameMessage[NAMESIZE + BUFSIZE];
int strLen;

while(1) {
 strLen= recv(sock, nameMessage, NAMESIZE+BUFSIZE-1,0);

 if(strLen==-1) return 1;

 nameMessage[strLen]=0;
 fputs(nameMessage, stdout);


}

}



void ErrorHandling(char *message)
{
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}

 

This Post Has One Comment

Leave a Reply