From today, I am going to review all my studies at Dankook.
Today's topic is TCP/IP, which is the basis of network programming.

The teacher is Dr. 조경산. He is a excellent teacher =)

Basically, we need three elements, a sender, a receiver and a thing that connects between them
 so that network will work

What's the computer communication?
   : the computer, which equals with a receiver and sender or something related to computer.

 The important thing is why the computer communication has come out.
  1) Direct connection : just like word itself, it connects btw two computers directly by using a wire or physical stuff.
  2) Indirect connection : connecting all computers with a lot of networks, basically networks are connected.

 Direct connection costs a lot of money since everything would have to be closer and it also has a distance problem.

So, in order to do the indirect connection, we would have to make a rule so that all datas should have been sended and received perfectly against the direct connections.

we call the rule "protocol"

The definition of protocol is "a rule that is accepted in order to communicate between computer systems"
it consists of syntax, semantics, and timing.
syntax : the level of signal or data type.
semantics : the information for handling exceptions and each communications
timing : corresponding data speed and make an order


Now, important thing is "OSI7 layer protocol", which is a bit different from TCP/IP protocol

OSI 7 layer

Application(7)
Presentation(6)
Session(5)
Transport(4)
Network(3)
Data Link(2)
Physical(1)

However, TCP/IP has five layers.

application(5)
transport(4)
internet(3)
network interface(2)
physical(1)

since physical layer only hands the data transfer, we sometimes merge layer 1 and layer 2 as "network interface" itself.

We all data 'Packet' in network computing. but each layer has a different term indicating "Packet"
Network interface(2) -> frame
internet(3) -> Datagram
Transport(4) -> Segment.

Why we use TCP/IP? it's because..
"in order to treat internet easily.." is there anything more persuading than this?

based on OSI 7 layers concept, each layer except for physical layer will put one header each in the data.

 Layer 2
header
 Layer 3
header
Layer 4
header
 Layer 5
header
 Layer 6
header
  Layer 7
header
data


Well let's see "network interface layer(2)"

there is a Ethernet which is commonly used in Layer 2.

The structure of frame(= packet, called as "frame" in network interface layer.

Preamble
DA
SA
length
Data type
Data
CRC
IEEE 802.3 ethernet frame type

  Well. the structure is not a original data. it's been added with "header!" see a lot of headers, Preamble, DA, SA stuff like that.  look at the DA(Destination address)... what kind of address we use? IP? nope...
we use MAC(Media Access control address), which is a unique number of NIC(Network interface Card).
why do we use MAC? Layer 2 only cares about just one Network inside.
If there was no MAC, all data in one network would be sent to all computers in one network.
That's why we need MAC in order to look at the destination of the computer which we are supposed to send.

Loot at the CRC located in the end of a frame.
What's CRC firstly? -> Cyclic redundancy check, an error-detecting code designed to detect accidental changes to raw comtpuer data Wikipedia said.

Well. the thing why we need this is because Layer 1(physical) only care about transmitting all data bit by bit
in hardware perspective, the CRC must be handled in NIC(network interface card). it's more likely physical perspective.

lastly, take a look at length part. why do we need length? because.. there is no way how the computer would know
the length of a data. That's all.

let's move on the layer 3(internet layer).
we will see the concept of IP..
why did IP come out? it's because there was a problem between networks.
each network needed to transmit data to one another. but the thing is the media is different, form of frame is different..
that's why people though of making one virtual network which contains all networks.(let's see all networks as one network)

You know what?
All computers connected to the internet is connected to another network.(do you agree?)

as you know internet is a set of a lot of network. IP is the address which is uniquely possessed by a computer which
is connected to the internet.

now, there is new packet coming out, data gram...
IP is just one kind of protocols which defines the path of a destination from a sender.

Let's think about the difference btw MAC and IP(Internet protocol) addresses..
again, MAC is only used in one network inside, but IP is used from a network to network. Tha'ts the different.


What is the purpose of the internet? : virtual single communication system

 -> All hosts must use the same manner to make an address.
 -> the address must be unique
 -> IP address : independent address indicated(networks only recognize MAC address)
 -> usually 32 bit but IPv6 is 128 bits these days.. there is prefix and suffix.
in datagram header(it could be maximum 6k bytes(20+4n byte(header) + data)

there is one element of the header, TTL = Time to leave... -> why?
if a datagram(I mean packet) missed the way where it's supposed to be, then check the TTL in each network
net if the length is "1"?(from 255) then just throw it away because it took too long!

also there is CHECKSUM... it is implemented by a system.

Encapsulation.. you know the header will be put from Layer 5 to Layer 1 in Sender
and a destination take each one off from layer 1 to layer 5

ICMP(Internet Control Message Protocol)

IP has one characteristic.. which is best-effort, no matter what situations are, it doesn't matter
the purpose of IP is to transmit a data from sender to a receiver that's all.
it sometimes causes a error" but it doesn't matter

ICMP is purposed for the situation if IP layer(internet layer) has a problem in communications then
send a error msg to a destination.

so IP should be used with ICMP for handling errors..

'Programming > Network Programming' 카테고리의 다른 글

.. no regret..  (0) 2011.10.26
The example of one server that sends a client ID  (0) 2011.10.15
get client IPadrress in server side  (0) 2011.10.10
Reference for unix socket api  (0) 2011.09.29
TCP/IP  (0) 2011.09.21
Posted by 박세범
Programming/WinAPI2011. 9. 4. 17:07
I just started to study WinAPI... Actually, I didn't know how Windows applications work,
now I understand sort of(less than 1% though).
Below is what I've practiced.

Input, output, handling resources.


#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam);
HINSTANCE g_hInst;
LPCTSTR lpszClass=TEXT("Menu");

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpszCmdParam, int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst = hInstance;            // ??
    HACCEL hAccel;

    hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(ID_TEST_MENU1));


   

    WndClass.cbClsExtra =0;
    WndClass.cbWndExtra = 0;
    WndClass.hbrBackground= CreateSolidBrush(RGB(0,0,255));
        //(HBRUSH)GetStockObject(BLACK_BRUSH);
    WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc = WndProc;
    WndClass.lpszClassName = lpszClass;
    WndClass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);

    hWnd = CreateWindow(lpszClass, TEXT("Sebeom's API"), WS_OVERLAPPEDWINDOW | WS_VSCROLL,
        CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL,(HMENU)NULL,hInstance,NULL);
    ShowWindow(hWnd,nCmdShow);

    while(GetMessage(&Message,NULL,0,0))
    {
        if(!TranslateAccelerator(hWnd,hAccel,&Message))
        {
            TranslateMessage(&Message);
            DispatchMessage(&Message);
        }

    }

    return (int)Message.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    //static TCHAR str[256];
    static int x=100, y=100;
    static BOOL bNowDraw = FALSE;
    static int len;
    static int index = 0;
    SYSTEMTIME st;
    static TCHAR sTime[128];
    static TCHAR *storage[256] = {TEXT("Hello"), TEXT("HIHIHIHI")};
    static TCHAR str[256];

    switch(iMessage)
    {
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case ID_TEST_MENU1:
                MessageBox(hWnd,TEXT("First menu has beeon chosen."), TEXT("Menu Demo"), MB_OK);
            case ID_TEST_MENU2:
                lstrcpy(str, TEXT("you chose Menu 2"));
        }

    case WM_CREATE:
        SetTimer(hWnd, 1, 2000, NULL);
        SendMessage(hWnd,WM_TIMER,1,0);
        return 0;
   
    case WM_TIMER:
//        GetLocalTime(&st);
//        wsprintf(sTime, TEXT("지금 시간은 %d:%d:%d입니다"), st.wDay, st.wHour, st.wMinute);
        lstrcpy(str,*(storage+index));
        index++;
        x += 100;
        y += 100;
        InvalidateRect(hWnd,NULL,TRUE);
        return 0;

    case WM_CHAR:
        if((TCHAR)wParam == VK_BACK)
        {
            MessageBox(hWnd,TEXT("haha"),TEXT("haha"),NULL);
           
        }
        return 0;

    case WM_LBUTTONDOWN:
        x= LOWORD(lParam);
        y= HIWORD(lParam);
        bNowDraw = TRUE;
   
        return 0;

    case WM_MOUSEMOVE:
        if(bNowDraw == TRUE)
        {
            hdc = GetDC(hWnd);
            MoveToEx(hdc, x, y, NULL);
            x= LOWORD(lParam);
            y= HIWORD(lParam);           
            LineTo(hdc, x, y);
            ReleaseDC(hWnd,hdc);
        }
        return 0;

    case WM_LBUTTONUP:
        bNowDraw = FALSE;
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hWnd,&ps);
        TextOut(hdc,x,y,str,lstrlen(str));        // to display the string that I will write down.
        EndPaint(hWnd,&ps);
        return 0;
       
    case WM_DESTROY:
        PostQuitMessage(0);
        KillTimer(hWnd,1);
        return 0;

    /*case WM_LBUTTONDOWN:
        hdc=GetDC(hWnd);
        TextOut(hdc,100,100,TEXT("Beautiful Korea"), 15);
        ReleaseDC(hWnd,hdc);
        return 0;
    }*/

/*
    case WM_PAINT:
        hdc = BeginPaint(hWnd,&ps);
//        DrawText(hdc,str,-1,&rt,DT_CENTER | DT_WORDBREAK);

//        MoveToEx(hdc,300,300,NULL);
//        LineTo(hdc,400,400);
        TextOut(hdc,100,100,str,lstrlen(str));
        EndPaint(hWnd,&ps);
        return 0;
//        MessageBox(hWnd,TEXT("그대는 찹쌀 떡"),TEXT("Jegal"),MB_OK);
        //TextOut(hdc,100,100,TEXT("Seeom"),5);
        //wsprintf("Hello %d", 10);
        //EndPaint(hWnd,&ps);

        */

    }

    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
    // it handles the rest of all cases like double click, quit stuff like that
}

'Programming > WinAPI' 카테고리의 다른 글

Win32 Timer with Callback function  (0) 2011.09.23
Posted by 박세범
Programming2011. 8. 31. 11:14
#include "Everything.h"        // this is book's own defined header
                            // it includes windows.h etc

#define BUF_SIZE 0x200       

static VOID CatFile (HANDLE, HANDLE);        // method
int _tmain (int argc, LPTSTR argv [])
{
    HANDLE hInFile, hStdIn = GetStdHandle (STD_INPUT_HANDLE);        // make an input handler, see the return type

    HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE);                // the handle where we want to write
    BOOL dashS;                       
    int iArg, iFirstFile;           

    /*    dashS will be set only if "-s" is on the command line. */
    /*    iFirstFile is the argv [] index of the first input file. */
    iFirstFile = Options (argc, argv, _T("s"), &dashS, NULL);

    if (iFirstFile == argc) { /* No files in arg list. */
        CatFile (hStdIn, hStdOut);
        return 0;
    }
   
    /* Process the input files. */
    for (iArg = iFirstFile; iArg < argc; iArg++) {
        hInFile = CreateFile (argv [iArg], GENERIC_READ,
                0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);        // Create file with
        if (hInFile == INVALID_HANDLE_VALUE) {            // if the file is not appropriate
            if (!dashS) ReportError (_T ("Cat Error: File does not exist."), 0, TRUE);
        } else {
            CatFile (hInFile, hStdOut);
            if (GetLastError() != 0 && !dashS) {
                ReportError (_T ("Cat Error: Could not process file completely."), 0, TRUE);
            }
            CloseHandle (hInFile);
        }
    }
    return 0;
}

static VOID CatFile (HANDLE hInFile, HANDLE hOutFile)
{
    DWORD nIn, nOut;                // Dword mean easily 32bit unsigned int
    BYTE buffer [BUF_SIZE];

    while (ReadFile (hInFile, buffer, BUF_SIZE, &nIn, NULL) && (nIn != 0)
            && WriteFile (hOutFile, buffer, nIn, &nOut, NULL));
    // Read the file and write exactly.
    return;
}

Easy example, take a look at the CatFile, using windows API, ReadFile and WriteFile, makes a copy of the
original file, argv[1]

LPTSTR -> Long Pointer something something, in other words, like char*
Posted by 박세범