root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# vim wrap.h
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# vim wrap.h
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# gcc web.c wrap.c -o web
web.c:9:20: fatal error: singnal.h: 没有那个文件或目录
#include<singnal.h>
^
compilation terminated.
wrap.c: In function ‘Accept’:
wrap.c:19:16: error: ‘ECONNABORTTED’ undeclared (first use in this function)
if((errno == ECONNABORTTED)||(errno == EINTR))
^
wrap.c:19:16: note: each undeclared identifier is reported only once for each function it appears in
wrap.c: In function ‘Read’:
wrap.c:70:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c: In function ‘my_read’:
wrap.c:176:3: error: ‘ptr’ undeclared (first use in this function)
*ptr = *read_ptr++;
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# gcc web.c wrap.c -o web
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘http_send’:
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat=]
len = sprintf(HTTP_INFO,"%S%S",HTTP_HEADER,content);
^
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’ [-Wformat=]
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c:87:6: note: each undeclared identifier is reported only once for each function it appears in
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c wrap.c -o -webone
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘http_send’:
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat=]
len = sprintf(HTTP_INFO,"%S%S",HTTP_HEADER,content);
^
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’ [-Wformat=]
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c:87:6: note: each undeclared identifier is reported only once for each function it appears in
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘http_send’:
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat=]
len = sprintf(HTTP_INFO,"%S%S",HTTP_HEADER,content);
^
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’ [-Wformat=]
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:12:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
root@wangmiao:~/lesson# vim wrap.h
root@wangmiao:~/lesson# gcc web.c -o webtwo
/tmp/ccSp36wf.o:在函数‘http_send’中:
web.c:(.text+0xc6):对‘Write’未定义的引用
/tmp/ccSp36wf.o:在函数‘main’中:
web.c:(.text+0x14f):对‘Socket’未定义的引用
web.c:(.text+0x1ee):对‘Bind’未定义的引用
web.c:(.text+0x202):对‘Listen’未定义的引用
web.c:(.text+0x235):对‘Accept’未定义的引用
web.c:(.text+0x255):对‘peeror’未定义的引用
web.c:(.text+0x278):对‘Close’未定义的引用
web.c:(.text+0x297):对‘Read’未定义的引用
web.c:(.text+0x357):对‘Close’未定义的引用
web.c:(.text+0x36f):对‘Close’未定义的引用
collect2: error: ld returned 1 exit status
root@wangmiao:~/lesson# gcc web.c wrap.c -o web
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c:87:6: note: each undeclared identifier is reported only once for each function it appears in
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# gcc web.c wrap.c -o web
/tmp/cc4QQlah.o:在函数‘main’中:
web.c:(.text+0x255):对‘peeror’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Accept’中:
wrap.c:(.text+0x65):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Bind’中:
wrap.c:(.text+0x99):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Connect’中:
wrap.c:(.text+0xca):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Listen’中:
wrap.c:(.text+0xf4):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Socket’中:
wrap.c:(.text+0x12a):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:wrap.c:(.text+0x3c1): 跟着更多未定义的参考到 perr_exit
collect2: error: ld returned 1 exit status
root@wangmiao:~/lesson#
web.c:9:20: fatal error: singnal.h: 没有那个文件或目录
#include<singnal.h>
^
compilation terminated.
wrap.c: In function ‘Accept’:
wrap.c:19:16: error: ‘ECONNABORTTED’ undeclared (first use in this function)
if((errno == ECONNABORTTED)||(errno == EINTR))
^
wrap.c:19:16: note: each undeclared identifier is reported only once for each function it appears in
wrap.c: In function ‘Read’:
wrap.c:70:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c: In function ‘my_read’:
wrap.c:176:3: error: ‘ptr’ undeclared (first use in this function)
*ptr = *read_ptr++;
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# gcc web.c wrap.c -o web
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘http_send’:
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat=]
len = sprintf(HTTP_INFO,"%S%S",HTTP_HEADER,content);
^
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’ [-Wformat=]
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c:87:6: note: each undeclared identifier is reported only once for each function it appears in
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c wrap.c -o -webone
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘http_send’:
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat=]
len = sprintf(HTTP_INFO,"%S%S",HTTP_HEADER,content);
^
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’ [-Wformat=]
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c:87:6: note: each undeclared identifier is reported only once for each function it appears in
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘http_send’:
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 3 has type ‘char *’ [-Wformat=]
len = sprintf(HTTP_INFO,"%S%S",HTTP_HEADER,content);
^
web.c:34:2: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’ [-Wformat=]
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
web.c: In function ‘main’:
web.c:54:2: error: ‘listendfd’ undeclared (first use in this function)
listendfd = Socket(AF_INET,SOCK_STREAM,0);
^
web.c:54:2: note: each undeclared identifier is reported only once for each function it appears in
web.c:55:25: error: ‘seraddr’ undeclared (first use in this function)
bzero(&servaddr,sizeof(seraddr));
^
web.c:61:22: error: ‘SOL_SOKET’ undeclared (first use in this function)
setsockopt(listenfd,SOL_SOKET,SO_REUSEADDR,&opt,sizeof(opt));
^
web.c:87:103: error: ‘cli_addr’ undeclared (first use in this function)
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cli_addr.sin_port));
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:11:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
root@wangmiao:~/lesson# vim web.c
root@wangmiao:~/lesson# gcc web.c -o webtwo
In file included from web.c:12:0:
wrap.h:6:44: error: unknown type name ‘socklent_t’
void Bind(int fd,const struct sockaddr *sa,socklent_t salen);
^
root@wangmiao:~/lesson# vim wrap.h
root@wangmiao:~/lesson# gcc web.c -o webtwo
/tmp/ccSp36wf.o:在函数‘http_send’中:
web.c:(.text+0xc6):对‘Write’未定义的引用
/tmp/ccSp36wf.o:在函数‘main’中:
web.c:(.text+0x14f):对‘Socket’未定义的引用
web.c:(.text+0x1ee):对‘Bind’未定义的引用
web.c:(.text+0x202):对‘Listen’未定义的引用
web.c:(.text+0x235):对‘Accept’未定义的引用
web.c:(.text+0x255):对‘peeror’未定义的引用
web.c:(.text+0x278):对‘Close’未定义的引用
web.c:(.text+0x297):对‘Read’未定义的引用
web.c:(.text+0x357):对‘Close’未定义的引用
web.c:(.text+0x36f):对‘Close’未定义的引用
collect2: error: ld returned 1 exit status
root@wangmiao:~/lesson# gcc web.c wrap.c -o web
wrap.c: In function ‘Write’:
wrap.c:87:6: error: ‘error’ undeclared (first use in this function)
if(error == EINTR)
^
wrap.c:87:6: note: each undeclared identifier is reported only once for each function it appears in
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# vim wrap.c
root@wangmiao:~/lesson# gcc web.c wrap.c -o web
/tmp/cc4QQlah.o:在函数‘main’中:
web.c:(.text+0x255):对‘peeror’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Accept’中:
wrap.c:(.text+0x65):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Bind’中:
wrap.c:(.text+0x99):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Connect’中:
wrap.c:(.text+0xca):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Listen’中:
wrap.c:(.text+0xf4):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:在函数‘Socket’中:
wrap.c:(.text+0x12a):对‘perr_exit’未定义的引用
/tmp/ccqcgrcT.o:wrap.c:(.text+0x3c1): 跟着更多未定义的参考到 perr_exit
collect2: error: ld returned 1 exit status
root@wangmiao:~/lesson#
原本是想把文件拷贝到目录下的,
可是发现root的路径压根打不开,气的我吐血呀!
后来在网上搜索到了这个命令,sudo nautilus,
试了之后发现还是没有用
root@wangmiao:~/lesson# sudo nautilus
xnxy123
sudo:无法解析主机:wangmiao
No protocol specified
可是发现root的路径压根打不开,气的我吐血呀!
后来在网上搜索到了这个命令,sudo nautilus,
试了之后发现还是没有用
root@wangmiao:~/lesson# sudo nautilus
xnxy123
sudo:无法解析主机:wangmiao
No protocol specified
** (nautilus:3397): WARNING **: Could not open X display
No protocol specified
error: XDG_RUNTIME_DIR not set in the environment.
No protocol specified
error: XDG_RUNTIME_DIR not set in the environment.
(nautilus:3397): Gtk-WARNING **: cannot open display: :0
root@wangmiao:~/lesson# xnxy123
xnxy123:未找到命令
root@wangmiao:~/lesson# sudo nautilus
xnxy123
sudo:无法解析主机:wangmiao
No protocol specified
root@wangmiao:~/lesson# xnxy123
xnxy123:未找到命令
root@wangmiao:~/lesson# sudo nautilus
xnxy123
sudo:无法解析主机:wangmiao
No protocol specified
** (nautilus:3409): WARNING **: Could not open X display
No protocol specified
error: XDG_RUNTIME_DIR not set in the environment.
No protocol specified
error: XDG_RUNTIME_DIR not set in the environment.
(nautilus:3409): Gtk-WARNING **: cannot open display: :0
root@wangmiao:~/lesson# xnxy123
xnxy123:未找到命令
root@wangmiao:~/lesson#
root@wangmiao:~/lesson# xnxy123
xnxy123:未找到命令
root@wangmiao:~/lesson#
我又进行了一波新的操作,试试!
Linux下 在root 用户下的文件夹如何复制到 另一个用户下 ?
首先
su root
然后
cp /root/文件夹 /home/用户名/文件夹
最后
chown -R 用户名:用户组 /home/用户名/文件夹
exit
Linux下 在root 用户下的文件夹如何复制到 另一个用户下 ?
首先
su root
然后
cp /root/文件夹 /home/用户名/文件夹
最后
chown -R 用户名:用户组 /home/用户名/文件夹
exit
**************************以下是web.c代码**********************************
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<ctype.h>
#include <signal.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<ctype.h>
#include <signal.h>
#include<sys/wait.h>
#include "wrap.h"
#define BUFF_SIZE 1024
#define SERV_PORT 8000
#define SERV_PORT 8000
void handle_sig(int sig)
{
printf("child exit\n");
wait(NULL);
}
{
printf("child exit\n");
wait(NULL);
}
char *http_res_tmpl = "HTTP/1.1 200 OK\r\n"
"Server:Cleey's Server v1.0\r\n"
"Accept-Ranges:bytes\r\n"
"Content_Length:%d\r\n"
"Connection:close\r\n"
"Content-Type:%s\r\n\r\n";
"Server:Cleey's Server v1.0\r\n"
"Accept-Ranges:bytes\r\n"
"Content_Length:%d\r\n"
"Connection:close\r\n"
"Content-Type:%s\r\n\r\n";
void http_send(int sock_client,char *content)
{
char HTTP_HEADER[BUFF_SIZE],HTTP_INFO[BUFF_SIZE];
int len= strlen(content);
sprintf(HTTP_HEADER,http_res_tmpl,len,"text/html");
len = sprintf(HTTP_INFO,"%s%s",HTTP_HEADER,content);
{
char HTTP_HEADER[BUFF_SIZE],HTTP_INFO[BUFF_SIZE];
int len= strlen(content);
sprintf(HTTP_HEADER,http_res_tmpl,len,"text/html");
len = sprintf(HTTP_INFO,"%s%s",HTTP_HEADER,content);
Write(sock_client,HTTP_INFO,len);
}
}
int main(void)
{
struct sockaddr_in servaddr,cliaddr;
socklen_t cliaddr_len;
int listenfd,connfd;
char buf[BUFF_SIZE];
char str[INET_ADDRSTRLEN];
int i,n;
{
struct sockaddr_in servaddr,cliaddr;
socklen_t cliaddr_len;
int listenfd,connfd;
char buf[BUFF_SIZE];
char str[INET_ADDRSTRLEN];
int i,n;
struct sigaction act;
act.sa_handler = handle_sig;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGCHLD,&act,NULL);
act.sa_handler = handle_sig;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGCHLD,&act,NULL);
listenfd = Socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
int opt = 1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
Bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
Listen(listenfd,20);
Listen(listenfd,20);
printf("Accepting connections../n");
while(1)
{
cliaddr_len = sizeof(cliaddr);
connfd = Accept(listenfd,(struct sockaddr *)&cliaddr,&cliaddr_len);
n = fork();
if(n ==-1 )
{
peeror("call to fork");
exit(1);
}else if(n == 0)
{
//child
Close(listenfd);
n = Read(connfd,buf,BUFF_SIZE);
if(n == 0)
{
printf("the other side has been closed.\n");
break;
}
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cliaddr.sin_port));
fputs(buf,stdout);
http_send(connfd,"hello world!");
Close(connfd);
exit(0);
}else
{
//parent
Close(connfd);
}
}
}
while(1)
{
cliaddr_len = sizeof(cliaddr);
connfd = Accept(listenfd,(struct sockaddr *)&cliaddr,&cliaddr_len);
n = fork();
if(n ==-1 )
{
peeror("call to fork");
exit(1);
}else if(n == 0)
{
//child
Close(listenfd);
n = Read(connfd,buf,BUFF_SIZE);
if(n == 0)
{
printf("the other side has been closed.\n");
break;
}
printf("received from %s at PORT %d \n",inet_ntop(AF_INET,&cliaddr.sin_addr,str,sizeof(str)),ntohs(cliaddr.sin_port));
fputs(buf,stdout);
http_send(connfd,"hello world!");
Close(connfd);
exit(0);
}else
{
//parent
Close(connfd);
}
}
}
**************************以下是wrap.c代码**********************************
#include<stdlib.h>
#include<errno.h>
#include<sys/socket.h>
#include<unistd.h>
#include<stdio.h>
#include<errno.h>
#include<sys/socket.h>
#include<unistd.h>
#include<stdio.h>
void peer_exit(const char *s)
{
perror(s);
exit(1);
}
{
perror(s);
exit(1);
}
int Accept(int fd,struct sockaddr *sa,socklen_t *salenptr)
{
int n;
again:
if((n = accept(fd,sa,salenptr)) < 0)
{
if((errno == ECONNABORTED)||(errno == EINTR))
{
goto again;
}else
{
perr_exit("accept error");
}
}
return n;
}
{
int n;
again:
if((n = accept(fd,sa,salenptr)) < 0)
{
if((errno == ECONNABORTED)||(errno == EINTR))
{
goto again;
}else
{
perr_exit("accept error");
}
}
return n;
}
void Bind (int fd,const struct sockaddr *sa,socklen_t salen)
{
if(bind(fd,sa,salen)<0)
{
perr_exit("bind error");
}
}
{
if(bind(fd,sa,salen)<0)
{
perr_exit("bind error");
}
}
void Connect (int fd,const struct sockaddr *sa ,socklen_t salen)
{
if(connect(fd,sa,salen) < 0)
{
perr_exit("connect error");
}
}
{
if(connect(fd,sa,salen) < 0)
{
perr_exit("connect error");
}
}
void Listen(int fd,int backlog)
{
if(listen(fd,backlog) < 0)
{
perr_exit("listen error");
}
}
{
if(listen(fd,backlog) < 0)
{
perr_exit("listen error");
}
}
int Socket(int family, int type,int protocol)
{
int n;
if((n = socket(family,type,protocol)) < 0)
{
perr_exit("socket error");
}
return n;
}
{
int n;
if((n = socket(family,type,protocol)) < 0)
{
perr_exit("socket error");
}
return n;
}
ssize_t Read(int fd,void *ptr,size_t nbytes)
{
ssize_t n;
again:
if((n = read(fd,ptr,nbytes)) == -1)
{
if(errno == EINTR)
{
goto again;
}else
{
return -1;
}
}
return n ;
}
{
ssize_t n;
again:
if((n = read(fd,ptr,nbytes)) == -1)
{
if(errno == EINTR)
{
goto again;
}else
{
return -1;
}
}
return n ;
}
ssize_t Write (int fd,const void *ptr,size_t nbytes)
{
ssize_t n;
again:
if((n = write(fd,ptr,nbytes)) == -1)
{
if(errno == EINTR)
{
goto again;
}else
{
return -1;
}
}
return n;
}
{
ssize_t n;
again:
if((n = write(fd,ptr,nbytes)) == -1)
{
if(errno == EINTR)
{
goto again;
}else
{
return -1;
}
}
return n;
}
ssize_t Readn(int fd,void *vptr,size_t n)
{
size_t nleft;
ssize_t nread;
char *ptr;
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = vptr;
nleft = n;
while(nleft >0)
{
if((nread = read(fd,ptr,nleft)) < 0)
{
if(errno == EINTR)
{
nread = 0;
}else if( nread == 0)
{
break;
}
}
nleft -= nread;
ptr += nread;
}
return n - nleft;
}
nleft = n;
while(nleft >0)
{
if((nread = read(fd,ptr,nleft)) < 0)
{
if(errno == EINTR)
{
nread = 0;
}else if( nread == 0)
{
break;
}
}
nleft -= nread;
ptr += nread;
}
return n - nleft;
}
ssize_t Writen(int fd,const void *vptr,size_t n)
{
size_t nleft;
ssize_t nwrittrn;
const char *ptr;
{
size_t nleft;
ssize_t nwrittrn;
const char *ptr;
ptr = vptr;
nleft = n;
nleft = n;
while(nleft > 0)
{
if((nwrittrn = write(fd,ptr,nleft)) <= 0)
{
if(nwrittrn < 0 && errno == EINTR)
{
nwrittrn = 0;
}else
{
return -1;
}
}
nleft -= nwrittrn;
ptr += nwrittrn;
}
return n;
}
{
if((nwrittrn = write(fd,ptr,nleft)) <= 0)
{
if(nwrittrn < 0 && errno == EINTR)
{
nwrittrn = 0;
}else
{
return -1;
}
}
nleft -= nwrittrn;
ptr += nwrittrn;
}
return n;
}
static ssize_t my_read(int fd,char *ptr)
{
static int read_cnt;
static char *read_ptr;
static char read_buf[100];
{
static int read_cnt;
static char *read_ptr;
static char read_buf[100];
if(read_cnt <= 0)
{
again:
if((read_cnt = read(fd,read_buf,sizeof(read_buf))) < 0)
{
if(errno == EINTR)
{
goto again;
}
return -1;
}else if(read_cnt == 0)
{
return 0;
}
read_ptr = read_buf;
}
read_cnt --;
*ptr = *read_ptr++;
return 1;
}
{
again:
if((read_cnt = read(fd,read_buf,sizeof(read_buf))) < 0)
{
if(errno == EINTR)
{
goto again;
}
return -1;
}else if(read_cnt == 0)
{
return 0;
}
read_ptr = read_buf;
}
read_cnt --;
*ptr = *read_ptr++;
return 1;
}
ssize_t Readline(int fd,void *vptr,size_t maxlen)
{
ssize_t n,rc;
char c,*ptr;
{
ssize_t n,rc;
char c,*ptr;
ptr = vptr;
for(n = 0;n < maxlen;n++)
{
rc = my_read(fd,&c);
if(rc == 1)
{
*ptr++ = c;
if(c == '\n')
{
break;
}
}else if(rc == 0)
{
*ptr = 0;
return n-1;
}else
{
return -1;
}
}
*ptr = 0;
return n;
}
for(n = 0;n < maxlen;n++)
{
rc = my_read(fd,&c);
if(rc == 1)
{
*ptr++ = c;
if(c == '\n')
{
break;
}
}else if(rc == 0)
{
*ptr = 0;
return n-1;
}else
{
return -1;
}
}
*ptr = 0;
return n;
}
void Close(int fd)
{
if(close(fd) == -1)
{
perr_exit("close error");
}
}
{
if(close(fd) == -1)
{
perr_exit("close error");
}
}
**************************以下是wrap.h代码*********************************
#ifndef WRAP_H
#define WRAP_H
#define WRAP_H
void perr_exit(const char *s);
int Accept(int fd,struct sockaddr *sa,socklen_t *salenptr);
void Bind(int fd,const struct sockaddr *sa,socklen_t salen);
void Connect(int fd,struct sockaddr *sa,socklen_t salen);
void Listen(int fd,int backlog);
int Socket(int family,int type,int protocol);
ssize_t Read(int fd,void *ptr,size_t nbytes);
ssize_t Write(int fd,void *ptr,size_t nbytes);
ssize_t Readn(int fd,void *vptr,size_t n);
ssize_t Writen(int fd,const void *vptr,size_t n);
ssize_t Readline(int fd,void *vptr,size_t maxlen);
void Close(int fd);
int Accept(int fd,struct sockaddr *sa,socklen_t *salenptr);
void Bind(int fd,const struct sockaddr *sa,socklen_t salen);
void Connect(int fd,struct sockaddr *sa,socklen_t salen);
void Listen(int fd,int backlog);
int Socket(int family,int type,int protocol);
ssize_t Read(int fd,void *ptr,size_t nbytes);
ssize_t Write(int fd,void *ptr,size_t nbytes);
ssize_t Readn(int fd,void *vptr,size_t n);
ssize_t Writen(int fd,const void *vptr,size_t n);
ssize_t Readline(int fd,void *vptr,size_t maxlen);
void Close(int fd);
#endif