[C++] TCP/IP 소켓 통신 예제
1. Sever, Client 각각 프로젝트 생성
2. C++에서 제공하는 Socket을 사용하기 위해 ws2_32.lib 추가.
3. Sever.cpp
#include<iostream>
#include<string>
#include<winsock2.h>
using namespace std;
void ShowErrorMessage(string message)
{
cout << "[오류발생]: " << message << '\n';
system("pause");
exit(1);
}
int main()
{
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
SOCKADDR_IN serverAddress, clientAddress;
int serverPort = 9876;
char received[256];
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) // Winsock을초기화합니다.
ShowErrorMessage("WSAStartup()");
serverSocket = socket(PF_INET, SOCK_STREAM, 0); // TCP 소켓을생성합니다.
if (serverSocket == INVALID_SOCKET)
ShowErrorMessage("socket()");
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); // 4바이트정수를네트워크바이트형식으로
serverAddress.sin_port = htons(serverPort); // 2바이트정수네트워크바이트형식으로
if (bind(serverSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
ShowErrorMessage("bind()");
cout << "[현재상태] bind()\n";
if (listen(serverSocket, 5) == SOCKET_ERROR)
ShowErrorMessage("listen()");
cout << "[현재상태] listen()\n";
int sizeClientAddress = sizeof(clientAddress);
clientSocket = accept(serverSocket, (SOCKADDR*)&clientAddress, &sizeClientAddress);
cout << "[현재상태] accept()\n";
if (clientSocket == INVALID_SOCKET)
ShowErrorMessage("accept()");
while (1) { // 클라이언트의메시지를받아서그대로다시전달합니다.
int length = recv(clientSocket, received, sizeof(received), 0);
received[length] = NULL;
cout << "[클라이언트메시지]: " << received << '\n';
cout << "[메시지전송]: " << received << '\n';
if (strcmp(received, "[exit]") == 0) {
send(clientSocket, received, sizeof(received) - 1, 0);
cout << "[서버종료]\n";
break;
}
send(clientSocket, received, sizeof(received) - 1, 0);
}
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
system("pause");
return 0;
}
4. Client.cpp
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<iostream>
#include<string>
#include<winsock2.h>
using namespace std;
void ShowErrorMessage(string message)
{
cout << "[오류발생]: " << message << '\n';
system("pause");
exit(1);
}
int main()
{
WSADATA wsaData;
SOCKET clientSocket;
SOCKADDR_IN serverAddress;
int serverPort = 9876;
char received[256];
string sent;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) // Winsock을초기화합니다.
ShowErrorMessage("WSAStartup()");
clientSocket = socket(PF_INET, SOCK_STREAM, 0); // TCP 소켓을생성합니다.
if (clientSocket == INVALID_SOCKET)
ShowErrorMessage("socket()");
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); // 문자열IP를네트워크바이트형식으로
serverAddress.sin_port = htons(serverPort); // 2바이트정수네트워크바이트형식으로
if (connect(clientSocket, (SOCKADDR*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
ShowErrorMessage("connect()");
cout << "[현재상태] connect()\n";
while (1) { // 반복적으로서버로메시지를전송하고에코메시지를전달받습니다.
cout << "[메시지전송]: ";
getline(cin, sent);
if (sent == "") continue;
send(clientSocket, sent.c_str(), sent.length(), 0);
int length = recv(clientSocket, received, sizeof(received), 0);
received[length] = '\0';
if (strcmp(received, "[exit]") == 0) {
cout << "[서버종료]\n";
break;
}
cout << "[서버메시지]: " << received << '\n';
}
closesocket(clientSocket);
WSACleanup();
system("pause");
return 0;
}
5. 실행화면.
출처 : https://velog.io/@underlier12/C-16-TCP-%EC%97%90%EC%BD%94-%ED%86%B5%EC%8B%A0
댓글
댓글 쓰기