Posted
Filed under 공부한 것들/C/C++
#include 
#include 
#include 
#include 

#define BUFSIZE 1024

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

int main(int argc, char **argv){
	WSADATA	wsaData;
	SOCKET	hServSock;
	SOCKET	hClntSock;
	char	message[BUFSIZE];
	int	strlen;

	SOCKADDR_IN	servAddr;
	SOCKADDR_IN	clntAddr;
	int clntAddrSize;

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

	/*윈속 초기화*/
	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(atoi(argv[1]));

	if(bind(hServSock, (SOCKADDR *)&servAddr, sizeof(servAddr))==SOCKET_ERROR)
		ErrorHandling("bind() Error!!");

	if(listen(hServSock, 5)==SOCKET_ERROR)
		ErrorHandling("listen() Error!!");

	clntAddrSize=sizeof(clntAddr);
	hClntSock=accept(hServSock, (SOCKADDR *) &clntAddr, &clntAddrSize);
	if(hClntSock==INVALID_SOCKET)
		ErrorHandling("accept() Error!!");

	while((strlen=recv(hClntSock, message, BUFSIZE, 0)) !=0){
		send(hClntSock, message, strlen, 0);
	}
	closesocket(hClntSock);
	WSACleanup();
	return 0;
}
소스 설명은 추후에
2008/12/22 18:15 2008/12/22 18:15