コード例 #1
0
/**
  * @brief  check the CRC value, return 1 if pass, otherwise 0;
  */
unsigned int wy_CRC_check(char *_buffer)
{
	char bf[BUFFESIZE];
	char temp[BUFFESIZE]="\0";
	char sep[2] = " "; //separation is space
	char *word;
	int  wcount=0;
	unsigned int  CRC_client=0;
	
	strcpy(bf, _buffer);
	sscanf(bf, "%d", &CRC_client);

	for (word = strtok(bf, sep);word;word = strtok(NULL, sep))
	{
		wcount++;
		if(wcount > 1) // jump the first space"_".
		{
			strcat(temp,word);
			strcat(temp," ");
		}	
	}
	temp[strlen(temp)-1] = 0;// delete the last space .
	
	//printf("test: CRC is %d     the sting is %s  the first is :%d",CRCpolynomial(temp),temp,temp[0]);
	//printf("test: the result is %d,",CRC_client == CRCpolynomial(temp));
	return CRC_client == CRCpolynomial(temp);
}
コード例 #2
0
ファイル: mylib.c プロジェクト: scottszb1987/ShiSimpleTCP
void make_pkt_CLS(int expectseqnum , char* pkt){
    char temp[78] = "";
    sprintf(temp, "CLS %d", expectseqnum);
    int crc;
    crc = (int)CRCpolynomial(temp);
    sprintf(pkt,"%d ",crc);
    strcat(pkt,temp);
}
コード例 #3
0
ファイル: sctbase.c プロジェクト: scottszb1987/ShiSimpleTCP
int compute_crc_with_newline(const char* message){
    int crc;
    char temp[78];
    strcpy(temp,message);
    strcat(temp,"\n");
    crc = (int)CRCpolynomial(temp);
    return crc;
}
コード例 #4
0
ファイル: sctbase.c プロジェクト: scottszb1987/ShiSimpleTCP
void make_pkt(int nextseqnum , const char* header , const char* data ,char* pkt ){
    char temp[78]="";
    sprintf(temp , "%s %d " ,header, nextseqnum);
    strcat(temp,data);
    int crc;
    crc = (int)CRCpolynomial(temp);
    sprintf(pkt, "%d ", crc);
    strcat(pkt,temp);
}
コード例 #5
0
ファイル: mylib.c プロジェクト: scottszb1987/ShiSimpleTCP
void make_pkt_close(int nextseqnum, char* pkt){
    int crc;
    char temp[78] = "";
    sprintf(temp , "CLOSE %d end of file\n",nextseqnum);
    char temp2[78];
    chopoffnewline(temp,temp2);
    crc = (int)CRCpolynomial(temp2);
    sprintf(pkt,"%d ",crc);
    strcat(pkt,temp);

}
コード例 #6
0
ファイル: mylib.c プロジェクト: scottszb1987/ShiSimpleTCP
void make_pkt(int nextseqnum , const char* data ,char* pkt ){
    char temp[78]="";
    sprintf(temp , "PACKET %d " , nextseqnum);
    strcat(temp,data);
    int crc;
    char temp2[78] = "";
    chopoffnewline(temp,temp2);
    crc = (int)CRCpolynomial(temp2);
    sprintf(pkt, "%d ", crc);
    strcat(pkt,temp);
}
コード例 #7
0
/**
  * @brief  build  ACK packet using SN_checked;
  */
void wy_ACK_build(char *_buffer, int _sn)
{
	char CRC_buffer[10],temp_buffer[BUFFESIZE];
	
	sprintf(temp_buffer,"ACKNOW %d", _sn);
	sprintf(CRC_buffer, "%d", CRCpolynomial(temp_buffer));
	strcat(CRC_buffer," ");
	strcat(CRC_buffer,temp_buffer);
	strcpy(temp_buffer,CRC_buffer);
	strcat(temp_buffer," \r\n");
	strcpy(_buffer, temp_buffer);
}
コード例 #8
0
ファイル: test.c プロジェクト: scottszb1987/ShiSimpleTCP
char* make_pkt(char* send_buffer, int counter){
    char temp_buffer[80];
    char temp_buffer2[80];
    sprintf(temp_buffer,"PACKET %d ",counter);
    strcat(temp_buffer,send_buffer);

    unsigned int crc_value;
    crc_value = CRCpolynomial(temp_buffer);
    sprintf(temp_buffer2,"%u ",crc_value);
    strcat(temp_buffer2,temp_buffer);

    return temp_buffer2;
}
コード例 #9
0
/* MAIN------------------------------------------------------------------------*/
int main(int argc, char *argv[]) 
{
   // WSSTARTUP
	if (WSAStartup(WSVERS, &wsadata) != 0) 
	{
		WSACleanup();
		printf("WSAStartup failed\n");
	}

	// Initialization
	memset(&localaddr, 0, sizeof(localaddr));//clean up
   int localPort=1234;
   localaddr.sin_family = AF_INET;
   localaddr.sin_addr.s_addr = INADDR_ANY;//server address should be local
   localaddr.sin_port = htons(localPort);
   memset(&remoteaddr, 0, sizeof(remoteaddr));//clean up  
   randominit();

   SOCKET s;

   char send_buffer[BUFFESIZE],receive_buffer[BUFFESIZE];
   remoteaddr.sin_family = AF_INET;

	//	Dealing with user's arguments
   if (argc != 5) 
	{
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
      exit(1);
   }
   remoteaddr.sin_addr.s_addr = inet_addr(argv[1]);//IP address
   remoteaddr.sin_port = htons((u_short)atoi(argv[2]));//always get the port number
   packets_lostbit=atoi(argv[3]);
   packets_damagedbit=atoi(argv[4]);
   if (packets_damagedbit<0 || packets_damagedbit>1 || packets_lostbit<0 || packets_lostbit>1)
	{
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
	   exit(0);
   }
	

	//CREATE CLIENT'S SOCKET 
   s = socket(AF_INET, SOCK_DGRAM, 0);//this is a UDP socket
   if (s < 0) 
	{
      printf("socket failed\n");
   	exit(1);
   }
	
   //NONBLOCKING OPTION for Windows
   u_long iMode=1;
   ioctlsocket(s,FIONBIO,&iMode);


	//Open A TEXT FILE 
   int counter=0;//sequence of packets
   int ackcounter=0;
   char temp_buffer[BUFFESIZE];
	char CRC_str[BUFFESIZE];

   FILE *fin=fopen("file1_Windows.txt","r");
   if(fin==NULL)
	{
	   printf("cannot open file\n");
	   exit(0);
   }
	
   while (1)
	{
	memset(send_buffer, 0, sizeof(send_buffer));//clean up the send_buffer before reading the next line
	fgets(send_buffer,SEGMENTSIZE,fin);
	if (!feof(fin)) {
		
		//add a headert to the packet with the sequence number
		sprintf(temp_buffer,"PACKET %d ",counter);
		counter++;
		strcat(temp_buffer,send_buffer);
		strcpy(send_buffer,temp_buffer);
		
		//---------coding-------
		temp_buffer[strlen(temp_buffer)-1] = 0;
		sprintf(CRC_str,"%d ",CRCpolynomial(temp_buffer));
		strcat(CRC_str,send_buffer);
		strcpy(send_buffer, CRC_str);
		//----------------------
		
		send_unreliably(s,send_buffer, remoteaddr);		

		Sleep(1000);
		

		//RECEIVE
		memset(receive_buffer, 0, sizeof(receive_buffer));
		recv_nonblocking(s,receive_buffer, remoteaddr);//you can replace this, but use MSG_DONTWAIT to get non-blocking recv
		printf("RECEIVE -->%s__\n",receive_buffer);
		
		
		
		
		//printf("@Alan the CRC check result is %d\n",wy_CRC_check(receive_buffer));
		
		
		

		Sleep(1000);
	}
	else 
	{
		printf("end of the file \n"); 
		memset(send_buffer, 0, sizeof(send_buffer)); 
		sprintf(send_buffer,"CLOSE \r\n");
		send_unreliably(s,send_buffer,remoteaddr);//we actually send this reliably, read UDP_supporting_functions_2012.c
		
		break;
	}
   }

	//CLOSESOCKET   
   printf("closing everything on the client's side ... \n");
   fclose(fin);

   closesocket(s);
   exit(0);
}
コード例 #10
0
// Simple usage: client IP port, or client IP (use default port) 
int main(int argc, char *argv[]) {
#if defined __unix__ || defined __APPLE__
	
#elif defined _WIN32 
//********************************************************************
// WSSTARTUP
//********************************************************************
	if (WSAStartup(WSVERS, &wsadata) != 0) {
		WSACleanup();
		printf("WSAStartup failed\n");
	}
#endif
//*******************************************************************
// Initialization
//*******************************************************************
   memset(&localaddr, 0, sizeof(localaddr));//clean up
   //char localIP[INET_ADDRSTRLEN]="127.0.0.1";
   int localPort=1234;
   localaddr.sin_family = AF_INET;
   //localaddr.sin_addr.s_addr = inet_addr(localIP);
   localaddr.sin_addr.s_addr = INADDR_ANY;//server address should be local
//********************************************************************
   localaddr.sin_port = htons(localPort);
   memset(&remoteaddr, 0, sizeof(remoteaddr));//clean up  
   randominit();
#if defined __unix__ || defined __APPLE__
   int s;
#elif defined _WIN32
   SOCKET s;
#endif
   char send_buffer[BUFFESIZE],receive_buffer[BUFFESIZE];
   remoteaddr.sin_family = AF_INET;
//*******************************************************************
//	Dealing with user's arguments
//*******************************************************************
   if (argc != 5) {
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
      exit(1);
   }
   remoteaddr.sin_addr.s_addr = inet_addr(argv[1]);//IP address
   remoteaddr.sin_port = htons((u_short)atoi(argv[2]));//always get the port number
   //localaddr.sin_port = htons((u_short)atoi(argv[3]));//always get the port number
   packets_lostbit=atoi(argv[3]);
   packets_damagedbit=atoi(argv[4]);
   if (packets_damagedbit<0 || packets_damagedbit>1 || packets_lostbit<0 || packets_lostbit>1){
	   printf("2011 code USAGE: client remote_IP-address port  lossesbit(0 or 1) damagebit(0 or 1)\n");
	   exit(0);
   }
//*******************************************************************
//CREATE CLIENT'S SOCKET 
//*******************************************************************
   s = socket(AF_INET, SOCK_DGRAM, 0);//this is a UDP socket
   if (s < 0) {
      printf("socket failed\n");
   	exit(1);
   }
#if defined __unix__ || defined __APPLE__

#elif defined _WIN32
   //***************************************************************//
   //NONBLOCKING OPTION for Windows
   //***************************************************************//
   u_long iMode=1;
   ioctlsocket(s,FIONBIO,&iMode);
#endif
//*******************************************************************
//SEND A TEXT FILE 
//*******************************************************************
   int counter=0;//sequence of packets
   int ackcounter=0;
   char temp_buffer[BUFFESIZE];
	char CRC_str[BUFFESIZE];
#if defined __unix__ || defined __APPLE__
   FILE *fin=fopen("file1.txt","r");
#elif defined _WIN32
   FILE *fin=fopen("file1_Windows.txt","r");
#endif
   if(fin==NULL){
	   printf("cannot open file\n");
	   exit(0);
   }
   while (1){
	memset(send_buffer, 0, sizeof(send_buffer));//clean up the send_buffer before reading the next line
	fgets(send_buffer,SEGMENTSIZE,fin);
	if (!feof(fin)) {
		
		//add a headert to the packet with the sequence number
		sprintf(temp_buffer,"PACKET %d ",counter);
		counter++;
		strcat(temp_buffer,send_buffer);
		strcpy(send_buffer,temp_buffer);
		
		//---------coding-------
		temp_buffer[strlen(temp_buffer)-1] = 0;
		sprintf(CRC_str,"%d ",CRCpolynomial(temp_buffer));
		strcat(CRC_str,send_buffer);
		strcpy(send_buffer, CRC_str);
		//printf("testing:the CRC is %d,lenth is %d \n",CRCpolynomial(temp_buffer),strlen(temp_buffer));
		//printf("len= %d    the last four :%d %d %d %d",strlen(temp_buffer),temp_buffer[34],temp_buffer[35],temp_buffer[36],temp_buffer[37]);
		//----------------------
		
		send_unreliably(s,send_buffer, remoteaddr);
		
#if defined __unix__ || defined __APPLE__
		sleep(1);
#elif defined _WIN32
		Sleep(1000);
#endif
	//********************************************************************
	//RECEIVE
	//********************************************************************
		memset(receive_buffer, 0, sizeof(receive_buffer));
		recv_nonblocking(s,receive_buffer, remoteaddr);//you can replace this, but use MSG_DONTWAIT to get non-blocking recv
		printf("RECEIVE --> %s \n",receive_buffer);
#if defined __unix__ || defined __APPLE__
		sleep(1);//wait for a bit before trying the next packet
#elif defined _WIN32
		Sleep(1000);
#endif
	}
	else {
		printf("end of the file \n"); 
		memset(send_buffer, 0, sizeof(send_buffer)); 
		sprintf(send_buffer,"CLOSE \r\n");
		send_unreliably(s,send_buffer,remoteaddr);//we actually send this reliably, read UDP_supporting_functions_2012.c
		
		break;
	}
   }
//*******************************************************************
//CLOSESOCKET   
//*******************************************************************
   printf("closing everything on the client's side ... \n");
   fclose(fin);
#if defined __unix__ || defined __APPLE__
   close(s);
#elif defined _WIN32
   closesocket(s);
#endif

   exit(0);

}