简体   繁体   English

为什么我的程序输出为“ direct://”?

[英]Why am i getting “direct://” as output for my program?

I am in the early stages of writing a proxy server in c for class and while debugging, my program gives me a weird output simpley with two lines of 我处于用c为类编写代理服务器的早期阶段,并且在调试时,我的程序通过两行代码为我提供了一个奇怪的输出简单方法

direct:// 直接://

direct:// 直接://

what does this mean? 这是什么意思? I've never had this happen before. 我以前从未发生过这种情况。 The program even outputs this when i dont provide 3 arguments which i required for this program to execute. 当我不提供执行该程序所需的3个参数时,程序甚至输出此命令。

#include <pthread.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <string.h>

int main (int argc, char *argv[]){

   if(argc!=3){
      printf("Usage: proxy <IP address> <port no.>");
      exit(1);
   }

   int csock, ssock, clen, slen;
   int csocka, ssocka;
   int rc, fd, ttl;
   char method[40];
   char uri[80];
   char prot[40];
   char cbuf[100];
   time_t logtime;
   char * pch;

   struct sockaddr_in caddr;
   struct sockaddr_in caddr2;
   struct sockaddr_in saddr;
   struct sockaddr_in saddr2;

   csock = socket(AF_INET, SOCK_STREAM, 0);
   caddr.sin_family = AF_INET;
   caddr.sin_addr.s_addr = inet_addr(argv[1]);
   caddr.sin_port = htons(atoi(argv[2]));
   clen = sizeof(caddr);
   rc = bind(csock, (struct sockaddr *) &caddr, clen);
   if(rc < 0){
      printf("bind failed");
      exit(1);
   }
   rc = listen(csock, 5);
   if(rc < 0){
      printf("listen failed");
      exit(1);
   }
   printf("hey");
   csocka = accept(csock, (struct sockaddr *) &caddr2, &clen);
   if(csocka < 0){
      printf("accept failed");
      exit(1);
   }

   while(1){
      read(csocka,&cbuf,sizeof(cbuf));
      time(&logtime);                                   //time of req.
      if(cbuf==NULL){
         cerror("400 Bad Request: empty request");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      ttl = strlen(cbuf);
      while(cbuf[ttl-1] == '\n' || cbuf[ttl-1] == '\r'){
         cbuf[ttl--] = '\0';
      }
      if(sscanf(cbuf,"%[^ ] %[^ ] %[^ ]", method, uri, prot) != 3){
         cerror("400 Bad Request: Unexpected number of arguments");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      if(method!="GET" || method !="HEAD"){
         cerror("405 Method Not Allowed: GET/HEAD only");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      if(uri == (char*) 0){
         cerror("400 Bad Request: empty url");
         write(csocka, &errbuf, sizeof(errbuf));
         continue;
      }
      printf("%s \n", cbuf);
   }

   close(csocka);

}

The most probable reason is that you aren't running your program, but some system program. 最可能的原因是您不是在运行程序,而是在运行某些系统程序。

If you are on a Linux machine, type: 如果您使用的是Linux计算机,请输入:

which <program name>

to find out which executable you are actually running. 找出您实际正在运行的可执行文件。

type: 类型:

./<program name>

to run your program instead (provided that you are in the same directory as your executable). 而是运行程序(前提是您与可执行文件位于同一目录中)。

Why are you ignoring the return value of read ? 为什么您忽略read的返回值? What makes you think read is null-terminating cbuf for you? 是什么让您认为read对您而言终止cbuf为空? If read isn't null terminating cbuf , then what makes you think it's safe to pass buf to strlen ? 如果read不是终止cbuf ,那么您为什么认为将buf传递给strlen是安全的呢? You're invoking undefined behaviour by passing something that isn't a string to strlen... The behaviour that follows may seem strange or inconsistent, but that's the nature of undefined behaviour. 您正在通过传递不是字符串的东西来调用未定义的行为,因此可能会感到奇怪或不一致,但这是未定义行为的本质。

int len = read(csocka,&cbuf,sizeof(cbuf));
if (len <= 0) {
    /* something went wrong in read().
     * report an error and stop here... */
    break;
}
/* Once error checking is performed, len is the number of bytes recieved by read. */

Consider the code above. 考虑上面的代码。 Do you need the line ttl=strlen(cbuf); 您是否需要行ttl=strlen(cbuf); , if you correctly check for errors? ,如果您正确检查错误?

printf("%s \\n", cbuf); is wrong, because cbuf isn't a string. 是错误的,因为cbuf不是字符串。 Consider fwrite(cbuf, len, stdout); putchar('\\n'); 考虑fwrite(cbuf, len, stdout); putchar('\\n'); fwrite(cbuf, len, stdout); putchar('\\n'); or printf("%.*s\\n", len, stdout); printf("%.*s\\n", len, stdout); .

write(csocka, &errbuf, sizeof(errbuf)); also looks wrong, but I'll leave that in your hands. 也看起来不对,但我会留在您手中。 If you need us to fix these kinds of errors for you, then your method of learning isn't working very well. 如果您需要我们为您解决此类错误,那么您的学习方法就无法很好地发挥作用。 Which book are you reading? 您正在读哪本书?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM