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

📄 tcpserv.c

📁 serverfox驱动的代码,对服务器/客户端模式感兴趣的可以看看!
💻 C
字号:
/*
 * Copyright (c) 2006 Jason Hua.  All rights reserved.
 * Permission to use or modify this software and its documentation only for
 * educational purposes and without fee is hereby granted, provided that
 * the above copyright notice appear in all copies.  The author makes no
 * representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

#include	<stdio.h>
#include 	<sys/time.h>
#include 	<unistd.h>
#include	"cliserv.h"

#define SYS_CTRL	0x00
#define PIC_CTRL	0x01
#define VID_CTRL	0x02
#define AUD_CTRL	0x03


int
main()					/* simple TCP server */
{
	struct sockaddr_in	serv, cli;
	struct timeval tv; 
	struct timezone tz;
	//unsigned int   request[10], reply[10];
	char   request[10], reply[10];
	int    listenfd, sockfd, n, clilen;
 	int i;

	for(i=0; i<10; i++)
		reply[i]=10-i;

	printf("tcp server begin!\n");
	if ( (listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
		err_sys("socket error");

	memset(&serv, 0, sizeof(serv));
	serv.sin_family      = AF_INET;
	serv.sin_addr.s_addr = htonl(INADDR_ANY);
	serv.sin_port        = htons(TCP_SERV_PORT);


	if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
		err_sys("bind error");
	printf("tcp server bind ok!\n");

	if (listen(listenfd, SOMAXCONN) < 0)
		err_sys("listen error");
	printf("tcp server listen ok!\n");

	for ( ; ; ) {
		clilen = sizeof(cli);
		if ( (sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
			err_sys("accept error");

		if ( (n = read_stream(sockfd, request, (sizeof(char)*10))) < 0)
			err_sys("read error");
		for(i=0;i<10;i++)
			printf("server received:%d\n",request[i]);
		if(request[0]==SYS_CTRL){
			if(request[1]==1){//time adjust
				printf("Time adjust!\n");
				for(i=0;i<10;i++)
					printf("server received:%d\n",request[i]);
#if 0
				request[2];//year
				request[3];//month
				request[4];//day
				request[5];//hour
				request[6];//minute
				request[6];//second
				if(settimeofday() < 0)
					err_sys("Time set error!\n");;
#endif
			}
			if(request[1]==2){//display the device
			}
			if(request[1]==3){//reset the device
			}
			if(request[1]==4){//display the status of status
			}
			if(request[1]==1){//time adjust
			}
			if(request[1]==1){//time adjust
			}
		}else if(request[0]==PIC_CTRL){
			if(request[1]==1){
				//set the param
			}
			if(request[1]==2){
				//get the param
			}
			if(request[1]==3){
				//begin to capture video
			}
			if(request[1]==4){
				//stop to capture video
			}
		}else if(request[0]==VID_CTRL){
			if(request[1]==1){
				//set the param
			}
			if(request[1]==2){
				//get the param
			}
			if(request[1]==3){
				//begin to capture video
			}
			if(request[1]==4){
				//stop to capture video
			}
		}else if(request[0]==AUD_CTRL){ 
			if(request[1]==1){
				//set the param
			}
			if(request[1]==2){
				//get the param
			}
			if(request[1]==3){
				//begin to capture audio
			}
			if(request[1]==4){
				//stop to capture audio
			}
		}else{//switch

		}


		/* process "n" bytes of request[] and create reply[] ... */

//		for(i=0; i++; i<10){
		if (write(sockfd, reply, (sizeof(unsigned int)*10) != (sizeof(unsigned int)*10)))
			err_sys("write error");
//		}
		close(sockfd);
	}
}

⌨️ 快捷键说明

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