2017-06-06 작업일지
본문
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#define SERVER_PORT 5000 // 0x1388 (포트 5000)
#define BUFFER_SIZE 1000
typedef struct {
SOCKET socket;
char buffer[BUFFER_SIZE];
int bufferSize;
} ClientSession;
// 로그인 서버에 연결하는 함수
signed int ConnectToLoginServer(ClientSession* client, const char* ipAddress) {
if (!client || !ipAddress) return 0;
client->socket = socket(AF_INET, SOCK_STREAM, 0);
if (client->socket == INVALID_SOCKET) {
return 0;
}
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
serverAddr.sin_addr.s_addr = inet_addr(ipAddress);
DWORD startTime = GetTickCount();
// 서버에 연결 시도
while (connect(client->socket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
int error = WSAGetLastError();
if (error != WSAECONNREFUSED && error != WSAENETUNREACH && error != WSAETIMEDOUT) {
closesocket(client->socket);
return 0;
}
if (GetTickCount() - startTime > 5000) {
closesocket(client->socket);
return 0;
}
}
// 소켓 비동기 설정
if (WSAAsyncSelect(client->socket, NULL, 0, FD_CONNECT | FD_READ | FD_CLOSE) == SOCKET_ERROR) {
closesocket(client->socket);
return 0;
}
client->bufferSize = 0;
// 로그인 패킷 준비 후 전송
return PrepareLoginPacket(client) ? 1 : 0;
}
// 로그인 패킷을 준비하는 함수
bool PrepareLoginPacket(ClientSession* client) {
if (!client) return false;
client->buffer[0] = 34;
int offset = 1;
// ID 문자열 추가
const char* userId = "TestUser"; // byte_43ED3C의 값 대체
int userIdLen = strlen(userId);
client->buffer[offset++] = (char)userIdLen;
memcpy(client->buffer + offset, userId, userIdLen);
offset += userIdLen;
// 비밀번호 문자열 추가
const char* password = "TestPass"; // byte_43ED10의 값 대체
int passwordLen = strlen(password);
client->buffer[offset++] = (char)passwordLen;
memcpy(client->buffer + offset, password, passwordLen);
offset += passwordLen;
client->buffer[offset] = 40;
client->bufferSize = offset + 1;
// 패킷 전송
return SendLoginPacket(client);
}
// 로그인 패킷을 서버로 전송하는 함수
signed int SendLoginPacket(ClientSession* client) {
if (!client || client->bufferSize <= 0) return 0;
int bytesSent = 0;
while (bytesSent < client->bufferSize) {
int result = send(client->socket, client->buffer + bytesSent, client->bufferSize - bytesSent, 0);
if (result == SOCKET_ERROR) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
return 0;
}
} else {
bytesSent += result;
}
}
client->bufferSize = 0;
return 1;
}
댓글목록 0
등록된 댓글이 없습니다.