-1

在以下程序中,我通过服务器进行了读写。但是当执行 write() 时,它会打印出多余的字符

// this a server program
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>             //  for ssize_t data type  
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <errno.h>
#include <wait.h>

#define LISTENQ        (24)   /*  Backlog for listen()   */
#define MAXBUFFSIZE 12

// Read in characters. Increase memory if we run out of space.
char *stdin_line(int conn)
{
  int rr=0, size=0, cap = MAXBUFFSIZE;
  char *str_ptr;//, oc;
  char buff[MAXBUFFSIZE];

   // allocate some memory for the pointer first
   str_ptr = (char *) malloc(cap * sizeof(char));
   assert(str_ptr != NULL);
   // start reading from socket
   rr=read(conn,buff,MAXBUFFSIZE)) > 0 )
       if(rr > 0){
    str_ptr = buff;
    size = MAXBUFFSIZE;
   }
   if( rr == -1) printf("Error (stdin_line): %s/n",strerror(errno));

  return str_ptr;
}


void daemonize(void){
   pid_t pid, sid=0;
   // fork off process
   pid = fork();
   // failure
   if( pid < 0 ){
    printf("Error (fork<0): %s\n",strerror(errno));
    exit(1);
   }
   // exit parent process
   if( pid > 0 ){
    printf("Error (fork>0): %s\n",strerror(errno));
    exit(0);
   }
   // change file mode mask
   umask(0);

// open log file for daemon debug information. OR as per requirements of hw4

   // create new Session ID for child process
   setsid();
   if( sid < 0 ){
    printf("Error (setsid<0): %s\n",strerror(errno));
    exit(1);
   }
   // exit parent process
   if( sid > 0 ){
    printf("Error (setsid>0): %s\n",strerror(errno));
    exit(0);
   }

   // change working directory to safe directory (one which is always there)
   if( chdir("/") < 0 ){
    printf("Error (chdir): %s\n",strerror(errno));
    exit(1);
   }

}


int main(void) {

   int lstn_sock, conn_sock;
   struct sockaddr_in my_serv;
   short int pnum = 4080;
   ssize_t wnum;
   char *rstr=NULL;

   // create listening socket
   if( (lstn_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Error (socket): %s\n",strerror(errno));
    exit(1);
   }

   // initialize socket address
   memset( &my_serv, 0, sizeof(my_serv) );
   my_serv.sin_family = AF_INET;
   my_serv.sin_addr.s_addr = INADDR_ANY;
   my_serv.sin_port = htons(pnum);

   // associate address with socket. 
   if( bind(lstn_sock, (struct sockaddr *) &my_serv, sizeof(my_serv)) < 0){
    printf("Error (bind): %s\n",strerror(errno));
    exit(1);
   }
   // start listening to socket
   if( listen(lstn_sock, LISTENQ) < 0){
    printf("Error (listen): %s\n",strerror(errno));
    exit(1);
   }
   // make it a daemon
   daemonize();

   // work in the background
   while(1){
    // retrieve connect request and connect
    if( (conn_sock = accept(lstn_sock, NULL, NULL)) < 0){
       printf("Error (accept): %s\n",strerror(errno));
       exit(1);
    }

    rstr = stdin_line(conn_sock);

    if( (wnum=write(conn_sock,rstr,strlen(rstr))) <= 0){
       printf("Error (write: rstr): %s\n",strerror(errno));
       exit(1);
    }

    // close connected socket
    if( close(conn_sock) < 0){
       printf("Error (close): %s\n",strerror(errno));
       exit(1);
    }
   }


return 0;

}

@ubuntu:$ ./my_code
@ubuntu:$ telnet localhost 4080

输出:
正在尝试 ::1...
正在尝试 127.0.0.1..
已连接到 localhost。
转义字符是 '^]'。
j
j
dhk
9
连接被外部主机关闭。

第一个“j”由用户(我)输入,第二个“j”由服务器回显。为什么我得到“dhk”和“9”字符?

4

1 回答 1

1

因为您忽略了您收到的实际数据量,也就是说rr,而是打印整个字符串。

rr=read(conn,buff,MAXBUFFSIZE)) > 0 )
    if(rr > 0){
 str_ptr = buff;
 size = MAXBUFFSIZE;
}

您也不应该这样做,str_ptr = buff;因为这只是复制指针而不是字符串本身。是在堆栈上分配的buff,因此当函数返回时它可能不再有效。

于 2012-04-23T01:05:24.723 回答