블로그 이미지
ludwings

카테고리

분류 전체보기 (130)
WIN API (22)
워게임 (61)
만든것 (2)
메모 (37)
Total18,086
Today0
Yesterday3

'WIN API'에 해당되는 글 22건

  1. 2013.05.06 WINAPI ] 채팅 프로그램1
  2. 2013.05.06 WINAPI ] tlhelp32 프로세스 목록 얻기

Server

1.     Winsock 초기화

2.     Socket 만들기

3.     Socket 바인딩(확정)

4.     클라이언트 접속 기다림

5.     클라이언트 Accept

6.     Data 받음

7.     연결종료

Client

1.     Winsock 초기화

2.     Socket 만들기

3.     서버에 연결

4.     Data 보내기

5.     연결종료

 

//server

#include<stdio.h>
#include<WinSock2.h>
#include<WS2tcpip.h>

#define DEFAULT_PORT "1994"
#define DEFAULT_BUFLEN 512
#pragma comment(lib,"Ws2_32.lib")

int main()
{
 WSADATA wsaData;
 char recvbuf[DEFAULT_BUFLEN];
 int iResult, iSendResult;
 int recvbuflen = DEFAULT_BUFLEN;

 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
 if(iResult != 0)
 {
  printf("WSAStartup failed: %d\n", iResult);
  return 1;
 }

 // create socket for server
 struct addrinfo *result = NULL, *ptr = NULL, hints;
 ZeroMemory(&hints, sizeof(hints));
 hints.ai_family = AF_INET;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP; // ip v4
 hints.ai_flags = AI_PASSIVE;

 iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
 if(iResult != 0)
 {
  printf("getaddrinfo failed: %d\n", iResult);
  return 1;
 }

 //create socket listensocket for client connections
 SOCKET ListenSocket = INVALID_SOCKET;

 ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
 if(ListenSocket == INVALID_SOCKET)
 {
  printf("Error at socket(): %d\n", WSAGetLastError());
  freeaddrinfo(result);
  WSACleanup();
  return 1;
 }

 // set up tcp listening socket
 iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
 if(iResult == SOCKET_ERROR)
 {
  printf("bind failed with error :%d\n", WSAGetLastError());
  freeaddrinfo(result);
  closesocket(ListenSocket);
  WSACleanup();
  return 1;
 }

 // listen on a socket

 if(listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
 {
  printf("listen failed with error: %ld\n", WSAGetLastError());
  closesocket(ListenSocket);
  WSACleanup();
  return 1;
 }


 SOCKET ClientSocket = INVALID_SOCKET;

 

 printf("wait for client....");
 ClientSocket = accept(ListenSocket, NULL, NULL);
 if(ClientSocket == INVALID_SOCKET)
 {
  printf("accept failed with error :%d\n", WSAGetLastError());
  closesocket(ListenSocket);
  WSACleanup();
  return 1;
 }

 // Receive until the peer shuts down the connection
 do {

  iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  if (iResult > 0) {
   printf("Bytes received: %d\n", iResult);

   // Echo the buffer back to the sender
   iSendResult = send(ClientSocket, recvbuf, iResult, 0);
   if (iSendResult == SOCKET_ERROR) {
    printf("send failed: %d\n", WSAGetLastError());
    closesocket(ClientSocket);
    WSACleanup();
    return 1;
   }
   printf("Bytes sent: %s\n", recvbuf);
  } else if (iResult == 0)
   printf("Connection closing...\n");
  else {
   printf("recv failed: %d\n", WSAGetLastError());
   closesocket(ClientSocket);
   WSACleanup();
   return 1;
  }

 } while (iResult > 0);

 // shutdown the send half of the connection since no more data will be sent
 iResult = shutdown(ClientSocket, SD_SEND);
 if (iResult == SOCKET_ERROR) {
  printf("shutdown failed: %d\n", WSAGetLastError());
  closesocket(ClientSocket);
  WSACleanup();
  return 1;
 }

 /*
 // cleanup
 closesocket(ClientSocket);
 WSACleanup();

 return 0;
 //*/


 return 0;
}

 

 

//client

#include<stdio.h>
#include<WinSock2.h>
#include<WS2tcpip.h>

#define DEFAULT_PORT "1994"
#define DEFAULT_BUFLEN 512
#pragma comment(lib,"Ws2_32.lib")

int main()
{
 WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
 struct addrinfo *result = NULL,
  *ptr = NULL,
  hints;

 char *sendbuf = "this is a test";
 char recvbuf[DEFAULT_BUFLEN];
 int iResult;
 int recvbuflen = DEFAULT_BUFLEN;

 ZeroMemory(&hints, sizeof(hints));
 hints.ai_family = AF_UNSPEC;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;

 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
 if(iResult != 0)
 {
  printf("WSAStartupFailed with error :%d\n", iResult);
  return 1;
 }

 ZeroMemory(&hints, sizeof(hints));
 hints.ai_family = AF_UNSPEC;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;

 iResult = getaddrinfo("127.0.0.1", DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

 // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
            ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) {
            printf("socket failed with error: %ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }

        // Connect to server.
        iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }

    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    printf("Bytes Sent: %ld\n", iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            printf("Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else
            printf("recv failed with error: %d\n", WSAGetLastError());

    } while( iResult > 0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}

 

MSDN에서 가져온 기본 server - client 소스다.

여기서 내 입맛에 맞게 변경시켜야징

'WIN API' 카테고리의 다른 글

Dll Injector, Dll Ejector  (0) 2013.10.11
#pragma once  (0) 2013.09.04
WINAPI ] 간단한 채팅2  (0) 2013.05.07
간단한 쓰레드 만들기  (0) 2013.05.06
WINAPI ] 채팅 프로그램1  (0) 2013.05.06
WINAPI ] tlhelp32 프로세스 목록 얻기  (0) 2013.05.06
Posted by ludwings

댓글을 달아 주세요

#include <Windows.h>
#include <tlhelp32.h>
#include <stdio.h>

int main()
{
BOOL next;

HANDLE hSnapshot;
PROCESSENTRY32 pe;

hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pe.dwSize = sizeof(PROCESSENTRY32);

// dwsize를 초기화 하지 않으면Process32First가 실패
next = Process32First(hSnapshot, &pe);

while(next)
{
printf("%ls\n",pe.szExeFile);
next = Process32Next(hSnapshot, &pe);
}

return 0;
}

 

'WIN API' 카테고리의 다른 글

Dll Injector, Dll Ejector  (0) 2013.10.11
#pragma once  (0) 2013.09.04
WINAPI ] 간단한 채팅2  (0) 2013.05.07
간단한 쓰레드 만들기  (0) 2013.05.06
WINAPI ] 채팅 프로그램1  (0) 2013.05.06
WINAPI ] tlhelp32 프로세스 목록 얻기  (0) 2013.05.06
Posted by ludwings

댓글을 달아 주세요

최근에 달린 댓글

최근에 받은 트랙백

글 보관함