闲话少说。。。。。。。。。。。
基础的知识可参考《UNIX环境高级编程》
*************************************************************************************************************************************
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <netdb.h>
#define MONITOR_SERVER_IP "129.1.23.37"
#define MSSERVER_PRINT_PORT 7010
#define MSSERVER_LISTEN_PORT 7010
#define DBSERVER_PRINT_PORT 7020
#define DBSERVER_LISTEN_PORT 7020
#define MFSERVER_PRINT_PORT 7030
#define MFSERVER_LISTEN_PORT 7030
#define MFSERVER_READMETER_PORT 7031
#define MFSERVER_CCE_PORT 7032
#define LCSERVER_PRINT_PORT 7040
#define LCSERVER_LISTEN_PORT 7040
#define LCDSERVER_PRINT_PORT 7050
#define LCDSERVER_LISTEN_PORT 7050
#define CCE_PRINT_PORT 7060
#define CCE_PRINT_PORT 7060
#define MEDIATOR_PRINT_PORT 7070
#define PROCWATCH_PRINT_PORT 7080
#define PROCWATCH_LISTEN_PORT 7080
#define PIPE_EMULATOR_PORT_OFST 0x01
#define DEBUG_MSG_LOCAL_PORT 7009
#define ERR "ERROR:"
#define WAR "WARNING:"
#define uchar unsigned char
static int socket_fd = -1;
struct recvbuf
{
uchar buff[1024];
int len;
}recvbuf;
static int init_a_sock(int port)
{
socklen_t serv_len; // Length of client address
struct sockaddr_in serv_adr;
int fd;
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("udp SERVER socket ");
return -1;
}
memset( &serv_adr, 0, sizeof(serv_adr) ); // Clear structure
serv_adr.sin_family = AF_INET; // Set address type
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); // Any interface
serv_adr.sin_port = htons(port); // Use our fake port
serv_len = sizeof(serv_adr);
if (bind(fd, (struct sockaddr *) &serv_adr,serv_len ) < 0)
{
perror("SERVER bind ");
return -2;
}
return(fd);
}
static int socket_channel_init(int port)
{
int on = 1;
if (socket_fd > 0)
return 0;
socket_fd = init_a_sock(port);
if (socket_fd < 0)
{
printf("open socket fail\n");
return -1;
}
fcntl(socket_fd, F_SETOWN, getpid());
//ioctl(socket_fd, FIOASYNC, &on);
return 0;
}
int tp_initfifo(int port)
{
if (socket_channel_init(port) < 0)
return -1;
recvbuf.len = 0x00;
return 0;
}
int check_port_valid(int port)
{
if (port < 1024)
{
return(-1);
}
return(0);
}
int socket_read(int socket, uchar *buf, int len)
{
struct sockaddr_in cliaddr;
int readn, alen;
alen = sizeof(cliaddr);
//readn = recvfrom(socket_fd ,buf, len, MSG_DONTWAIT,(struct sockaddr *)&cliaddr, &alen);
readn = recvfrom(socket_fd ,buf, len, 0,(struct sockaddr *)&cliaddr, &alen);
return readn;
}
int read_data_from_port(void)
{
//int len = 0;
if (socket_fd < 0)
{
printf(ERR"SOMETHING IS NOT EXPECTED HAPPENED!!!\n");
return(-1);
}
recvbuf.len = socket_read(socket_fd, recvbuf.buff, 1000);
return(recvbuf.len);
}
void read_buffer_init(void)
{
memset(&recvbuf, 0x00, sizeof(recvbuf));
}
void print_belongs_to_which(char *process)
{
unsigned char buffer[0x100]={0};
int str_len=0;
time_t now_time;
struct tm *timenow; //实例化tm结构指针
time(&now_time);
timenow = localtime(&now_time);
sprintf(&buffer[str_len] ,"[%02d-%02d-%02d %02d:%02d:%02d]",timenow->tm_year+1900,timenow->tm_mon,
timenow->tm_mday, timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
str_len = strlen(buffer);
strcpy(&buffer[str_len], process);
str_len += strlen(buffer);
buffer[str_len] = ':';
str_len += 1;
printf("%s ",buffer);
fflush(stdout);
}
void print_buf_2_STDOUT(char *process)
{
int i,len ;
len = recvbuf.len;
fflush(stdout);
print_belongs_to_which(process);
fflush(stdout);
for (i=0; i < len; i++)
{
printf("%c",recvbuf.buff[i]);
}
printf("\n");
fflush(stdout);
}
/**
*
*
* @author Administrator (2014/7/7)
*
* @param argc
* @param argv
*
* @return int
*/
int main(int argc, char * argv[])
{
int len =0, nready =0;
fd_set rset;
int port = MSSERVER_PRINT_PORT;
if (argc >= 2)
{
sscanf(argv[1],"%d",&port);
}
if(check_port_valid(port) < 0)
{
printf(ERR"Invaid input----port!! \n");
return(-1);
}
tp_initfifo(port);
printf("\n start listening...\n");
while (1)
{
FD_ZERO(&rset);
FD_SET(socket_fd,&rset);
if((nready = select(socket_fd+1,&rset,NULL,NULL,NULL)) < 0 )
{
printf("select error.\n");
fflush(stdout);
continue;
}
if(0x00 == nready) continue;
read_buffer_init();
len = read_data_from_port();
if (len > 0)
{
print_buf_2_STDOUT("msserver");
//sleep(1);
}
else
{
//sleep(1);
}
}
return(0);
}