当前位置: 技术问答>linux和unix
linux shocket问题
来源: 互联网 发布时间:2016-09-09
本文导语: 以下程序用客户端运行不返回结果到客户端,研究了好久都不行,请各位大虾帮帮忙 #include #include #include #include #include #include #include #include #include #include #include #include #define MAXLINE 300 ...
以下程序用客户端运行不返回结果到客户端,研究了好久都不行,请各位大虾帮帮忙
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 300
#define SERVPORT 80
#define BACKLOG 10
extern char * environ;
void sigchld_handler(int sig);
int parse_uri(char *uri, char *filename, char *cgiargs);
void server_dynamic(int fd,char *filename,char *cgiargs);
void server_static(int fd,char *filename,int filesize);
void doit(int fd);
main()
{
int sockfd,client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
struct hostent * entity;
pid_t pid;
char * recvBuffer = (char*) malloc(4001);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket error"); exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero),8);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
== -1) {
perror("bind error");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen error");
exit(1);
}
printf("Accepting connections...n");
while(1)
{
int sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size)) == -1)
{
perror("accept error");
exit(1);
}
// if((pid = fork()) == -1)
// perror("fork error");
// if(pid == 0)
// {
//close(sockfd);
entity = gethostbyaddr((char*) &remote_addr.sin_addr,sizeof(struct in_addr),AF_INET);
printf("Connection from %sn",inet_ntoa((struct in_addr) remote_addr.sin_addr));
doit(client_fd);
//close(client_fd);
// }
close(client_fd);
}
}
void sigchld_handler(int sig)
{
while (waitpid(-1,0,WNOHANG));
return;
}
void doit(int fd)
{
int is_static;
struct stat sbuf;
char buf[MAXLINE], method[MAXLINE],uri[MAXLINE],version[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
if(read(fd, buf, 200) ==-1)
perror("doit read error");
sscanf(buf,"%s %s %s", method, uri,version);
printf("method:%s uri:%s version:%s n",method,uri,version);
if(strcasecmp(method,"GET"))
{
//clienterror(fd,method,"501","Not Implemented", "Tiny does not implement this method");
return;
}
//read_requesthdrs();
is_static = parse_uri(uri,filename,cgiargs);
printf("filename:%sn",filename);
printf("uri:%sn",uri);
printf("cgiargs:%sn",cgiargs);
if(stat(filename,&sbuf)index.html
[root@VPS test]# ./t
Accepting connections...
Connection from 192.168.0.2
method:GET uri:/index.html version:HTTP/1.1
/index.html
filename:./index.html
uri:/index.html
cgiargs:
HTTP/1.0 200 OK
Server:Tiny Web Server
Content-Length: 6
Content-Type: text/html
hello
Connection from 192.168.0.2
method:GET uri:/favicon.ico version:HTTP/1.1
/favicon.ico
filename:./favicon.ico
uri:/favicon.ico
cgiargs:
客户端请求加上/index.html
显示正常
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 300
#define SERVPORT 80
#define BACKLOG 10
extern char * environ;
void sigchld_handler(int sig);
int parse_uri(char *uri, char *filename, char *cgiargs);
void server_dynamic(int fd,char *filename,char *cgiargs);
void server_static(int fd,char *filename,int filesize);
void doit(int fd);
main()
{
int sockfd,client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
struct hostent * entity;
pid_t pid;
char * recvBuffer = (char*) malloc(4001);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket error"); exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero),8);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
== -1) {
perror("bind error");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen error");
exit(1);
}
printf("Accepting connections...n");
while(1)
{
int sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size)) == -1)
{
perror("accept error");
exit(1);
}
// if((pid = fork()) == -1)
// perror("fork error");
// if(pid == 0)
// {
//close(sockfd);
entity = gethostbyaddr((char*) &remote_addr.sin_addr,sizeof(struct in_addr),AF_INET);
printf("Connection from %sn",inet_ntoa((struct in_addr) remote_addr.sin_addr));
doit(client_fd);
//close(client_fd);
// }
close(client_fd);
}
}
void sigchld_handler(int sig)
{
while (waitpid(-1,0,WNOHANG));
return;
}
void doit(int fd)
{
int is_static;
struct stat sbuf;
char buf[MAXLINE], method[MAXLINE],uri[MAXLINE],version[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
if(read(fd, buf, 200) ==-1)
perror("doit read error");
sscanf(buf,"%s %s %s", method, uri,version);
printf("method:%s uri:%s version:%s n",method,uri,version);
if(strcasecmp(method,"GET"))
{
//clienterror(fd,method,"501","Not Implemented", "Tiny does not implement this method");
return;
}
//read_requesthdrs();
is_static = parse_uri(uri,filename,cgiargs);
printf("filename:%sn",filename);
printf("uri:%sn",uri);
printf("cgiargs:%sn",cgiargs);
if(stat(filename,&sbuf)index.html
[root@VPS test]# ./t
Accepting connections...
Connection from 192.168.0.2
method:GET uri:/index.html version:HTTP/1.1
/index.html
filename:./index.html
uri:/index.html
cgiargs:
HTTP/1.0 200 OK
Server:Tiny Web Server
Content-Length: 6
Content-Type: text/html
hello
Connection from 192.168.0.2
method:GET uri:/favicon.ico version:HTTP/1.1
/favicon.ico
filename:./favicon.ico
uri:/favicon.ico
cgiargs:
客户端请求加上/index.html
显示正常
|
什么意思?跟你这里的socket有关系么?