- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用C编写了一个简单的代理服务器代码。它仅在尝试连接到www.yahoo.com/news.html时才起作用。
但其他网站没有。(例如 google 或 cnn 等)
当程序首先转到“accept()”时,就OK了。
所有变量都有自己的值。
但是在由“while()”引起的第二个“accept()”之后,程序关闭了。
直到那时,变量(sockfd2、newsockfd)仍然有自己的值。
这很奇怪,因为我在进入 while() 之前关闭了它们。
我真的不知道为什么我的程序关闭,以及为什么它有时告诉我“地址已在使用中”。
是不是我漏掉了什么?请告诉我。
这是代码。
/*
A simple server in the internet domain using TCP
Usage:./server port (E.g. ./server 10000 )
*/
#include <stdio.h>
#include <sys/types.h> // definitions of a number of data types used in socket.h and netinet/in.h
#include <sys/socket.h> // definitions of structures needed for sockets, e.g. sockaddr
#include <netinet/in.h> // constants and structures needed for internet domain addresses, e.g. sockaddr_in
#include <stdlib.h>
#include <strings.h>
#include <netdb.h> // define structures like hostent
void error(char *msg)
{
perror(msg);
exit(1);
}
char *replaceAll(char *s, const char *olds, const char *news) {
char *result, *sr;
size_t i, count = 0;
size_t oldlen = strlen(olds); if (oldlen < 1) return s;
size_t newlen = strlen(news);
if (newlen != oldlen) {
for (i = 0; s[i] != '\0';) {
if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
else i++;
}
} else i = strlen(s);
result = (char *) malloc(i + 1 + count * (newlen - oldlen));
if (result == NULL) return NULL;
sr = result;
while (*s) {
if (memcmp(s, olds, oldlen) == 0) {
memcpy(sr, news, newlen);
sr += newlen;
s += oldlen;
} else *sr++ = *s++;
}
*sr = '\0';
return result;
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd; //descriptors rturn from socket and accept system calls
int sockfd2, newsockfd2; //Socket descriptor
int portno; // port number
int portno2, n2;
socklen_t clilen;
char buffer[1024];
char resMsg[2048];
char bufferForAddr[1024];
char ip[100];
/*sockaddr_in: Structure Containing an Internet Address*/
struct sockaddr_in serv_addr, cli_addr;
struct sockaddr_in serv_addr2;
struct hostent *server2; //contains tons of information, including the server's IP address
struct in_addr **addr_list;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
/*Create a new socket
AF_INET: Address Domain is Internet
SOCK_STREAM: Socket Type is STREAM Socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
/*browser's port no is 80*/
portno2 = 80;
sockfd2 = socket(AF_INET, SOCK_STREAM, 0); //create a new socket
if (sockfd2 < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]); //atoi converts from String to Integer
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; //for the server the IP address is always the address that the server is running on
serv_addr.sin_port = htons(portno); //convert from host to network byte order
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) //Bind the socket to the server address
error("ERROR on binding");
listen(sockfd,5); // Listen for socket connections. Backlog queue (connections to wait) is 5
clilen = sizeof(cli_addr);
/*accept function:
1) Block until a new connection is established
2) the new socket descriptor will be used for subsequent communication with the newly connected client.
*/
while(1){
printf("==========================main========================\n");
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,1024);
n = read(newsockfd,buffer,1023); //Read is a block function. It will read at most 255 bytes
if (n < 0) error("ERROR reading from socket");
printf("Here is the message1: %s\n",buffer);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
get an info about destination site from browser(client)
*/
printf("Here is the message2: %s\n",buffer);
char *reqMSG;
char *address;
reqMSG = replaceAll(buffer, "HTTP/1.1", "HTTP/1.0");
reqMSG = replaceAll(reqMSG, "keep-alive", "close");
//strcat(reqMSG, "\r\nProxy-Connection: keep-alive\r\n");
strcat(reqMSG,"\r\n");
//printf("MSG : %s",reqMSG);
strcpy(bufferForAddr,buffer);
address = strtok(bufferForAddr+11," ");
address = strtok(address,"/");
printf("address : %s\n",address);
printf("MSG : %s\n",reqMSG);
/*
Q2. make a algorithm for filtering about cached site.
*/
/*
if the site is not cached, send the Msg to it's ip address!
in this section, I need to use functions each called connect, write and read.
*/
server2 = gethostbyname(address); //takes a string like "www.yahoo.com", and returns a struct hostent which contains information, as IP address, address type, the length of the addresses...
//get an ip address from hostname
/*
addr_list = (struct in_addr **) server2->h_addr_list;
int i;
for(i = 0; addr_list[i] != NULL; i++)
{
strcpy(ip , inet_ntoa(*addr_list[i]) );
}
*/
if (server2 == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr2, sizeof(serv_addr2));
serv_addr2.sin_family = AF_INET; //initialize server's address
bcopy((char *)server2->h_addr, (char *)&serv_addr2.sin_addr.s_addr, server2->h_length);
serv_addr2.sin_port = htons(portno2);
if (connect(sockfd2,(struct sockaddr *)&serv_addr2,sizeof(serv_addr2)) < 0){ //establish a connection to the server
error("ERROR connecting");
}
//send a REQ MSG from above step!
//do{
n2 = write(sockfd2,reqMSG,strlen(reqMSG)); //write to the socket
if (n2 < 0) {
error("ERROR writing to socket");
//break;
}
printf("What's wrong : %s\n",reqMSG);
printf("%s\n",resMsg);
bzero(resMsg,2047);
while (n2 = read(sockfd2,resMsg,2047)>0){ //read from the socket
printf("=================================sub=============================\n");
if (n2 < 0){
error("ERROR reading from socket");
//break;
}
printf("resMsg : %s\n",resMsg);
//printf("\nI'm in the loop!\n");
//}while(1);
/*
if I get response Msg to my destination site, I will return it to my source site.
and wait for closing Msg to browser(client).
*/
write(newsockfd,resMsg,n2); //NOTE: write function returns the number of bytes actually sent out Ñ> this might be less than the number you told it to send
if (n < 0) error("ERROR writing to socket");
bzero(resMsg,2047);
}
close(sockfd2);
/*
make a log file and save this site!
*/
//printf("\nip : %s \n",ip);
close(newsockfd);
}
close(sockfd);
return 0;
}
最佳答案
绑定(bind)之前尝试
int tr=1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &tr, sizeof(int)) == -1) {
perror("setsockopt");
}
关于c - c 套接字编程错误 : after accept(), 程序将关闭。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27062053/
在 HTTP 中,您可以在请求中指定您的客户端可以使用 accept header 接受响应中的特定内容,其值如 application/xml。内容类型规范允许您在内容类型中包含参数,例如chars
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我的 RESTFul API 只能响应 JSON 编码的数据(即我所有的 header 都有 Content-Type: application/json)。如果请求具有不允许 JSON 的 Acce
我有 C linux TCP 客户端/服务器应用程序。我想出了一个奇怪的场景,但我不知道这个应用程序是否有任何后果。我有一个可以接受 N 个连接的服务器端,例如这个服务器将接受 100 个连接。在这种
我试图找出 的可能值范围接受 和 接受-联系 header 字段,但我在 RFC 中找不到完整列表。有谁知道他们在哪里?我经常看到 Accept: application/sdp;level=1,
在 TCP 套接字代码中,我们创建了 2 个套接字。第一个接受新连接,第二个接受来自客户端的数据并在新连接建立时创建。 TCP header 中的哪个控制位允许服务器知道将此段传递给 ServerSo
我可以设置 Request.Content-Type = ... , Request.Content-Length = ... 如何设置Accept和Accept-Language? 我想上传一个文件
假设A是服务器,B是客户端。 B 创建一个到服务器 A 的套接字 b 并 ServerSocket.accept() 创建一个相应的套接字 a 对于客户端 B 现在,如果服务器 A 关闭了 a 但客户
如果客户端connect()先于服务端accept()成功,那么客户端如何知道服务端accept()成功呢?如果在 connect() 成功和服务器 accept() 之间的时间间隔调用 send()
这是我的代码: void error(const char *msg) { perror(msg); exit(1); } void sServer::acceptClientConn
在我的 pom.xml 中有以下详细信息 org.codehaus.jackson jackson-core-asl 1.9.13
假设我有两个如下所示的端点: @GET @Path("/blah") @Produces(MIME_TYPE_1) public Thing getThing() { .... } @GET
我的 Controller : @RestController public class ClawerController { @RequestMapping("/hello"
我不想要可视化 merge 工具,我也不希望必须 vi 冲突文件并手动在 HEAD(我的)和导入的更改(他们的)之间进行选择。大多数时候,我要么想要他们的所有更改,要么想要我的所有更改。通常这是因为我
我正在使用 spring 4.1.1.RELEASE 并包括: pom 中的 jackson-core-asl 1.9.13 和 jackson-mapper-asl 1.9.13 来创建一个带有 R
我正在尝试开发自己的基于非阻塞 NIO 消息的通信库。我已经阅读了 1000 个关于它的教程和书籍章节,我认为最终我得到了一些可以在很少的同时连接下工作的东西。但是当我在服务器端共存许多连接时,我遇到
我正在尝试编写一个 Bind 元编程模板辅助元函数,将模板参数绑定(bind)到某物。 我有一个简单的模板元函数的工作实现: template struct MakePair { using
我使用 spring 构建 Restful API,当我访问以下方法时: // get the entity in DB by using id number @RequestMapping(
只是想知道是否有一种方法可以通过 ASP.NET Core 中的属性强制检查 HTTP header 值是否属于一组给定值(例如 application/json,应用程序/xml) 我知道有 Pro
我是一名优秀的程序员,十分优秀!