如何返回Socket Client-Server中的菜单

时间:2015-11-16 09:11:01

标签: sockets server

我写了这段代码,但它无法正常工作。 这是菜单界面:

*************ENCODE_DECODE_BASE64**************

*******             1. Encode          ********

*******             2. Decode          ********

*******             3. Exit            ********

***********************************************

当我选择1时,“编码”。该函数运行但进程退出。

我希望当我选择1,运行的功能然后再运行菜单。

你能帮助我吗?

这是Clientcode:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h> 
#include <netinet/in.h>  
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1024
// read filename
char *inputString(int size){
  int test=0;
  char *s=(char*)malloc(size);
  do{
    if(test!=0){
      printf("File not found !!!!!!");
    }
  fgets(s,size,stdin);
  test++;
  }while(strlen(s)<=1);
  return strtok(s,"\n");
}

int main(int argc, char *argv[]){
int sockfd;
struct sockaddr serverAddr; //server address
char buff[1024]; 
struct sockaddr_in inAddr;
long int sentBytes,revedBytes;
sockfd=socket(AF_INET,SOCK_STREAM,0); // create socket
if (sockfd == -1)
    {
        printf("ERROR opening socket\n");
        return 1;
    }
printf("Socket done\n");
inAddr.sin_family=AF_INET; //default
inAddr.sin_port=htons(5500); // service port
inet_aton("127.0.0.1",&inAddr.sin_addr); 
//connectting
if(connect(sockfd,(struct sockaddr *)&inAddr,sizeof(struct sockaddr))<0){
    printf("Connect failed.\n");
    return 1;
}
  printf("Connection accepted\n");
  char *FileName; // file input
  char *Result; // file return
  int choice; 
  do{
    printf("\n*************ENCODE_DECODE_BASE64**************");
    printf("\n*******             1. Encode          ********");
    printf("\n*******             2. Decode          ********");
    printf("\n*******             3. Exit            ********");
    printf("\n***********************************************\n");
    printf("Choice: ");
  choice = getchar(); 
  while(getchar() !='\n');
  switch(choice)
    {
    case '1':
//demo encode////////////////////////////////////////
  send(sockfd,"encode",5,0); // send to server when choose 1
  printf("File Encode : ");
  FileName = inputString(20); 
  FILE *fpt = fopen(FileName,"r"); 
  if(fpt==NULL){
    printf("File not found");
    return -1;
  }
  printf("File Result: ");
  Result = inputString(20);
  FILE *ft = fopen(Result,"w");
  while(!feof(fpt)){
    if (fgets(buff,MAX,fpt) != NULL ){
      sentBytes=send(sockfd,buff,1024,0); 

      revedBytes=recv(sockfd,buff,1024,0);

      fprintf(ft,"%s\n",buff);
    }
  }
  printf("Encode done!thanks you !\n");
  //close(sockfd);
  fclose(fpt);fclose(ft);
  return 0;
  break;

//decode ///////////////////////////////////////////////   
  case '2':
  send(sockfd,"decode",6,0);
  printf("File Decode : ");
  FileName = inputString(20);
  FILE *fpt1 = fopen(FileName,"r");
  if(fpt1==NULL){
    printf("File not found");
    return -1;
  }
  printf("File Result : ");
  Result = inputString(20);
  FILE *ft1 = fopen(Result,"w");

  while(!feof(fpt1)){
    if (fgets(buff,MAX,fpt1) != NULL ){
      sentBytes=send(sockfd,buff,1024,0);

      revedBytes=recv(sockfd,buff,1024,0);

      fprintf(ft1,"%s",buff);
    }
  }
  printf("Decode done ! thanks you !\n");
  //close(sockfd);
  fclose(fpt1);fclose(ft1);
  return 0;
  break;
///////////////////////////////////////////////////////
    case '3':
      printf("Thanks!\n");
      break;
    default: printf("wrong number, please try again!\n"); break;
      //end choice///////////////////////////////////////
    }
    }while(choice!='3');
      //end menu
      //close(sockfd);
      //return 0;
}

这是ServerCode:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


/* decodeblock - decode 4 '6-bit' characters into 3 8-bit binary bytes */
void decodeblock(unsigned char in[], char *clrstr) {
  unsigned char out[4];
  out[0] = in[0] << 2 | in[1] >> 4;
  out[1] = in[1] << 4 | in[2] >> 2;
  out[2] = in[2] << 6 | in[3] >> 0;
  out[3] = '\0';
  strncat(clrstr, out, sizeof(out));
}

void b64_decode(char *b64src, char *clrdst) {
  int c, phase, i;
  unsigned char in[4];
  char *p;

  clrdst[0] = '\0';
  phase = 0; i=0;
  while(b64src[i]) {
    c = (int) b64src[i];
    if(c == '=') {
      decodeblock(in, clrdst); 
      break;
    }
    p = strchr(b64, c);
    if(p) {
      in[phase] = p - b64;
      phase = (phase + 1) % 4;
      if(phase == 0) {
        decodeblock(in, clrdst);
        in[0]=in[1]=in[2]=in[3]=0;
      }
    }
    i++;
  }
}

/* encodeblock - encode 3 8-bit binary bytes as 4 '6-bit' characters */
void encodeblock( unsigned char in[], char b64str[], int len ) {
    unsigned char out[5];
    out[0] = b64[ in[0] >> 2 ];
    out[1] = b64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
    out[2] = (unsigned char) (len > 1 ? b64[ ((in[1] & 0x0f) << 2) |
             ((in[2] & 0xc0) >> 6) ] : '=');
    out[3] = (unsigned char) (len > 2 ? b64[ in[2] & 0x3f ] : '=');
    out[4] = '\0';
    strncat(b64str, out, sizeof(out));
}

/* encode - base64 encode a stream, adding padding if needed */
void b64_encode(char *clrstr, char *b64dst) {
  unsigned char in[3];
  int i, len = 0;
  int j = 0;

  b64dst[0] = '\0';
  while(clrstr[j]) {
    len = 0;
    for(i=0; i<3; i++) {
     in[i] = (unsigned char) clrstr[j];
     if(clrstr[j]) {
        len++; j++;
      }
      else in[i] = 0;
    }
    if( len ) {
      encodeblock( in, b64dst, len );
    }
  }
}
void sig_chld(int signo) //child
{
pid_t pid;
int stat;
while((pid = waitpid(-1, &stat, WNOHANG))>0)
printf("child %d terminated\n", pid);
return;
}
int main()
{
int listen_sock, conn_sock; 
int server_len, client_len,choice;
struct sockaddr_in server_address; 
struct sockaddr_in client_address;
char myb64[1024] = ""; //encode
char mydst[1024] = ""; //decode
int sentBytes,revedBytes,bytes_readi; 
char buff[1024]; //buffer to send data

listen_sock = socket(AF_INET, SOCK_STREAM, 0); //create socket
if (listen_sock == -1)
 {
    printf("ERROR opening socket\n");
    return 0;
 }
 printf("Socket done\n");
//Thiet lap dia chi server
server_address.sin_family = AF_INET;  //default       
inet_aton("127.0.0.1",&server_address.sin_addr); //ip server
server_address.sin_port = htons(5500); // port server
server_len = sizeof(server_address); 

if(bind(listen_sock, (struct sockaddr *)&server_address,server_len)<0)
{
  printf("ERROR on binding\n");
    return 0;
}
printf("Bind done\n");
int check = listen(listen_sock,10);
if (check == -1)
 {
 printf("Error connect");
 return 0;
 }
printf("Waiting connect..\n");


while(1) {
client_len = sizeof(client_address); 
conn_sock = accept(listen_sock,(struct sockaddr *)&client_address, &client_len);
if(conn_sock==-1){
  printf("Error connect\n");
  return 1;
}else{
  printf("Accept new connection\n");
}
if(fork() == 0){
  close(listen_sock);
  do{
          revedBytes = recv(conn_sock,buff,1024,0); 
          buff[revedBytes]='\0';
          if(strcmp(buff,"mahoa")==0) choice=1;  
          else if(strcmp(buff,"giaima")==0) choice=2; else choice = 3;
          switch(choice)
      {
      case 1:
      while((revedBytes = recv(conn_sock,buff,1024,0)) > 0){ 
               buff[revedBytes]='\0';
            //printf("string send by client encode : %s\n",buff);
            b64_encode(buff, myb64); //ma hoa
            sentBytes=send(conn_sock,myb64,1024,0); //gui lai string da ma hoa cho client
          }
      close(conn_sock);//Dong ket noi cua client
      exit(0);
      break;
      case 2:
      while((revedBytes = recv(conn_sock,buff,1024,0)) > 0){ 
               buff[revedBytes]='\0';
            //printf("string send by client decode: %s\n",buff);
            b64_decode(buff,mydst); // giaima
            sentBytes=send(conn_sock,mydst,1024,0); 
          }
      close(conn_sock);
      exit(0); 
      break;
      case 3:break;
          }
        }while(choice!=3);
        break;
}
signal(SIGCHLD,sig_chld);
close(conn_sock);
}
return 1;
}

0 个答案:

没有答案