📄 client.c
字号:
#include "client.h"
void main()
{
clientInit();
client();
closesocket( localSkt );
}
void clientInit()
{
struct sockaddr_in clientAddr;
struct hostent far *host;
struct in_addr *ip;
char serverIP[20];
char name[40];
unsigned long lRemIP;
char showSvr[20];
char showClt[20];
WSADATA wsaData;
WSAStartup( WINSOCK_VERSION , &wsaData ) ;
localSkt = socket( AF_INET , SOCK_DGRAM , 0 );
/*get local IP address*/
gethostname( name , sizeof(name) ) ;
host = gethostbyname( name ) ;
ip = (struct in_addr*)*host->h_addr_list ;
/*bind local IP address to localSkt*/
clientAddr.sin_addr = *ip;
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons( LOCAL_PORT );
/*can NOT bind*/
/*get server IP*/
printf( "Please enter the server IP:" );
gets( serverIP );
if ( strlen( serverIP ) )
lRemIP = inet_addr( serverIP );
else
lRemIP = inet_addr(SERVER_IP);
serverAddr.sin_addr.S_un.S_addr = lRemIP;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT);
strcpy( showSvr,inet_ntoa(serverAddr.sin_addr) );
printf( "Server IP: %s " , inet_ntoa( serverAddr.sin_addr ));
strcpy( showClt,inet_ntoa(clientAddr.sin_addr) );
printf( "Local IP:%s\n" ,inet_ntoa(clientAddr.sin_addr) );
printf( "Client Initiate Successfully\nBegin to communicate:\n" ) ;
printf( "*************************************************************\n" ) ;
}
void client()
{
int isFirstPkt = 0;
int flagResend = 0;
int bfsz = MSG_MAX_SIZE;
int totalSize = 0; /*the size of the file to be send*/
short int seqNum = 0;
int location = 0;
unsigned char msg[MSG_MAX_SIZE];
unsigned char packet[MSG_MAX_SIZE+PKT_CHECKSUM_SIZE+PKT_SEQNUM_SIZE+PKT_MSG_SIZE];
unsigned char recvMsg[MSG_MAX_SIZE];
unsigned char recvPacket[MSG_MAX_SIZE+PKT_CHECKSUM_SIZE+PKT_SEQNUM_SIZE+PKT_MSG_SIZE];
int msgLen;
int addrSize = sizeof(serverAddr);
int res;
fd_set readSet;
struct timeval tv;
char name[NAME_SIZE];
printf( "Please enter the file name:" );
gets( name );
fSrc = fopen( name,"rb" );
totalSize = getFileSize( fSrc );
while ( location != totalSize &&bfsz != 0 )
{
if ( flagResend == 0 )/*if send new data*/
{
/*read data from file*/
memset( msg,0,sizeof(msg) );
memset( packet,0,sizeof(packet) );
if ( isFirstPkt == 0 )/*prepare the first packet*/
{
_32to8( msg,totalSize );
packageMsg( packet,msg,0,seqNum );
}
else
{
memset( msg,0,sizeof(msg) );
if ( fread( msg,1,bfsz,fSrc ) )
{
location += bfsz;
packageMsg( packet,msg,bfsz,seqNum );
}
else
{
fseek( fSrc,location,0 );
bfsz = bfsz/2;
continue;
}
}
}
else
printf( "Msg Resend--" );
printf( "SeqNum %d \n %s\n",seqNum,msg );
if ( sendto( localSkt,packet,sizeof(packet),0,(struct sockaddr*)&serverAddr,
sizeof(serverAddr) )
== SOCKET_ERROR)
printf( "Send Error----%d\n",WSAGetLastError() );
else
printf( "%d************************************************Message SendOut\n",location );
/*wait for ACK*/
memset( recvPacket,0,sizeof(recvPacket) );
memset( recvMsg,0,sizeof(recvMsg) );
while (1)
{
tv.tv_sec = 11;
tv.tv_usec = 0;
FD_ZERO(&readSet);
FD_SET(localSkt, &readSet);
res = select(localSkt+1, &readSet, NULL, NULL, &tv);
if(res == SOCKET_ERROR)
{
printf("Socket Error on selecting!\n");
break;
}
if(res == 0) //timeout
{
printf("Request timed out.\n");
flagResend = 1;
break;
}
if(FD_ISSET(localSkt, &readSet))
{
msgLen = recvfrom( localSkt,recvPacket,sizeof(recvPacket),0,
(struct sockaddr*)&serverAddr,&addrSize );
if ( msgLen > 0 && !checkError( recvPacket ) )
{
flagResend = 0;
disPacketMsg( recvMsg,recvPacket );
printf( "Receive from %s:\n %d %s\n",inet_ntoa( serverAddr.sin_addr ),
getSeqNum(recvPacket),recvMsg );
printf( "*************************************************************\n" );
if ( getSeqNum(recvPacket) == seqNum )
{
if ( isFirstPkt == 0 )
isFirstPkt = 1;
seqNum = (seqNum+1)%SPACE_NUM;
break;
}
}
flagResend = 1;
break;
}
}
}
}
int getFileSize(FILE *fSrc)
{
int size = 0;
int bfsz = MSG_MAX_SIZE;
char temp[MSG_MAX_SIZE];
while ( bfsz != 0 )
{
memset( temp,0,sizeof(temp) );
if ( fread( temp,1,bfsz,fSrc ) )
{
size += bfsz;
}
else
{
fseek( fSrc,size,0 );
bfsz = bfsz/2;
}
}
fseek( fSrc,0,0 );
return size;
}
void _32to8(unsigned char* buffer,int size)
{
int i=0;
while ( i<4 )
{
buffer[i] = (size>>(32-(i+1)*8))&0x00ff;
i++;
}
}
int _8to32(unsigned char *buffer)
{
return ((buffer[0]*0x1000000)&0xff000000)+((buffer[1]*0x10000)&0xff0000)
+((buffer[2]*0x100)&0xff00)+buffer[3];
}
void packageMsg(unsigned char *dstMsg,unsigned char *msg,int msgSize,int seqNum )
{
unsigned int sum = 0;
unsigned int hWord = 0;
int i = 0;
unsigned char temp[4];
unsigned char seq[2];
for ( i=0;i<MSG_MAX_SIZE;i++ )
{
dstMsg[PKT_CHECKSUM_SIZE+PKT_SEQNUM_SIZE+PKT_MSG_SIZE+i] = msg[i];
}
_16to8( seq,seqNum );
dstMsg[2] = seq[0];
dstMsg[3] = seq[1];
_32to8( temp,msgSize );
dstMsg[4] = temp[0];
dstMsg[5] = temp[1];
dstMsg[6] = temp[2];
dstMsg[7] = temp[3];
i = 0;
while ( i < MSG_MAX_SIZE+PKT_CHECKSUM_SIZE+PKT_SEQNUM_SIZE+PKT_MSG_SIZE )
{
sum += dstMsg[i]*0x100+dstMsg[i+1];
i = i + 2;
}
hWord = (sum>>16)&0x0000ffff;
sum = sum &0x0000ffff;
sum = sum + hWord;
sum = ~sum;
_32to8( temp,sum );
dstMsg[0] = temp[2];
dstMsg[1] = temp[3];
}
int disPacketMsg( unsigned char *dstMsg,unsigned char *srcMsg )
{
int i;
unsigned char temp[4];
for ( i = 0;i < MSG_MAX_SIZE;i ++ )
{
dstMsg[i] = srcMsg[PKT_CHECKSUM_SIZE+PKT_SEQNUM_SIZE+PKT_MSG_SIZE+i];
}
temp[0] = srcMsg[4];
temp[1] = srcMsg[5];
temp[2] = srcMsg[6];
temp[3] = srcMsg[7];
return _8to32( temp );
}
int checkError( unsigned char* msg )
{
unsigned int sum = 0;
unsigned int hWord = 0;
int i = 0;
while ( i < PKT_CHECKSUM_SIZE+PKT_SEQNUM_SIZE+MSG_MAX_SIZE+PKT_MSG_SIZE )
{
sum += ((msg[i]*0x100)&0xff00) + msg[i+1];
i = i + 2;
}
hWord = (sum>>16)&0x0000ffff;
sum = sum &0x0000ffff;
sum = sum + hWord;
return (sum^0xffff);
}
int getSeqNum( unsigned char *pkt )
{
return ( ((pkt[2]*0x100)&0xff00)+pkt[3] );
}
void _16to8( unsigned char *buffer,int value )
{
buffer[0] = ((value>>8)&0xff);
buffer[1] = value&0xff;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -