计算机网络作业四


一、Hello客户服务器

1.在CodeBlocks里建立服务器端serv和客户端dialog WIN32 GUI工程

这里写图片描述这里写图片描述

2.GUI 设计

打开Resource.rc进入设计界面,拖控件上来,保存,至于软件前面的作业有介绍
这里写图片描述

3.环境配置

serv工程和dialog工程分别在CodeBlocks里打开Project–>Build Options–>Linker settings进行如下配置
这里写图片描述

4.写代码

serv服务端:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <stdlib.h>
#include <string.h>
#include <Iphlpapi.h>
#include <winsock2.h>

HINSTANCE hInst;
HWND button2;
HWND button3;
HWND list1;
HWND edit1;

WSADATA wsaData2;
SOCKET hSocket;
SOCKADDR_IN servAddr2;
char message2[30];
char name1[30],name2[30],check2[30];
int strLen2;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {

        button2=GetDlgItem(hwndDlg,IDC_BUTTON2);
        button3=GetDlgItem(hwndDlg,IDC_BUTTON3);
        list1=GetDlgItem(hwndDlg,IDC_LIST1);
        edit1=GetDlgItem(hwndDlg,IDC_EDIT1);
    }
    return TRUE;

    case WM_CLOSE:
    {
        closesocket(hSocket);
        WSACleanup();
        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {

        case IDC_BUTTON2:

            if(WSAStartup(MAKEWORD(2, 2), &wsaData2)!=0)
                printf("WSAStartup() error!");

            hSocket=socket(PF_INET, SOCK_STREAM, 0);
            if(hSocket == INVALID_SOCKET)
                printf("socket() error");

            memset(&servAddr2, 0 ,sizeof(servAddr2));
            servAddr2.sin_family=AF_INET;
            servAddr2.sin_addr.s_addr=inet_addr("127.0.0.1");
            servAddr2.sin_port=htons(9091);

            if(connect(hSocket, (struct sockaddr*)&servAddr2, sizeof(servAddr2))==SOCKET_ERROR)
                printf("connect() error");

            printf("连接成功..\n");

            break;

        case IDC_BUTTON3:
            Edit_GetText(edit1,check2,30);
            Edit_SetText(edit1,"");
            send(hSocket, check2, sizeof(check2), 0);
            memset(name1, 0, sizeof(name1));
            strcat(name1,"我:");
            strcat(name1,check2);
            ListBox_AddItemData(list1,name1);

            Sleep(1000);
            memset(name2, 0, sizeof(name2));
            memset(message2, 0, sizeof(message2));
            recv(hSocket,message2,sizeof(message2)-1, 0);
            strcat(name2,"我才不是机器人:");
            printf("message2: %s\n",message2);
            strcat(name2,message2);
            ListBox_AddItemData(list1,name2);
            break;
        }
    }
    return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

dialog客户端:

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <stdlib.h>
#include <string.h>
#include <Iphlpapi.h>
#include <winsock2.h>

HINSTANCE hInst;
HWND button2;
HWND button3;
HWND list1;
HWND edit1;

WSADATA wsaData2;
SOCKET hSocket;
SOCKADDR_IN servAddr2;
char message2[30];
char name1[30],name2[30],check2[30];
int strLen2;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {

        button2=GetDlgItem(hwndDlg,IDC_BUTTON2);
        button3=GetDlgItem(hwndDlg,IDC_BUTTON3);
        list1=GetDlgItem(hwndDlg,IDC_LIST1);
        edit1=GetDlgItem(hwndDlg,IDC_EDIT1);
    }
    return TRUE;

    case WM_CLOSE:
    {
        closesocket(hSocket);
        WSACleanup();
        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {

        case IDC_BUTTON2:

            if(WSAStartup(MAKEWORD(2, 2), &wsaData2)!=0)
                printf("WSAStartup() error!");

            hSocket=socket(PF_INET, SOCK_STREAM, 0);
            if(hSocket == INVALID_SOCKET)
                printf("socket() error");

            memset(&servAddr2, 0 ,sizeof(servAddr2));
            servAddr2.sin_family=AF_INET;
            servAddr2.sin_addr.s_addr=inet_addr("127.0.0.1");
            servAddr2.sin_port=htons(9091);

            if(connect(hSocket, (struct sockaddr*)&servAddr2, sizeof(servAddr2))==SOCKET_ERROR)
                printf("connect() error");

            printf("连接成功..\n");

            break;

        case IDC_BUTTON3:
            Edit_GetText(edit1,check2,30);
            Edit_SetText(edit1,"");
            send(hSocket, check2, sizeof(check2), 0);
            memset(name1, 0, sizeof(name1));
            strcat(name1,"我:");
            strcat(name1,check2);
            ListBox_AddItemData(list1,name1);

            Sleep(1000);
            memset(name2, 0, sizeof(name2));
            memset(message2, 0, sizeof(message2));
            recv(hSocket,message2,sizeof(message2)-1, 0);
            strcat(name2,"我才不是机器人:");
            printf("message2: %s\n",message2);
            strcat(name2,message2);
            ListBox_AddItemData(list1,name2);
            break;
        }
    }
    return TRUE;
    }
    return FALSE;
}

完成

运行

二、TCP客户服务器

第1步、第2步、第3步参考上面不再赘述,只贴出工程图和GUI图

这里写图片描述

这里写图片描述

4.代码

tserv服务端:

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <stdlib.h>
#include <string.h>
#include <Iphlpapi.h>
#include <winsock2.h>

HINSTANCE hInst;
HWND button1;

WSADATA wsaData;
SOCKET hServSock,hClntSock;
SOCKADDR_IN servAddr, clntAddr;

int szClntAddr;
char message[30]="Hello World!";
char check[30],ans[30];
int flag=1;
int strLen=0,i;
int idx=0, readLen=0;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {
        button1=GetDlgItem(hwndDlg,IDC_BUTTON1);
    }
    return TRUE;

    case WM_CLOSE:
    {

        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {
        case IDC_BUTTON1:
            printf("建立中..\n");
            if(WSAStartup(MAKEWORD(2, 2), &wsaData)!=0)
                printf("WSAStartup() error!");

            hServSock=socket(PF_INET, SOCK_STREAM, 0);
            if(hServSock == INVALID_SOCKET)
                printf("socket() error");

            memset(&servAddr, 0 ,sizeof(servAddr));
            servAddr.sin_family=AF_INET;
            servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
            servAddr.sin_port=htons(9091);

            if(bind(hServSock, (SOCKADDR*) &servAddr, sizeof(servAddr))==SOCKET_ERROR)
                printf("bind() error");

            if(listen(hServSock, 5)==SOCKET_ERROR)
                printf("listen() error");

            MessageBox(hwndDlg,"建立成功","建立信息", MB_OK);
            printf("正在监听...\n");


            szClntAddr=sizeof(clntAddr);
            hClntSock=accept(hServSock, (SOCKADDR*)&clntAddr, &szClntAddr);
            if(hClntSock==INVALID_SOCKET)
                printf("accept() error");
            printf("接听成功..\n");

            printf("开始发送..\n");
            for(i=strlen(message)-1; i>0; i--)
            {
                readLen=send(hClntSock, &message[idx++], 1, 0);
                if(readLen==-1)
                    printf("read() error!");
            }
            printf("发送完毕");

            closesocket(hClntSock);
            closesocket(hServSock);
            WSACleanup();

            break;
        }
    }
    return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

tclnt客户端:

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <stdlib.h>
#include <string.h>
#include <Iphlpapi.h>
#include <winsock2.h>

HINSTANCE hInst;
HWND button1;
HWND button2;
HWND edit1;

WSADATA wsaData2;
SOCKET hSocket;
SOCKADDR_IN servAddr2;
char message[30],a;
char name1[100]="Message from server: ",name2[100]="Function read call count: ",check2[30];
int strLen2;
int strLen=0,i;
int idx=0, readLen=0;

BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
    {

        button1=GetDlgItem(hwndDlg,IDC_BUTTON1);
        button2=GetDlgItem(hwndDlg,IDC_BUTTON2);
        edit1=GetDlgItem(hwndDlg,IDC_EDIT1);
    }
    return TRUE;

    case WM_CLOSE:
    {
        closesocket(hSocket);
        WSACleanup();
        EndDialog(hwndDlg, 0);
    }
    return TRUE;

    case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {

        case IDC_BUTTON1:

            if(WSAStartup(MAKEWORD(2, 2), &wsaData2)!=0)
                printf("WSAStartup() error!");

            hSocket=socket(PF_INET, SOCK_STREAM, 0);
            if(hSocket == INVALID_SOCKET)
                printf("socket() error");

            memset(&servAddr2, 0 ,sizeof(servAddr2));
            servAddr2.sin_family=AF_INET;
            servAddr2.sin_addr.s_addr=inet_addr("127.0.0.1");
            servAddr2.sin_port=htons(9091);

            if(connect(hSocket, (struct sockaddr*)&servAddr2, sizeof(servAddr2))==SOCKET_ERROR)
                printf("connect() error");

            printf("连接成功..\n");

            break;

        case IDC_BUTTON2:
            for(i=0; i<3000; i++)
                printf("Wait time %d \n",i);
            while(readLen=recv(hSocket, message, sizeof(message)-1, 0))
            {
                if(readLen==-1)
                    printf("read() error!");

                strLen++;
            }
            printf("接收完成\n");
            printf("Message from server: %s \n", message);
            printf("Function read call count: %d \n", strLen);
            break;
        }
    }
    return TRUE;
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值