티스토리 뷰

C, C++

[C++] DayTime server/client

Dev.sohee 2020. 3. 11. 02:59
   
Server)


#include <stdio.h>

#include <stdlib.h>

#include <winsock.h>

#include <time.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");

 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;



​

Client)


#include <stdio.h>

#include <stdlib.h>

#include <winsock.h>

#include <time.h>

#define MAXLINE 1000

int main()

{

 // Initialize Winsock

 WSADATA wsaData;

 int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

 if (iResult != NO_ERROR)

 printf("Client: Error at WSAStartup().\n");

 // Create a SOCKET

 SOCKET sockClient;

 sockClient = socket(AF_INET, SOCK_STREAM, 0);

 if (sockClient == INVALID_SOCKET)

 {

 printf("Client: 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;

 char servHost[16];

 memset(&servAddr, 0, sizeof(servAddr));

 printf("Enter Host IP: ");

 scanf_s("%s", &servHost, sizeof(servHost));

 servAddr.sin_family = AF_INET;

 servAddr.sin_addr.s_addr = inet_addr(servHost);

 servAddr.sin_port = htons(13); /* daytime server */

 // Connect to a server.

 if (connect(sockClient, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)

 {

 printf("Client: connect() - Failed to connect.\n");

 WSACleanup();

 return 0;

 }

 // Read data from server and display

 while (1)

 {

 int bytesRecv;

 char recvbuff[MAXLINE];

 bytesRecv = recv(sockClient, recvbuff, MAXLINE, 0);

 if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)

 {

 printf("Client: Connection Closed.\n");

 break;

 }

 else {

 recvbuff[bytesRecv] = 0;

 printf("Server Day Time: %s \n", recvbuff);

 break;

 }

 }

 closesocket(sockClient);

 WSACleanup();

 return 0;

}

'C, C++' 카테고리의 다른 글

[C] Byte ordering check  (0) 2020.03.11
[C++] TCP socket_file descriptor, text file descriptor  (0) 2020.03.11
공지사항
최근에 올라온 글
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함