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]='