Giter Site home page Giter Site logo

socketprogramming's Introduction

SocketProgramming

TCP 채팅서버 구현

  • 시연 영상 (유튜브로 이동합니다)
Video Label

<패킷 구조>

해더(4 byte) + 데이터(0~1024 byte)

  • 해더 : 패킷 크기(2byte) + 패킷 코드(2byte)

패킷 코드(enum)

S2C : Server to Client

C2S : Client to Server

enum
  • None = 0

  • S2C_CastMessage = 1

  • C2S_CastMessage = 2 //reserved

  • S2C_Login_UserIDReq = 100

  • C2S_Login_UserIDAck = 101

  • S2C_Login_UserIDFailureReq = 102

  • C2S_Login_UserIDReq = 103

  • C2S_Login_MakeNewUserReq = 110

  • S2C_Login_NewUserNickNameReq = 111

  • C2S_Login_NewUserNickNameAck = 112

  • S2C_Login_NewUserPwdReq = 113

  • C2S_Login_NewUserPwdAck = 114

  • S2C_Login_UserPwdReq = 120

  • C2S_Login_UserPwdAck = 121

  • S2C_Login_UserPwdFailureReq = 122

  • C2S_Login_UserPwdReq = 123

  • S2C_LoginSuccess = 150

  • S2C_AlreadyLoginOnServer = 151

  • S2C_CanChat = 200

  • C2S_Chat = 201

  • S2C_Chat = 202

  • Max



Server

  • 미리 만들어둔 config.txt 파일에서 DB 서버 IP, UserName, Pwd 가져옴. config.txt 파일은 ignore 처리함.
  • mysql/c++ connector 라이브러리 사용해서 DB와 연결
  • Select Server 구현
    • 각각의 클라들에 대한 ServerThread 실행(싱글 스레드)

ServerThread

  1. ID 요청 패킷 전송

  2. 아래 반복

    1. 헤더의 패킷크기 수신 대기 (2byte)

    2. 헤더의 패킷 크기만큼 패킷 데이터 수신 (2byte(패킷 코드) + 패킷크기 - 2byte)

    3. 패킷 코드에 따라 case문 실행

      • 전송할 데이터가 있으면 패킷을 만들어 전송

DB

  • 한글(멀티 바이트)를 깨지지 않게 DB에 입출력 하기위해 MultibyteToUtf8, Utf8ToMultibyte 함수 구현

    • Nickname, 채팅을 위 함수를 사용하여 변환하여 DB에 입출력
  • DB 테이블

    • userconfig
      • 유저의 ID, 닉네임, 비밀번호를 저장하는 테이블
    • chatlog
      • 유저들이 채팅 내역을 저장하는 테이블


Client

  • TCP 서버에 접속 요청
  • 서버에 접속 성공 시 RecvThread, SendThread 실행 (멀티 스레드)

RecvThread

  • 아래 반복
    1. 서버 소켓으로 패킷크기 수신 대기 (2byte)

    2. 헤더의 패킷 크기만큼 패킷 데이터 수신 (2byte(패킷 코드) + 패킷크기 - 2byte)

    3. 수신받은 패킷 코드를 전역변수에 저장, 패킷 코드에 따라 case문 실행

SendThread

  • 아래 반복
    1. 수신받은 패킷 코드에 따라 case문 실행
      • 전송할 데이터를 패킷을 만들어 전송


socketprogramming's People

Contributors

jjw1270 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.