70,023
社区成员




#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <fcntl.h>
int main(void) {
struct sockaddr_in sin;
struct sockaddr_in cin;
int lfd;
int afd;
FILE * fp;
socklen_t len;
char buf[400];
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(8000);
sin.sin_addr.s_addr = INADDR_ANY;
lfd = socket(AF_INET,SOCK_STREAM,0);
if(lfd < 0) {
perror("socket");
exit(1);
}
if(bind(lfd,(struct sockaddr *)&sin,sizeof(sin)) == -1) {
perror("bind");
exit(1);
}
listen(lfd,10);
while(1) {
bzero(buf,400);
afd = accept(lfd,(struct sockaddr *)&cin,&len);
if(afd < 0) {
perror("afd");
exit(1);
}
printf("错在哪?\n");
fp = fdopen(afd,"a+");
fgets(buf,400,fp);
printf("%s\n",buf);
fclose(fp);
close(afd);
}
return EXIT_SUCCESS;
}
fp = fdopen(afd,"a+");
fgets(buf,400,fp);
printf("%s\n",buf);
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <fcntl.h>
int main(void) {
struct sockaddr_in sin;
struct sockaddr_in cin;
int lfd;
int afd;
FILE * fp;
FILE * fp1;
socklen_t len;
char buf[400];
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(8000);
sin.sin_addr.s_addr = INADDR_ANY;
lfd = socket(AF_INET,SOCK_STREAM,0);
if(lfd < 0) {
perror("socket");
exit(1);
}
if(bind(lfd,(struct sockaddr *)&sin,sizeof(sin)) == -1) {
perror("bind");
exit(1);
}
listen(lfd,10);
while(1) {
bzero(buf,400);
afd = accept(lfd,(struct sockaddr *)&cin,&len);
if(afd < 0) {
perror("afd");
exit(1);
}
fp = fdopen(afd,"r");
fp1 = fdopen(afd,"w");
while(fgets(buf,400,fp)) {
if(strlen(buf) == 2)
break;
if(strstr(buf,"POST")) {
printf("post提交。\n");
}
printf("%s",buf);
}
fputs("HTTP/1.1 200\r\n",fp1);
fflush(fp1);
fclose(fp1);
fclose(fp);
close(afd);
}
return EXIT_SUCCESS;
}
:
fp = fdopen(afd,"r");
fp1 = fdopen(afd,"w");
fgets(buf,400,fp);
printf("%s\n",buf);
fclose(fp);
close(afd);