到底是如何实现网络编程??? (100分)

  • 主题发起人 主题发起人 小鱼儿2000
  • 开始时间 开始时间

小鱼儿2000

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:我想知道如何利用一些协议如:ICMP TELNET 等等 来编写程序来利用这些协议!
小弟专业不是计算机的。我看书以后 我觉得是这样的:首先是要根据协议定义一些数据类型
比如说是:
typedef struct _iphdr
{
unsigned int h_len:4; // Length of the header
unsigned int version:4; // Version of IP
unsigned char tos; // Type of service
unsigned short total_len; // Total length of the packet
unsigned short ident; // Unique identifier
unsigned short frag_and_flags; // Flags
unsigned char ttl; // Time to live
unsigned char proto; // Protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum

unsigned int sourceIP;
unsigned int destIP;
} IpHeader;
。。。。。。。。
。。。。。。。。
。。。。。。。。
。。。。。。。。

然后就是利用<WINSOCK.H>提供的函数编写通讯程序 不知道是不是这样的??

不知道 哪一位有耐心的朋友可以给我解释一下 下面的PING 程序

#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>

#define IP_RECORD_ROUTE 0x7


typedef struct _iphdr
{
unsigned int h_len:4; // Length of the header
unsigned int version:4; // Version of IP
unsigned char tos; // Type of service
unsigned short total_len; // Total length of the packet
unsigned short ident; // Unique identifier
unsigned short frag_and_flags; // Flags
unsigned char ttl; // Time to live
unsigned char proto; // Protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum

unsigned int sourceIP;
unsigned int destIP;
} IpHeader;

#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0
#define ICMP_MIN 8 // Minimum 8-byte ICMP packet (header)


typedef struct _icmphdr
{
BYTE i_type;
BYTE i_code; // Type sub code
USHORT i_cksum;
USHORT i_id;
USHORT i_seq;
ULONG timestamp;
} IcmpHeader;


typedef struct _ipoptionhdr
{
unsigned char code; // Option type
unsigned char len; // Length of option hdr
unsigned char ptr; // Offset into options
unsigned long addr[9]; // List of IP addrs
} IpOptionHeader;

#define DEF_PACKET_SIZE 32 // Default packet size
#define MAX_PACKET 1024 // Max ICMP packet size
#define MAX_IP_HDR_SIZE 60 // Max IP header size w/options

BOOL bRecordRoute;
int datasize;
char *lpdest;

void usage(char *progname)
{
printf("usage: ping -r <host> [data size]/n");
printf(" -r record route/n");
printf(" host remote machine to ping/n");
printf(" datasize can be up to 1KB/n");
ExitProcess(-1);
}


void FillICMPData(char *icmp_data, int datasize)
{
IcmpHeader *icmp_hdr = NULL;
char *datapart = NULL;

icmp_hdr = (IcmpHeader*)icmp_data;
icmp_hdr->i_type = ICMP_ECHO; // Request an ICMP echo
icmp_hdr->i_code = 0;
icmp_hdr->i_id = (USHORT)GetCurrentProcessId();
icmp_hdr->i_cksum = 0;
icmp_hdr->i_seq = 0;

datapart = icmp_data + sizeof(IcmpHeader);
memset(datapart,'E', datasize - sizeof(IcmpHeader));
}

USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;

while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}



void DecodeIPOptions(char *buf, int bytes)
{
IpOptionHeader *ipopt = NULL;
IN_ADDR inaddr;
int i;
HOSTENT *host = NULL;

ipopt = (IpOptionHeader *)(buf + 20);

printf("RR: ");
for(i = 0; i < (ipopt->ptr / 4) - 1; i++)
{
inaddr.S_un.S_addr = ipopt->addr;
if (i != 0)
printf(" ");
host = gethostbyaddr((char *)&inaddr.S_un.S_addr,
sizeof(inaddr.S_un.S_addr), AF_INET);
if (host)
printf("(%-15s) %s/n", inet_ntoa(inaddr), host->h_name);
else
printf("(%-15s)/n", inet_ntoa(inaddr));
}
return;
}



void DecodeICMPHeader(char *buf, int bytes,
struct sockaddr_in *from)
{
IpHeader *iphdr = NULL;
IcmpHeader *icmphdr = NULL;
unsigned short iphdrlen;
DWORD tick;
static int icmpcount = 0;

iphdr = (IpHeader *)buf;
// Number of 32-bit words * 4 = bytes
iphdrlen = iphdr->h_len * 4;
tick = GetTickCount();

if ((iphdrlen == MAX_IP_HDR_SIZE) && (!icmpcount))
DecodeIPOptions(buf, bytes);

if (bytes < iphdrlen + ICMP_MIN)
{
printf("Too few bytes from %s/n",
inet_ntoa(from->sin_addr));
}
icmphdr = (IcmpHeader*)(buf + iphdrlen);

if (icmphdr->i_type != ICMP_ECHOREPLY)
{
printf("nonecho type %d recvd/n", icmphdr->i_type);
return;
}
// Make sure this is an ICMP reply to something we sent!
//
if (icmphdr->i_id != (USHORT)GetCurrentProcessId())
{
printf("someone else's packet!/n");
return ;
}
printf("%d bytes from %s:", bytes, inet_ntoa(from->sin_addr));
printf(" icmp_seq = %d. ", icmphdr->i_seq);
printf(" time: %d ms", tick - icmphdr->timestamp);
printf("/n");

icmpcount++;
return;
}

void ValidateArgs(int argc, char **argv)
{
int i;

bRecordRoute = FALSE;
lpdest = NULL;
datasize = DEF_PACKET_SIZE;

for(i = 1; i < argc; i++)
{
if ((argv[0] == '-') || (argv[0] == '/'))
{
switch (tolower(argv[1]))
{
case 'r': // Record route option
bRecordRoute = TRUE;
break;
default:
usage(argv[0]);
break;
}
}
else if (isdigit(argv[0]))
datasize = atoi(argv);
else
lpdest = argv;
}
}

//
// Function: main
//


int main(int argc, char **argv)
{

WSADATA wsaData;
SOCKET sockRaw = INVALID_SOCKET;
struct sockaddr_in dest,
from;
int bread,
fromlen = sizeof(from),
timeout = 1000,
ret;
char *icmp_data = NULL,
*recvbuf = NULL;
unsigned int addr = 0;
USHORT seq_no = 0;
struct hostent *hp = NULL;
IpOptionHeader ipopt;


if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup() failed: %d/n", GetLastError());
return -1;
}
ValidateArgs(argc, argv);


sockRaw = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,
WSA_FLAG_OVERLAPPED);
if (sockRaw == INVALID_SOCKET)
{
printf("WSASocket() failed: %d/n", WSAGetLastError());
return -1;
}
if (bRecordRoute)
{
// Setup the IP option header to go out on every ICMP packet
//
ZeroMemory(&ipopt, sizeof(ipopt));
ipopt.code = IP_RECORD_ROUTE; // Record route option
ipopt.ptr = 4; // Point to the first addr offset
ipopt.len = 39; // Length of option header

ret = setsockopt(sockRaw, IPPROTO_IP, IP_OPTIONS,
(char *)&ipopt, sizeof(ipopt));
if (ret == SOCKET_ERROR)
{
printf("setsockopt(IP_OPTIONS) failed: %d/n",
WSAGetLastError());
}
}
// Set the send/recv timeout values
//
bread = setsockopt(sockRaw, SOL_SOCKET, SO_RCVTIMEO,
(char*)&timeout, sizeof(timeout));
if(bread == SOCKET_ERROR)
{
printf("setsockopt(SO_RCVTIMEO) failed: %d/n",
WSAGetLastError());
return -1;
}
timeout = 1000;
bread = setsockopt(sockRaw, SOL_SOCKET, SO_SNDTIMEO,
(char*)&timeout, sizeof(timeout));
if (bread == SOCKET_ERROR)
{
printf("setsockopt(SO_SNDTIMEO) failed: %d/n",
WSAGetLastError());
return -1;
}
memset(&dest, 0, sizeof(dest));
//
// Resolve the endpoint's name if necessary
//
dest.sin_family = AF_INET;
if ((dest.sin_addr.s_addr = inet_addr(lpdest)) == INADDR_NONE)
{
if ((hp = gethostbyname(lpdest)) != NULL)
{
memcpy(&(dest.sin_addr), hp->h_addr, hp->h_length);
dest.sin_family = hp->h_addrtype;
printf("dest.sin_addr = %s/n", inet_ntoa(dest.sin_addr));
}
else
{
printf("gethostbyname() failed: %d/n",
WSAGetLastError());
return -1;
}
}

//
// Create the ICMP packet
//
datasize += sizeof(IcmpHeader);

icmp_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
MAX_PACKET);
recvbuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
MAX_PACKET);
if (!icmp_data)
{
printf("HeapAlloc() failed: %d/n", GetLastError());
return -1;
}
memset(icmp_data,0,MAX_PACKET);
FillICMPData(icmp_data,datasize);
//
// Start sending/receiving ICMP packets
//
while(1)
{
static int nCount = 0;
int bwrote;

if (nCount++ == 4)
break;

((IcmpHeader*)icmp_data)->i_cksum = 0;
((IcmpHeader*)icmp_data)->timestamp = GetTickCount();
((IcmpHeader*)icmp_data)->i_seq = seq_no++;
((IcmpHeader*)icmp_data)->i_cksum =
checksum((USHORT*)icmp_data, datasize);

bwrote = sendto(sockRaw, icmp_data, datasize, 0,
(struct sockaddr*)&dest, sizeof(dest));
if (bwrote == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAETIMEDOUT)
{
printf("timed out/n");
continue;
}
printf("sendto() failed: %d/n", WSAGetLastError());
return -1;
}
if (bwrote < datasize)
{
printf("Wrote %d bytes/n", bwrote);
}
bread = recvfrom(sockRaw, recvbuf, MAX_PACKET, 0,
(struct sockaddr*)&from, &fromlen);
if (bread == SOCKET_ERROR)
{
if (WSAGetLastError() == WSAETIMEDOUT)
{
printf("timed out/n");
continue;
}
printf("recvfrom() failed: %d/n", WSAGetLastError());
return -1;
}
DecodeICMPHeader(recvbuf, bread, &from);

Sleep(1000);
}
// Cleanup
//
if (sockRaw != INVALID_SOCKET)
closesocket(sockRaw);
HeapFree(GetProcessHeap(), 0, recvbuf);
HeapFree(GetProcessHeap(), 0, icmp_data);

WSACleanup();
return 0;
}
 
看完协议如:ICMP以后,定义一些数据结构以后 就要利用<WINSOCK.H>中提供的函数来实现
这些协议,我的想法对吗?请各位指正。
 
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
 
对不起给位 我的机器不知道不呢什么毛病 怎么在也不能显示 include 中包含的头文件
在include 中应该包括 “插座“头文件(本来要用英文可是在大富翁中显示不出来,只好
用汉语)
 
to 小鱼儿:
不能显示的问题在于你不能直接使用“<>”,而要用……(看看我写的这部分的源代码吧)。
你的问题我也不会!
 
先要懂协议原理(如果要深入研究的话),其中每种协议都有一定的格式,对应到各个
系统平台下就是数据结构了,每个平台下都有一些网络编程用的函数,一般讲,
用这些函数就可以编写一些简单的网络程序了,比如聊天,ftp, ping 程序等等,但是
如果要写一些有分量的程序,象netxray,ipman用的那个包什么的,不懂各种协议原理
根本下不了手,而且我觉得写网络程序还是在unix下好一点,尽量少用delphi,vb的控件,
(赶活时除外,haha).

网络编程主要就是掌握一些网络函数的用法,但是这只能使你入门,要想成为个中手,
高手,不把各种协议吃透就根本别想.我也并不是这方面的高手,但是我见的几个师兄
对http,ftp,smtp,pop3,nntp什么熟的很,而且对各种网络设备也熟的很,没个几年我
看成不了高手吧...
 
大家 都发表一些看法吧!有没有必要研究这些协议 还是自己利用现成的各种控件!!
 
急用的话当然用控件啦,想成为高手的话,就要研究协议了。
 
小鱼儿2000:如果你还要继续讨论请定期提前你的帖子,如果不想继续讨论请结束帖子。
 

先把各种协议吃透.再来考虑网络编程。
 
小鱼儿2000:请自己提前或结束帖子,谢谢合作。
 
小鱼儿2000,你的问题值得讨论,如果要继续请提前,否则请结束问题,谢谢
 
网络编程在win 平台,用VC吧。
 
后退
顶部