🧼C, C++/네트워크
소켓 기본 틀 #2
[[[ 서버 ]]]
#include <iostream>
#include <winsock2.h>
using namespace std;
#define PACKET_SIZE 1024
int main(){
WSADATA wsa;
if(WSAStartup(MAKEWORD(2,2), &wsa)!=0){
cout << "WSA error";
return 0;
}
SOCKET skt;
skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(skt==INVALID_SOCKET){
cout << "socket error";
closesocket(skt);
WSACleanup();
return 0;
}
SOCKADDR_IN addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(4444);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(skt, (SOCKADDR*)&addr,sizeof(addr))==SOCKET_ERROR){
cout << "bind error";
closesocket(skt);
WSACleanup();
return 0;
}
if(listen(skt,SOMAXCONN)==SOCKET_ERROR){
cout << "listen error";
closesocket(skt);
WSACleanup();
return 0;
}
SOCKADDR_IN client = {0};
int client_size = sizeof(client);
SOCKET client_sock;
client_sock = accept(skt,(SOCKADDR*)&client,&client_size);
if(client_sock==INVALID_SOCKET){
cout << "accept error";
closesocket(client_sock);
closesocket(skt);
WSACleanup();
return 0;
}
closesocket(client_sock);
closesocket(skt);
WSACleanup();
}
[[[ 클라이언트 ]]]
#include <iostream>
#include <winsock2.h>
using namespace std;
#define PACKET_SIZE 1024
int main(){
WSADATA wsa;
if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){
cout << "WSA error";
WSACleanup();
return 0;
}
SOCKET skt;
skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if(skt==INVALID_SOCKET){
cout << "socket error";
closesocket(skt);
WSACleanup();
return 0;
}
SOCKADDR_IN addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(4444);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
while(connect(skt, (SOCKADDR*)&addr,sizeof(addr)));
closesocket(skt);
WSACleanup();
}
'🧼C, C++ > 네트워크' 카테고리의 다른 글
소켓 구조체 정보 전송 C++ TCP (4) | 2020.10.16 |
---|---|
다중 클라이언트 C++ TCP #1 (0) | 2020.10.14 |
소켓 파일전송 C++ #1 (0) | 2020.10.06 |
소켓 기본 틀 #1 (0) | 2020.10.05 |
리버스 쉘 Reverse Shell C++ (0) | 2020.10.05 |
댓글