본문 바로가기

개발/C++

C++ 소켓 에코 채팅 구현하기

소켓 초기화 하는 부분은 ip주소랑 포트 번호 빼고는 거의 고정이라고 보면 되는듯

 

 

클라이언트

#include "stdafx.h"
#include <iostream>
#include <winsock2.h>
#include <WS2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

const int BUFSIZE = 50;
int main()
{
	WSADATA  wsadata;
	SOCKET client_socket;
	SOCKADDR_IN client_address;

	int port = 5050;

	if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) {
		std::cout << "MAKEWORD error" << std::endl;
		return 0;
	}

	client_socket = socket(AF_INET, SOCK_STREAM, 0);

	if (client_socket == INVALID_SOCKET) {
		std::cout << "socket error" << std::endl;
		return 0;
	}

	memset(&client_address, 0, sizeof(client_address));
	client_address.sin_family = AF_INET;
	client_address.sin_port = htons(port);
	inet_pton(AF_INET, "127.0.0.1", &client_address.sin_addr.s_addr);

	if (connect(client_socket, (SOCKADDR*)&client_address, sizeof(client_address)) == SOCKET_ERROR) {
		std::cout << "connect error" << std::endl;
		return 0;
	}

	std::cout << "연결완료" << std::endl;
	while (true)
	{
		char message[BUFSIZE];
		int len;
		std::cout << "메시지 입력 : ";
		std::cin >> message;
		len = send(client_socket, message, sizeof(message), 0);
		if (len == -1) {
			std::cout << "send error" << std::endl;
			break;
		}
		else if (strcmp(message, "exit") == 0) {
			break;
		}
		memset(message, 0, sizeof(message));
		len = recv(client_socket, message, sizeof(message), 0);
		if (len == -1) {
			std::cout << "recv error" << std::endl;
			break;
		}
		std::cout << "서버에서 받은 메시지 : " << message << std::endl;
		memset(message, 0, sizeof(message));
	}

	closesocket(client_socket);
	WSACleanup();

	return 0;
}

 

에코서버

#include "stdafx.h"
#include <iostream>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")

const int BUFSIZE = 50;
int main()
{
	WSADATA wsadata;
	SOCKET server_socket, client_socket;
	SOCKADDR_IN server_address, client_address;

	int client_address_size;
	int port = 5050;

	if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) {
		std::cout << "MAKEWORD error" << std::endl;
		return 0;
	}

	server_socket = socket(AF_INET, SOCK_STREAM, 0);

	if (server_socket == INVALID_SOCKET) {
		std::cout << "socket error" << std::endl;
		return 0;
	}

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

	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr = htonl(INADDR_ANY);
	server_address.sin_port = htons(port);

	if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == SOCKET_ERROR) {
		std::cout << "bind error" << std::endl;
		return 0;
	}

	if (listen(server_socket, 5) == SOCKET_ERROR) {
		std::cout << "listen error" << std::endl;
		return 0;
	}
	client_address_size = sizeof(client_address);
	client_socket = accept(server_socket, (SOCKADDR*)&client_address, &client_address_size);

	if (client_socket == INVALID_SOCKET) {
		std::cout << "socket error" << std::endl;
		return 0;
	}

	std::cout << "연결완료" << std::endl;
	while (true)
	{
		char message[BUFSIZE];
		int len;
		len = recv(client_socket, message, sizeof(message), 0);
		if (len == -1) {
			std::cout << "send error" << std::endl;
			break;
		}
		else {
			std::cout << "클라이언트에서 받은 메시지 : " << message << std::endl;
			if (strcmp(message, "exit") == 0) {
				break;
			}
		}
		send(client_socket, message, sizeof(message), 0);
		memset(message, 0, sizeof(message));
	}

	closesocket(client_socket);
	closesocket(server_socket);
	WSACleanup();

	return 0;
}