本文主要是介绍嵌入式 setsockopt设置socket—attr达到send立即发送不延时MTU,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
void start_jrm(void)
{
FILE *fp;
char buf[4096];
unsigned int myport;
int on = 1;
int sockfd,new_fd,len;
SOCK_ATTR sock_attr;
struct sockaddr_in my_addr, their_addr;
char certificate[128] = "cacert.pem";
char privatekey[128] = "privkey.pem";
fp = fopen(config_file, "r");
while(fgets(buf, sizeof(buf), fp)){
if(strstr(buf,"listen_port")){
myport = atoi(buf+sizeof("listen_port"));
}
}
fclose(fp);
eric_print("bind port %d\n",myport);
//start_config_warden();
if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP)) == -1) {
perror("socket");
exit(1);
} else
eric_print("socket created\n");
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(myport);//big or small endian problems
my_addr.sin_addr.s_addr = 0;//why????
setsockopt (sockfd, SOL_SOCKET,SO_REUSEADDR,TCP_NODELAY, (void *) &on, sizeof (on)); //Solve the port takes up
if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
} else
eric_print("binded\n");
if (listen(sockfd, 5) == -1) {
perror("listen");
exit(1);
} else{
eric_print("begin listen port %d\n",myport);
}
sock_attr.ctx =init_ssl(0);
sec_load_cert_file(sock_attr.ctx,certificate,privatekey);//install certificate in server
while(1){
len = sizeof(struct sockaddr);
//in the listening state
if ((new_fd = accept(sockfd, (struct sockaddr *) &their_addr,&len)) == -1) {
perror("accept");
eric_print("accept error! \n");
} else{
eric_print("tcp server: got connection from %s, port %d, socket %d\n",
inet_ntoa(their_addr.sin_addr),
ntohs(their_addr.sin_port), new_fd);
}
sock_attr.sockfd = new_fd;
handle_connection(sock_attr);//to oprate the new sock every time, then continue listening ???
}
}
目录
这篇关于嵌入式 setsockopt设置socket—attr达到send立即发送不延时MTU的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!