بررسی و تحلیل یک قطعه کد در سوکت پروگرمینگ ( Socker Programming )

شرح پروژه :یک پرسش و پاسخ بین client و server است. ابتدا کلاینت وسرور به یکدیگر سلام میدهند. وبعد کلاینت از سرور تقاضای کمک میکند.سرور میتواند قبول و یا رد کند. اگر تقاضا رد شود کلاینت پیغام thank you میدهد و ارتباط قطع میشود.در غیر این صورت شروع به پرسیدن سوالات از پیش تعریف شده از سرور میکند. و با پیغام thank you از طرف client برنامه به اتمام میرسد.نکته :برای برقراری ارتباط بین دو پروسه، حداقل نیاز است که یکی سرویس دهنده ( سرور ) و یکی سرویس گیرنده ( کاربر ) وجود باشد.

دوره های شبکه، برنامه نویسی، مجازی سازی، امنیت، نفوذ و ... با برترین های ایران

کد برنامه ی سرور به زبان C :

/****************** SERVER CODE ****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int welcomeSocket, newSocket;
char buffer[1024];
void answer ();
int main(){
  
  
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(7891);
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/
  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  /*---- Listen on the socket, with 5 max connection requests queued ----*/
  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/
  addr_size = sizeof serverStorage;
  newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
  
  
  /*---- Read the message from the server into the buffer ----*/
  recv(newSocket, buffer, 1024, 0);

  /*---- Print the received message ----*/
  printf("Data received: %s",buffer);
  strcpy(buffer,"\0");

  /*---- Send message to the socket of the incoming connection ----*/
  strcpy(buffer,"Hello client\n");
  sleep(1);
  send(newSocket,buffer,13,0);

  
  /*---- Read the message from the server into the buffer ----*/
  recv(newSocket, buffer, 1024, 0);
  
  /*---- Print the received message ----*/
  printf("Data received: %s\n",buffer);
  strcpy(buffer,"\0");

  
  answer();  


  return 0;
}
void answer (){ 
  scanf("%s",buffer);
  send(newSocket,buffer,13,0);
  char b[1024];
  time_t t = time (NULL);
  struct tm *tm = localtime(&t);
  strcpy(b,asctime(tm));
  char f=b[0];
  

  while(1){
        
	strcpy(buffer,"");
	recv(newSocket, buffer, 1024, 0);
  	/*---- Print the received message ----*/
        printf("Data received: %s\n",buffer);
        
  	if (strcmp(buffer,"thank you")==0){
		close(newSocket);
		printf("close");
		sleep(2);
		break;
	}
  	else if(strcmp(buffer,"date and time?")==0){ 
		strcpy(buffer,"\0");
		time_t t = time (NULL);
  		struct tm *tm = localtime(&t);
 		strcpy(buffer,asctime(tm));
  		send(newSocket,buffer,90,0);
	}
	else if(strcmp(buffer,"what is the today?")==0){
		strcpy(buffer,"\0");
  		if (f=='f'){
  			strcpy(buffer,"holiday\n");
 			send(newSocket,buffer,13,0);
		}
		else{
  			strcpy(buffer,"workday\n");
  			send(newSocket,buffer,13,0);
		}
	}
	else if(strcmp(buffer,"what is the weather?")==0){	
		strcpy(buffer,"");
		char *wea[5]={"rainy","sunny","foggy","cloudy","snowy"};
  		srand(time(0));
  		int r = rand()%5;
  		strcpy(buffer,wea[r]);
  		send(newSocket,buffer,13,0);
	}
	else{
		strcpy(buffer,"");
		strcpy(buffer,"the question, not found");
  		send(newSocket,buffer,25,0);	
	}
}
}

تحلیل کد بالا:

  1. تابع socket یک عدد int به عنوان socket descriptor برمیگرداند. چون از IPv4 استفاده میکنیم، به عنوان Internet Family برای IPv4 از کلمه کلیدی PFINET استفاده میکنیم. در آرگومان دوم این تابع مشخص میکنیم که از لایه انتقال مدل TCPIP استفاده کند (SOCKSTREAM). آرگومان سوم هم معمولا صفر است .
  2. تابع bind جزئیاتی که در ساختار serverAddr وجود دارد را به سوکتی که در مرحله قبل ایجاد شد اضافه میکند.
  3. در تابع listen آرگومان دوم که عدد 5 گرفته یعنی اینکه سرور حداکثر 5 تا connection از طرف سوکت کاربر قبول میکند.
  4. پس از اینکه تابع listen با موفقیت ایجاد شد، سوکت بصورت عملیاتی شروع به گوش دادن میکند.
  5. هنگامی که یک درخواست از طرف کاربر رسید، در سرور زمان و تاریخ محاسبه شده و از طریق descriptor که توسط تابع accept برگردانده میشود، درون سوکت ارتباطی با کاربر نوشته میشود(newSocket) و کاربر آنرا دریافت میکند. سوکت ارتباطی طرف کاربر در serverStorage قرار میگیرد و اندازه ی آن نیز در addr_size قرار میگیرد.
/****************** CLIENT CODE ****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/unistd.h>


int main(){
  int clientSocket;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  clientSocket = socket(PF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(7891);
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Connect the socket to the server using the address struct ----*/
  addr_size = sizeof serverAddr;
  connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
  /*---- Send message to the socket of the incoming connection ----*/
  strcpy(buffer,"hello server\n");
  send(clientSocket,buffer,13,0);

  /*---- Read the message from the server into the buffer ----*/
  recv(clientSocket,buffer, 1024, 0);
  /*---- Print the received message ----*/
  printf("Data received: %s\n",buffer);   
  strcpy(buffer,"");

  
  /*---- Send message to the socket of the incoming connection ----need*/
  strcpy(buffer,"i need some help and i have some question .can i ask them ?");
  sleep(2);
  send(clientSocket,buffer,80,0);
  
  /*---- Read the message from the server into the buffer ----*/
  recv(clientSocket, buffer, 1024, 0);
  /*---- Print the received message ----*/
  printf("Data received: %s\n",buffer); 
  char d=buffer[0];  
  strcpy(buffer,"");
  if(d=='Y'||d=='y'){
	while (1){
		printf("what the question?\n");
		strcpy(buffer,"");
  		/*---- Send message to the socket of the incoming connection ----date and time*/
		gets(buffer);
  		send(clientSocket,buffer,30,0);
		if (strcmp(buffer,"thank you")==0){close(clientSocket);break;}
		/*---- Read the message from the server into the buffer ----*/
 		recv(clientSocket, buffer, 1024, 0);
  		/*---- Print the received message ----*/
  		printf("Data received: %s\n",buffer); 
		strcpy(buffer,"");
		
	}//while 1
  }//end of if d
  else {
  	/*---- Send message to the socket of the incoming connection ----*/
  	strcpy(buffer,"thank you\n");
  	send(clientSocket,buffer,18,0);
  	close (clientSocket);
  }
  return 0;
}

در کد کلاینت نیز سوکتی ساخته شده و مانند کد سرور است.با تابع connect به server متصل میشویم . امیدوارم به کارتان بیاید.


نظرات