#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<winsock2.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib")
#define BUFSIZE 1024
void ErrorHandling(char *message);
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET hServSock;
SOCKET hClntSock;
char message[BUFSIZE];
int strLen;
time_t timer;
struct tm *t;
SOCKADDR_IN servAddr;
SOCKADDR_IN clntAddr;
int clntAddrSize;
if(WSAStartup(MAKEWORD(2,2),&wsaData) !=0)
ErrorHandling("WSAStartup() error!");
hServSock=socket(PF_INET, SOCK_STREAM,0);
if(hServSock == 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(5050);
if(bind(hServSock, (SOCKADDR*) &servAddr, sizeof(servAddr))==SOCKET_ERROR)
ErrorHandling("bind() error");
if(listen(hServSock, 5)==SOCKET_ERROR)
ErrorHandling("listen() error");
while(1){
clntAddrSize=sizeof(clntAddr);
hClntSock=accept(hServSock, (SOCKADDR*)&clntAddr, &clntAddrSize);
if(hClntSock==INVALID_SOCKET)
ErrorHandling("accept() error");
while(1){
strLen=recv(hClntSock, message, BUFSIZE, 0);
message[strLen]=0;
printf("클라이언트로부터 전송된 메시지 : %s \n", message);
send(hClntSock, message, strLen, 0);
}
printf("클라이언트 IP주소 : %s\n",inet_ntoa(clntAddr.sin_addr));
timer = time(NULL);
t=localtime(&timer);
printf("클라이언트 접속시간 : %2d:%02d:%02d\n\n",t->tm_hour,t->tm_min,t->tm_sec);
closesocket(hClntSock);
}
closesocket(hClntSock);
WSACleanup();
return 0;
}
void ErrorHandling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
'네트워크 프로그래밍' 카테고리의 다른 글
UDP daytime 에코서버 프로그래밍! (0) | 2009.09.18 |
---|---|
TCP/IP echo_client programming! (0) | 2009.09.18 |
윈도우에서 클라이언트 구성하기 (0) | 2009.09.15 |
윈도우에서 서버 구축하기 (0) | 2009.09.15 |
FILE * pfile = fopen(pfilename, "rt"); 에서 "rt"부분 설명 (0) | 2009.09.15 |