🧼C, C++/자체제작 프로그램및 배포

codeSock <v2.0> 라이브러리 배포 C++

Mawile 2020. 11. 12.
728x90

본 글은 "codeSock"라이브러리 함수들에

대한 설명과 배포를 위한 글입니다.

 

 

개발환경 >> DevCpp

언어 >> C++11

운영체제 >> Windows10

 

 


 

지난 버전

 

codeSock 헤더파일 배포 <1.0> C++

시작하기 전 이 헤더 파일은 Windows10 운영체제를 사용, IDE는 DevCpp를 사용, 언어는 C++11을 사용했음을 알려드립니다 안녕하세요! 이번에는 저가 소켓을 쉽게 만들도록 도와주는 C++ 기반의 소켓 헤

mawile.tistory.com

 

 


 

시작하며...

 

 

 

안녕하세요!! 드디어!!!

codeSock라이브러리 제작을 완료했습니다!!

우선 해당 라이브러리는 ws2_32 관련 링커가 연결되야지 정상 작동합니다...!!

확장자는. h(헤더 파일)입니다

 

라이브러리 파일 다운로드는

아래 링크에서 다운로드 가능합니다~

 

DRAGONPROCESS/codeSock-v2.0-libraries

Contribute to DRAGONPROCESS/codeSock-v2.0-libraries development by creating an account on GitHub.

github.com

 


TCP이름 공간 사용

use an namespace TCP

 

 

using namespace TCP;

 

 

데이터 패킷 크기 설정

Data Packet Size

 

char hello[PACKET_SIZE]={0};

 

 

 

메인 함수 마지막 줄에 브레이크 포인트

BreakPoint on final line of main function

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	
	while(1);
}

 

 

 

 

소켓 생성 [ 서버 ]

create a [ server socket ]

 

 

SERVER(int, int);

SERVER([ServerPort], [Options]);

 

서버 옵션 매크로 (ServerSocket Options Macro)

SINGLE - 단일 클라이언트 서버 생성(Create a single clientServer)

MULTI - 다중 클라이언트 서버 생성(Create a multi clientServer)

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,SINGLE);
    
    while(1);
}

 

 

 

소켓 생성 [ 클라이언트 ]

create a [ client socket ]

 

 

CLIENT(const char*,int, int, int);

CLIENT([ServerIp], [ServerPort], [Options], [ConnectionOption]);

 

클라이언트 옵션 매크로 (ClientSocket Options Macro)

SINGLE - 단일 클라이언트 생성(Create a single client)

MULTI - 다중 클라이언트 생성(Create a multi client)

 

클라이언트 접속 옵션 매크로 (ClientSocket ConnectionOption Macro)

NONE - 클라이언트가 서버 접속 시 묵시적으로 연속적으로 연결 시도를 하지 않음(Do not loop connection to server)

LOOP - 클라이언트가 서버 접속 시 묵시적으로 연속적으로 연결 시도를 함(Do loop connection to server)

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::CLIENT("127.0.0.1",8080,SINGLE,LOOP);
    
    while(1);
}

 

 

 

소켓 폐쇄

Cleanup socket

 

 

CLOSE();

 

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,SINGLE);
	
    while(1);
	TCP::CLOSE();
}

 

 

 

소켓 송수신 옵션 (서버에서만)

Socket Receiving and sending options (Only clientServer)

 

 

OPTIONS(int);

 

데이터 송수신 옵션 매크로 (Socket Receiving and sending Options Macro)

WAIT_NO (기본값(NORMAL_VALUE)) - 서버가 클라이언트 접속을 기다리지 않고 데이터를 전송 (Server not wait client connection and send packet data)

WAIT_YES - 서버가 클라이언트 접속을 기다린 후, 데이터를 전송 (Server wait client connection and send packet data)

 

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	TCP::OPTIONS(WAIT_YES);
	
	TCP::SEND(2,"test");
    //만약 현재서버에 2번클라이언트가없어도 들어올시 보내짐
    //Wait a client number 2 connection and send data,
    //if client number 2 is not exists on this server
	
	while(1);
	TCP::CLOSE();
}

//////////////////////////////////////////////////////////////////////////////////////

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	TCP::OPTIONS(WAIT_NO);  //(기본값)
	
	TCP::SEND(2,"test");
    //만약 현재서버에 2번클라이언트가없으면 후에 들어와도 데이터가 보내지지않음
    //Not wait a client number 2 connection and send data,
    //if client number 2 is not exists on this server
	
	while(1);
	TCP::CLOSE();
}

 

 

 

클라이언트 자동 멈춤

Client BreakPoint

 

 

WAIT();

 

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::CLIENT("127.0.0.1",8080,SINGLE,LOOP);
	
	
	TCP::WAIT();
	TCP::CLOSE();
}

 

 

클라이언트 접속 인원수 얻기 (서버에서만)

Get client connctions on MultiClientServer (Only clientServer)

 

 

CLIENTS();

 

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	
	int clientnum = TCP::CLIENTS();
	cout << clientnum;
	
	while(1);
	TCP::CLOSE();
}

 

 

 

 

나의 클라이언트 번호 얻기 (클라이언트에서만)

Get my client number on MultiClientServer (Only client)

 

 

GETNUMBER();

 

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::CLIENT("127.0.0.1",8080,MULTI,LOOP);
	
	int mynum = TCP::GETNUMBER();
	cout << mynum;
	
	TCP::WAIT();
	TCP::CLOSE();
}

 

 

 

 

데이터 송신

Send data

 

 

SEND(const char*);

SEND(int, const char*);

 

데이터 송신 매크로 (Socket Send data Macro)

ALL - 모든 클라이언트에게 메시지 전송 (Send message to all of clients)

 

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	
	char hello[PACKET_SIZE]="hello world";
	TCP::SEND(ALL,hello);
	
    while(1);
	TCP::CLOSE();
}
#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,SINGLE);
	
	char hello[PACKET_SIZE]="hello world";
	TCP::SEND(hello);
	
    while(1);
	TCP::CLOSE();
}
#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	
	char hello[PACKET_SIZE]="hello world";
	TCP::SEND(0,hello);
    //0번클라이언트에게 전송(Send a message to client number 0)
	
    while(1);
	TCP::CLOSE();
}
#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::CLIENT("127.0.0.1",8080,MULTI,LOOP);
	
	
	TCP::SEND(1,"hello");
    //멀티클라이언트서버의 클라이언트는 다른 클라이언트에게 메세지전송이 가능하다.
    //1번째 클라이언트에게 메세지전송
    //this client is Sending a message to client number 1
	
	TCP::WAIT();
	TCP::CLOSE();
}

 

 

데이터 수신

Receive data

 

 

RECEIVE(const char*);

RECEIVE(int, const char*);

 

 

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	
	char hello[PACKET_SIZE]={0};
	TCP::RECEIVE(hello);
	cout << hello;
	
	
	while(1);
	TCP::CLOSE();
}
#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	
	char hello[PACKET_SIZE]={0};
	TCP::RECEIVE(0,hello);
	cout << hello;
    
    //0번째클라이언트에게 메세지받기
    //Receive a message from client number 0
	
	
	while(1);
	TCP::CLOSE();
}

 

 

 

데이터 반응

Response PacketData

 

 

RESPONSE(int);

 

주의사항(Warning)

RESPONSE함수는 멀티 클라이언트 서버에서 클라이언트 간의 데이터 통신이 이루어질 때 반드시 사용해야 합니다.

(RESPONSE function have to use when data communication is carried out on MultiClientServer)

 

/////////////////서버(SERVER)///////////////////////

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	
	char hello[PACKET_SIZE]={0};
	TCP::RESPONSE(0);
    //0번째 클라이언트로부터 데이터반응
    //Response a data from client number 0
	
	while(1);
	TCP::CLOSE();
}

/////////////////0번째 클라이언트(CLIENT NUMBER 0)///////////////////////

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::CLIENT("127.0.0.1",8080,MULTI,LOOP);
	
	TCP::SEND(1,"hello");
	
	TCP::WAIT();
	TCP::CLOSE();
}

/////////////////1번째 클라이언트(CLIENT NUMBER 1)///////////////////////

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::CLIENT("127.0.0.1",8080,MULTI,LOOP);
	
	char hello[PACKET_SIZE]={0};
	TCP::RECEIVE(hello);
    cout << hello << endl;
	
	TCP::WAIT();
	TCP::CLOSE();
}

 

 

 

 

 

 


 

예시 (Examples.)

///////////////===서버(SERVER)===////////////////

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::SERVER(8080,MULTI);
	TCP::OPTIONS(WAIT_NO);
	
	TCP::RESPONSE(0);
	
	while(1);
	TCP::CLOSE();
}

///////////////===클라이언트(CLIENT)===////////////////

#include "codeSockv2.h"
#include <iostream>
using namespace std;

int main(){
	TCP::CLIENT("127.0.0.1",8080,MULTI,LOOP);
	
	int mynum = TCP::GETNUMBER();
	if(mynum==0) TCP::SEND(1,"hello client number 1");
	else if(mynum==1){
		char buf[PACKET_SIZE]={0};
		TCP::RECEIVE(buf);
		cout << buf;
	}
	
	TCP::WAIT();
	TCP::CLOSE();
}

 

 

 

 

 

 


 

 

 

 

 

그러면 이상 긴 글 봐주셔서

감사합니다~!!!!!!!!!!!!!!

728x90

댓글