#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define BUFSIZE 30
void ErrorHandling(char *message);
int main(int argc, char **argv){
WSADATA wsaData;
SOCKET hServSock;
char message[BUFSIZE];
int strLen,num=0;
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_DGRAM,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(4000);
if(bind(hServSock,(SOCKADDR*)&servAddr,sizeof(servAddr))==SOCKET_ERROR)
ErrorHandling("bind() error");
while(1){
timer = time(NULL);
t=localtime(&timer);
clntAddrSize = sizeof(clntAddr);
strLen = recvfrom(hServSock,message,BUFSIZE,0,(SOCKADDR*)&clntAddr,&clntAddrSize);
sendto(hServSock,message,strLen,0,(SOCKADDR*)&clntAddr,sizeof(clntAddr));
sendto(hServSock,(char *)t,sizeof(struct tm),0,(SOCKADDR*)&clntAddr,sizeof(clntAddr));
printf("클라이언트 IP주소 : %s\n",inet_ntoa(clntAddr.sin_addr));
printf("클라이언트 접속시간 : %2d:%02d:%02d\n\n",t->tm_hour,t->tm_min,t->tm_sec);
}
closesocket(hServSock);
WSACleanup();
return 0;
}
void ErrorHandling(char *message){
fputs(message,stderr);
fputc('\n',stderr);
exit(1);
}
'네트워크 프로그래밍' 카테고리의 다른 글
UDP 에코 서버 구축하기 (0) | 2009.09.18 |
---|---|
UDP daytime 에코 클라이언트 (0) | 2009.09.18 |
TCP/IP echo_client programming! (0) | 2009.09.18 |
TCP/IP echo server programming ! (0) | 2009.09.18 |
윈도우에서 클라이언트 구성하기 (0) | 2009.09.15 |