#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<winsock2.h>
#include<time.h>
#pragma comment(lib,"ws2_32.lib")
#define BUFSIZE 30
void ErrorHandling(char *message);
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET hSocket;
char message[BUFSIZE];
int strLen,addrSize,i;
SOCKADDR_IN servAddr;
SOCKADDR_IN fromAddr;
time_t timer = time(NULL);
struct tm* t = localtime(&timer);
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
ErrorHandling("WSAStartup() error!");
hSocket=socket(PF_INET,SOCK_DGRAM,0);
if(hSocket ==INVALID_SOCKET)
ErrorHandling("socket() error");
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servAddr.sin_port=htons(4000);
if(connect(hSocket, (struct sockaddr*)&servAddr, sizeof(servAddr))==SOCKET_ERROR)
ErrorHandling("connect() error!");
while(1)
{
fputs("Daytime 서버에 접속하시겠습니까(y/n) : ", stdout);
fgets(message, BUFSIZE, stdin); fflush(stdin);
if(!strcmp(message,"y\n")) {
sendto(hSocket,message,sizeof(message),0,(SOCKADDR*)&servAddr,sizeof(servAddr));
addrSize = sizeof(fromAddr);
strLen = recvfrom(hSocket,message,BUFSIZE,0,(SOCKADDR*)&fromAddr,&addrSize);
recvfrom(hSocket,(char *)t,sizeof(struct tm),0,(SOCKADDR*)&fromAddr,&addrSize);
message[strLen]=0;
printf("서버로부터 전송된 메시지 : ");
printf("%4d년%4d월%4d일%4d시%4d분\n",
t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);
}
else if (!strcmp(message,"n\n")) break;
else continue;
}
closesocket(hSocket);
WSACleanup();
return 0;
}
void ErrorHandling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
'네트워크 프로그래밍' 카테고리의 다른 글
UDP 에코 클라이언트 구축하기 (0) | 2009.09.18 |
---|---|
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 |