티스토리 뷰
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <time.h>
#include <fstream>
#include <iostream>
#include <fcntl.h>
#include <io.h>
#define MAXLINE 1000
#pragma warning(disable: 4996)
int main()
{
// Initialize Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
printf("Server: Error at WSAStartup().\n");
// Create a SOCKET for listening for incoming connection requests.
SOCKET sockListen;
sockListen = socket(AF_INET, SOCK_STREAM, 0);
if (sockListen == INVALID_SOCKET)
{
printf("Server: Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 0;
}
// The sockaddr_in structure specifies the address family,
// IP address, and port for the socket that is being bound.
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(13); /* daytime server */
if (bind(sockListen, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
{
printf("Server: bind() failed.\n");
closesocket(sockListen);
return 0;
}
// Listen for incoming connection requests on the created socket
if (listen(sockListen, 1) == SOCKET_ERROR)
printf("Server: listen(): Error listening on socket.\n");
printf("Server: I'm listening on socket, waiting for connection...\n");
printf("socket file descriptor : %d\n", sockListen);
int file = open("descriptor.txt", O_RDONLY);
if (file == 0) {
printf("파일을 찾을 수 없습니다\n");
return 0;
}
else {
printf("txt file descriptor : %d\n", file);
close(file);
}
while (1)
{
char buff[MAXLINE];
time_t ticks;
// Accept connection
SOCKET sockConn = accept(sockListen, NULL, NULL);
// Get Time and send to Client
ticks = time(NULL);
sprintf(buff, "%.24s\r\n", ctime(&ticks));
send(sockConn, buff, strlen(buff), 0);
closesocket(sockConn);
}
WSACleanup();
return 0;
}
'C, C++' 카테고리의 다른 글
[C++] DayTime server/client (0) | 2020.03.11 |
---|---|
[C] Byte ordering check (0) | 2020.03.11 |