|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
*************************************send***********************
*****************************************************************
*****************************************************************
#include <stdio.h>
#include <winsock2.h>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
int crc;
int create(int data,int POLY,int crcbitnumber)
{
int data_temp=data;
int databitnumber=12;
int regi = 0x0;
data <<= crcbitnumber;
for ( int cur_bit = databitnumber+crcbitnumber-1; cur_bit >= 0; -- cur_bit )
{
if ( ( ( regi >> crcbitnumber ) & 0x0001 ) == 0x1 ) regi = regi ^ POLY;
regi <<= 1;
int tmp = ( data >> cur_bit ) & 0x0001;
regi |= tmp;
}
if ( ( ( regi >> crcbitnumber ) & 0x0001 ) == 0x1 ) regi = regi ^ POLY;
printf("crc=%x\n",regi);
crc=regi;
data_temp<<=12;
data_temp=data_temp+regi;
return data_temp;
}
#pragma comment(lib, "ws2_32.lib")
void main()
{
// 初始化
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// 建立socket
SOCKET server;
server = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( server == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}
// 绑定socket
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = htonl( INADDR_ANY);
service.sin_port = htons(8000);
if ( bind( server, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) {
printf( "bind() failed.\n" );
closesocket(server);
return;
}
// 监听 socket
if ( listen( server, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n");
// 接受连接
SOCKET AcceptSocket;
printf( "Waiting for a client to connect...\n" );
while (1) {
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( server, NULL, NULL );
}
printf( "Client Connected.\n");
server = AcceptSocket;
break;
}
FILE *lp_code;
int code;
int data;
int POLY=0x180d;
int crc;
int crcbitnumber=12;
printf("请输入数据:");
scanf("%x",&data);
code=create(data,POLY,crcbitnumber);
printf("code=%x\n",code);
lp_code=fopen("d:\\send.txt","w");
fprintf(lp_code,"%x",code);
getchar();
fclose(lp_code);
char sendbuf[1024];//发送缓冲区
DWORD dwRead;
BOOL bRet;
char filename[100];
memset(filename,0,100);
recv(server,filename,100,0);
HANDLE hFile=CreateFile(filename,GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
while(true)
{
bRet=ReadFile(hFile,sendbuf,1024,&dwRead,NULL);
if(bRet==FALSE)
{
MessageBox(NULL,"File not found!","Error",MB_OK);
break;
}
else if(dwRead==0)
{
MessageBox(NULL,"Send file OK!","OK",MB_OK);
break;
}
else
{
send(server,sendbuf,dwRead,0);
}
};
CloseHandle(hFile);
return;
}
************************************recv**********************
**************************************************************
*************************************************************
#include <stdio.h>
#include <winsock2.h>
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
int exam(int data,int POLY,int crcbitnumber)
{
int regi=0x0;
int data_temp;
data_temp=data;
int databitnumber=12;
for(int cur_bit=databitnumber+crcbitnumber-1;cur_bit>=0;--cur_bit)
{
if ( ( ( regi >> crcbitnumber ) & 0x0001 ) == 0x1 ) regi = regi ^ POLY;
regi <<= 1;
unsigned short tmp = ( data >> cur_bit ) & 0x0001;
regi |= tmp;
if ( ( ( regi >> crcbitnumber ) & 0x0001 ) == 0x1 ) regi = regi ^ POLY;
}
return regi;
}
#pragma comment(lib, "ws2_32.lib")
void main() {
// 初始化 Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// 建立socket socket.
SOCKET client;
client = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( client == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}
// 连接到服务器.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 8000 );
if ( connect( client, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
char recvbuf[1024];//发送缓冲区
int read;
DWORD dwWrite;
BOOL bRet;
printf("please input the route:\n");
char buf[100];
gets(buf);
send(client,buf,100,0);
char filename[]="d:\\recv.txt";
HANDLE hFile=CreateFile(filename,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
while(true)
{
read=recv( client, recvbuf, 1024, 0 );
if(read==-1)break;
bRet=WriteFile(hFile,recvbuf,read,&dwWrite,NULL);
if(bRet==FALSE)
{
MessageBox(NULL,"SFile not found!","OK",MB_OK);
break;
}
}
FILE *lp_code2;
int code;
int result;
int POLY=0x180d;
int crcbitnumber=12;
lp_code2=fopen("d:\\recv.txt","r");
fscanf(lp_code2,"%x\n",&code);
printf("code=%x\n",code);
fclose(lp_code2);
result=exam(code,POLY,crcbitnumber);
printf("result=%x",result);
if(result==0)
{
printf("数据传输正确\n");
code>>=12;
printf("去除CRC编码后得到的信息为:%x\n",code);
}
else
printf("数据传输失败");
system("pause");
return;
}
|
|