⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysocket.c

📁 一个智能门禁系统的linux的程序
💻 C
字号:
/*
 *  mysocket.c -- E-Sm@rt socket functions
 *
 * Copyright (C) 2002  hisense
 *
 * Author: kingmonkey <monkey96142@sina.com.cn>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Exceptionally, hisense grants discretionary and conditional permissions for 
 *additional use of the text contained in the company's release of the hisense
 *  e-sm@rt  provisions set forth hereunder.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the provisions of the GNU
 * General Public License.
 *
 * $Id: mysocket.c,v 1.0 2002/04/16 16:50:41 kingmonkey $
 *
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>

#include <linux/sockios.h>
#include <linux/socket.h>
#include <linux/if.h>
#include <linux/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>

#include "common.h"
#include "config.h"
#include "mysocket.h"

#define NALLOC 5

//Client	*client=NULL;
Client	client[10];
u8		client_size=10;
//u8		client_size=0;
/*
static void
client_alloc(void)
{
	int i;
	printf("this alloc for client array\n");
	if(client==NULL)
		client=malloc(NALLOC*sizeof(Client));
	else
		client=realloc(client,(client_size+NALLOC)*sizeof(Client));
	if(client==NULL)
		perror("can't alloc for client array");

	for(i=client_size;i<client_size+NALLOC;i++)
		client[i].fd=-1;

	client_size +=NALLOC;
}
*/
int 
client_add(int fd)
{
	int i;
/*	if(client==NULL)
		client_alloc();
again:*/
	for(i=0;i<client_size;i++)
		if(client[i].fd==0){
			client[i].fd=fd;
			return(i);
		}
/*
	client_alloc( );
	goto again;
*/
	return 0;
}

void
client_del(int fd)
{
 	int i;
 	for (i=0;i<client_size;i++){
 		if(client[i].fd==fd){
 			client[i].fd=0;
 			return;
 		}
 	}
 	printf("can't find client entry for fd %d\n",fd);
}

int 
server_open(u8 which){
	int conn_fd;
	struct sockaddr_in serv_addr;
	int ret;
	int flags;
	

	conn_fd=socket(AF_INET,SOCK_STREAM,0);
	bzero(&serv_addr,sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(server[which].port);
//	serv_addr.sin_addr=inet_addr(server[which].ip);
	inet_aton(server[which].ip,&serv_addr.sin_addr);

	ret=connect(conn_fd,(struct sockadddr *)&serv_addr,sizeof(struct sockaddr));
	if(ret==-1){
		perror("client connect error");
		return -1;	
	}
	if((flags=fcntl(conn_fd,F_GETFL,0))<0)
		perror("F_GETFL error");
	flags &=~O_NONBLOCK;
	if(fcntl(conn_fd,F_SETFL,flags)<0)
		perror("F_SETFL error");
	return conn_fd;	
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -