网络安全编程——编程题、简答题

  • 格式:docx
  • 大小:21.99 KB
  • 文档页数:3

下载文档原格式

  / 8
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Tcp server:#include #include #include

#pragma comment( lib, "ws2_32.lib" )

void main()

{ //参数声明

WSADATA wsaData; SOCKET ServerSocket; SOCKET NewSocket;

SOCKADDR_IN ServerAddr; SOCKADDR_IN ClientAddr;

intClientAddrLen;intServerPort = 4000;

wsastarup(makeword(1,0),&wsadata)//初始化windows socket

//创建socket

ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

ServerAddr.sin_family = AF_INET;

ServerAddr.sin_port = htons(ServerPort);

ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

bind(ServerSocket, (SOCKADDR*) &ServerAddr, sizeof(ServerAddr)); //绑定

listen(ServerSocket, 5);//监听

do{NewSocket=accept(ServerSocket,(SOCKADDR*)&ClientAddr,&ClientAddrLen);CreateThread(NULL, 0, DealHandler, (void*)NewSocket, 0, NULL);//线程回调

}while (1);

recv(newsocket,receivebuffer,sizeof(receivebuffer)-1,0)//接收

send(newsocket, receive buffer,strlen(receivebuffer),0)//发送

closesocket (newsocket);

closesocket (serversocket);//关闭

wsacleanup()//释放

DWORD WINAPI DealHandler(void* sd_)

{intnRetval;SOCKET sd = (SOCKET)sd_;nRetval=DealReq(sd);

returnnRetval;}

连接请求:

connect(clientsocket,(socketaddr*)&serveraddr,sizef(serveraddr);

抓包

#include "stdio.h"#include "Winsock2.h"#include "time.h"#pragma comment(lib,"WS2_32.lib")

#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)

int main(intargc, char **argv)

{//定义参数

SOCKET SnifferSocket;intResult;char Packet[6000];WSADATA wsaData;

Result = WSAStartup(MAKEWORD(2, 2), &wsaData); //初始化

SnifferSocket = socket(AF_INET, SOCK_RAW, IPPROTO_IP);//创建原始socket

structsockaddr_in sock={0};structhostent *pHostent;char Name[255];

sock.sin_family = AF_INET;Result = gethostname(Name, sizeof(Name)); //获取主机名

pHostent = gethostbyname(Name); //用主机名获取IP

memcpy(&sock.sin_addr, pHostent->h_addr_list[0], pHostent->h_length);

Result = bind(SnifferSocket, (PSOCKADDR) &sock, sizeof(sock));//绑定

Result =ioctlsocket(SnifferSocket, SIO_RCVALL, &dwBufferInLen);//设置混杂模式

intpacketcount = 0;structsockaddr_in from;//rendbintfromlen ;time_t temp;

while (true)

{fromlen=sizeof(from);

Result=recvfrom(SnifferSocket,Packet,,izeof(Packet),0);//读取数据

packetcount++;time(&temp);if (packetcount==70) break;}

closesocket(SnifferSocket);WSACleanup();return 1;}

窗口

#include#include "resource.h"

BOOL WINAPI DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)

{DialogBoxParam(

hInstance,// 实例句柄(LPCTSTR)IDD_DIALOG1, // 对话框资源ID号NULL, // 父窗口句柄

DlgProc, // 消息处理函数0); // 对话框初始化的值,在WM_INITDIALOG消息lParam参数中取出

return 0;}

char szLText[]="鼠标左键";char szOKText[]="OK button";HWNDMyList;

BOOL WINAPI DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

{switch(message){

case WM_INITDIALOG:

MyList=GetDlgItem(hDlg, IDC_LIST1);

SendMessage(MyList, LB_ADDSTRING, NULL, (long)"初始化了");

break;

case WM_LBUTTONDOWN: // 显示给用户, 向列表框控件中添加文本

SendMessage(MyList, LB_ADDSTRING, NULL, (long)szLText);

break;

case WM_COMMAND:

switch(LOWORD(wParam))

{

case IDOK:

SendMessage(MyList, LB_ADDSTRING, NULL, (long)szOKText);

break;

case IDCANCEL: // 退出程序

EndDialog (hDlg, IDCANCEL);

break;

}

break;

}

return 0;

}

Tcp的特点

(1)面向流的投递服务

应用程序之间传输的数据可视为无结构的字节流(或位流),流投递服务保证收发的字节顺序完全一致

2)面向连接的投递服务

数据传输之前,TCP模块之间需建立连接(类似虚电路),其后的TCP报文在此连接基础上传输。

(2)可靠传输服务

接收方根据收到的报文中的校验和,判断传输的正确性:如果正确,进行应答,否则丢弃报文。发送方如果在规定的时间内未能获得应答报文,自动进行重传。

(4)缓冲传输