A. 怎么样用出C语言写一个分析TCP/IP报文的程
socket编程写了一个简单的DNS服务器。
是这样实现的,用两台PC,一个做客户端,一个做服务器;
在服务器用socket监视53端口,recvfrom()函数负责接收DNS查询报文,将其存入事先写好的数据结构里;
因为DNS查询报文和应答报文的前部分字段格式都是一样的,只是修改了几个参数,然后重点把DNS应答报文后面的字段进行增添。
然后用sendto();函数发送就可以了。
在ie里面输入任意的网址,回车,就会触发客户端向服务器发出DNS查询报文,
此时你可以同时在服务器和客户端抓包,看收到的报文是否正确。
注意几点:
1、一定要熟悉报文结构,定义合适的结构体,将报文进行解析
2、一定要注意网络序和主机序的转换htonl(),htons(),ntohl(),ntohs()等
3、找个熟知端口,最好是你熟悉的协议,如ftp,http,dns等。
4、客户端要把DNS服务器配置为你的服务器ip。
B. Windows系统用C语言写TCP通信
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
typedef struct{
SOCKET accpt;
int lock;
}Arg;
void *transfer(void *arg)
{
Arg * info = (Arg *)arg;
SOCKET clientSock;
char recvbuf[102];
char sendBuf[] = "10";
int ret;
memcpy(&clientSock,(void*)&info->accpt,sizeof(clientSock));
info->lock =1;
while (TRUE)
{
ret = send(clientSock,sendBuf,2,0);
if (ret == -1)
{
break;
}
ret = recv(clientSock,recvbuf,102,0);
printf("%s\n",recvbuf);
}
return (void *)0;
}
void* timer(void *arg)
{
time_t last = time(NULL);
time_t now;
int i = 20;
while(i--)
{
now = time(NULL);
if(now - last == 1)
{
printf("1s past!\n");
last = now;
}
Sleep(500);
}
printf("timer exit.\n");
return (void *)0;
}
int main(void) {
WSADATA wsaData;
SOCKET ListenSocket;
SOCKADDR_IN service,client;
int len = sizeof(client);
Arg argument;
pthread_t tid;
char sendBuf[] = "ID=2;WHAT=host";
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
printf("Error at WSAStartup()\n");
return 1;
}
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(27115);
if (bind( ListenSocket,
(SOCKADDR*) &service,
sizeof(service)) == SOCKET_ERROR) {
printf("bind() failed.\n");
closesocket(ListenSocket);
WSACleanup();
return 1;
}
if (listen( ListenSocket, 1 ) == SOCKET_ERROR) {
printf("Error listening on socket.\n");
closesocket(ListenSocket);
WSACleanup();
return 1;
}
argument.lock = 1;
printf("Waiting for client to connect...\n");
pthread_create(&tid,NULL,timer,NULL);
while(TRUE){
argument.accpt = accept( ListenSocket, (SOCKADDR*)&client, &len );
if (argument.accpt == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
} else {
printf("accept%s:%d\n",inet_ntoa(client.sin_addr),client.sin_port);
while (!argument.lock);
argument.lock = 0;
// sendto(argument.accpt,sendBuf,sizeof(sendBuf),0,(SOCKADDR*)&client,len);
pthread_create(&tid,NULL,transfer,&argument);
//send(AcceptSocket,sendBuf,sizeof(sendBuf),0);
}
}
// No longer need server socket
closesocket(ListenSocket);
WSACleanup();
return 0;
}
C. 求C语言高手,实现一个简单的TCPIP程序以实现两台计算机之间的聊天通信,
你上面给出的代码其实就是MSDN里面的演示代码,不过不完整,只演示了两个函数的使用,我给你看看我写的TCP通讯程序,可以在同一个局域网内的两台不同计算机之间聊天:
这其实就是某本将网络通讯的教程里面的例子,不过是我自己重写了一遍,下面给你代码:
========================
下面是公共代码:
========================
#ifndef__CINITSOCK__H__
#define__CINITSOCK__H__
#include<winsock2.h>
#include<iphlpapi.h>
#pragmacomment(lib,"ws2_32.lib")
#pragmacomment(lib,"iphlpapi.lib")
classCInitSock
{
public:
CInitSock(intnMinorVer=2,intnMajorVer=2)
{
WSADATAwsData;
WORDwVer=MAKEWORD(nMinorVer,nMajorVer);
if(0!=WSAStartup(wVer,&wsData))exit(0);
}
~CInitSock()
{
WSACleanup();
}
};
#endif
========================
下面是客户端的代码:
=======================
#include"CInitSock.h"
#include<iostream>
usingnamespacestd;
CInitSockg_Sock;
voidmain()
{
SOCKETsockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sockClient)
return;
sockaddr_insockAddr;
sockAddr.sin_family=AF_INET;
sockAddr.sin_port=htons(4567);
sockAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
if(-1==connect(sockClient,(sockaddr*)&sockAddr,sizeof(sockAddr)))
{
cout<<"connectfailed"<<endl;
return;
}
while(true)
{
charszBuf[MAX_PATH];
ZeroMemory(szBuf,sizeof(szBuf));
cout<<"YouSay:";
cin>>szBuf;
if(SOCKET_ERROR ==send(sockClient,szBuf,MAX_PATH,0))
{
cout<<"sendfailed"<<endl;
return;
}
intnRecvLen=recv(sockClient,szBuf,MAX_PATH,0);
if(nRecvLen>0)
{
//szBuf[nRecvLen]='