소켓 초기화 하는 부분은 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;
}
'개발 > C++' 카테고리의 다른 글
C++ Map, HashMap(unorder_map) 사용할 때 주의할 점 (0) | 2019.04.27 |
---|---|
C++ 가상 소멸자 (feat. 상속 생성자 소멸자 호출순서) (0) | 2019.04.22 |
C++ HashMap 구현해보기 (unorder_map) (0) | 2019.03.03 |
C++ 맵 구현해보기 (0) | 2019.02.23 |
C++ 리스트 구현해보기 (0) | 2019.01.26 |