LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 806|回复: 6

my web server problem

[复制链接]
发表于 2006-2-22 19:51:15 | 显示全部楼层 |阅读模式
then I am working in linux,and I have not config my Chinese input,
and I must encode a web server use C,
and then ,I have a problem ,that is :
I listen the 80 port, and recv someting,
after that ,input http://myname via Firefox
it tell me the "connect is deny" ,so ,I do not know how I can do.

underneath is my code:

  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>

  6. #define MYPORT 80    // the port users will be connecting to

  7. #define BACKLOG 10     // how many pending connections queue will hold

  8. int main()
  9. {
  10.         int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
  11.         struct sockaddr_in my_addr;    // my address information
  12.         struct sockaddr_in their_addr; // connector's address information
  13.         int sin_size;

  14.         sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!

  15.         my_addr.sin_family = AF_INET;         // host byte order
  16.         my_addr.sin_port = htons(MYPORT);     // short, network byte order
  17.         my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
  18.         memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

  19.         // don't forget your error checking for these calls:
  20.         bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));

  21.         listen(sockfd, 5);

  22.         sin_size = sizeof(struct sockaddr_in);
  23.         new_fd = accept(sockfd, (struct sockaddr *)&their_addr, (size_t*)&sin_size);
  24.         char strTemp[1000];
  25.         memset(strTemp,0,1000);
  26.         recv(new_fd,strTemp,1000,0);
  27.         printf("%s\n",strTemp);
  28.         send(new_fd,strTemp,sizeof(strTemp),0);
  29.         return 0;
  30. }

复制代码

please check it for me,thank you very much!
发表于 2006-2-22 23:10:37 | 显示全部楼层
just consult the rfc about http
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-23 11:19:14 | 显示全部楼层
ok ,thank you very much
but where can I get the rfc?
回复 支持 反对

使用道具 举报

发表于 2006-2-23 11:47:37 | 显示全部楼层
权限问题???? 对这些一点没有经验
帮你顶一下
我也关注这个帖子的恢复
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-23 19:04:26 | 显示全部楼层
I have looked at the http's rfc,but I think it is no use for me,How can I do next?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-24 09:02:24 | 显示全部楼层

  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <unistd.h>

  7. #define MYPORT 80   
  8. #define BACKLOG 10   

  9. int main()
  10. {
  11.         int sockfd, new_fd;  
  12.         struct sockaddr_in my_addr;
  13.         struct sockaddr_in their_addr;
  14.         int sin_size;

  15.         sockfd = socket(PF_INET, SOCK_STREAM, 0);

  16.         my_addr.sin_family = AF_INET;        
  17.         my_addr.sin_port = htons(MYPORT);   
  18.         my_addr.sin_addr.s_addr = INADDR_ANY;
  19.         memset(&(my_addr.sin_zero), '\0', 8);

  20.         bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));

  21.         listen(sockfd, 5);

  22.         sin_size = sizeof(struct sockaddr_in);
  23.         new_fd = accept(sockfd, (struct sockaddr *)&their_addr, (size_t*)&sin_size);
  24.         char strTemp[1000];
  25.         while(true)
  26.         {
  27.                 memset(strTemp,0,1000);
  28.                 if(recv(new_fd,strTemp,1000,0)<=0)
  29.                 {
  30.                         printf("close socket");//为什么不能运行到这里???
  31.                         close(new_fd);
  32.                         return 0;
  33.                 }
  34.                 printf("%s\n",strTemp);
  35.         }       
  36.         close(new_fd);
  37.         return 0;
  38. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-2-24 09:03:14 | 显示全部楼层
上边是我的最新的代码,能够得到所有浏览器发过来的信息了,但是我总是认为是非正常退出,大家有空帮我看看好吗?
谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表