pusher服务端,push 客户端
pusher.c 文件
[root@centos6 pusher-0.1]# cat pusher.c
/*
* pusher for file push
* by xk
* 2012-10-30
*/
#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#include "utils.h"
#define PORT_NUMBER 7500
#define MAX_LISTEN_NUMBERS 20
#define BUFFER_SIZE 128
typedef struct sockaddr_in sockaddr_in;
typedef struct sockaddr sockaddr;
static void _show_usage();
int main( int argc, char* argv[])
{
int sockServer, newSocket;
if( argc == 2 && !strcmp(argv[1],"-D"))
{
printf("OK\n");
ul_daemon();
}
else
{
_show_usage();
exit(-1);
}
sockServer = socket(AF_INET,SOCK_STREAM,0);
if(-1 == sockServer)
{
perror("socket create error!\n");
exit(1);
}
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT_NUMBER);
serverAddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(serverAddr.sin_zero),8);
int errBind = bind(sockServer,(sockaddr*)&serverAddr,sizeof(sockaddr));
if(-1 == errBind)
{
perror("bind error!\n");
exit(1);
}
if(listen(sockServer,20) == -1)
{
perror("listen error!\n");
exit(1);
}
sockaddr_in clientAddr;
//int sin_size = sizeof(sockaddr_in);
socklen_t sin_size;
char dataBuffer[BUFFER_SIZE+1];
bzero(dataBuffer,BUFFER_SIZE+1);
/*waitting......*/
while(1)
{
printf("waitting connect......\n");
newSocket = accept(sockServer,(sockaddr*)&clientAddr,&sin_size);
if(-1 == newSocket)
{
perror("accept error!\n");
exit(1);
}
int errRecv = recv(newSocket,dataBuffer,BUFFER_SIZE,0);
if(-1 == errRecv)
{
perror("receive error!\n");
exit(1);
}
printf("received from client:%s\n",dataBuffer);
int errSend = send(newSocket,dataBuffer,strlen(dataBuffer),0);
if(-1 == errSend)
{
perror("send error!");
close(newSocket);
exit(1);
}
close(newSocket);
}
close(sockServer);
return 0;
}
/*_show_usage*/
void _show_usage()
{
printf("Usage: pusher -D\n");
}
push.c 文件
[root@centos6 pusher-0.1]# cat push.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PROT_NUMBER 7500
#define BUFFER_SIZE 128
typedef struct sockaddr_in sockaddr_in;
typedef struct in_addr in_addr;
typedef struct sockaddr sockaddr;
void createSocket(int* sockClient)
{
*sockClient = socket(AF_INET,SOCK_STREAM,0);
if(-1 == *sockClient)
{
perror("Error socket create!");
exit(1);
}
}
int main(int argc, char* argv[])
{
int sockClient;
/*create socket*/
createSocket(&sockClient);
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PROT_NUMBER);
char server_ip[20];
bzero(server_ip,sizeof(server_ip));
printf("Please input server IP:");
scanf("%s",server_ip);
printf("Server IP:%s\n",server_ip);
serverAddr.sin_addr.s_addr = inet_addr(server_ip);
bzero(&(serverAddr.sin_zero),8);
/*connecting server*/
int sockaddr_in_length = sizeof(sockaddr_in);
while(1)
{
int errConnect = connect(sockClient,(sockaddr*)&serverAddr,sockaddr_in_length);
if(-1 == errConnect)
{
continue;
}
else
break;
}
/*send "hello!"*/;
char dataBuffer[BUFFER_SIZE+1];
bzero(dataBuffer,BUFFER_SIZE+1);
strncpy(dataBuffer,"hello",strlen("hello"));
int errSend = send(sockClient,dataBuffer,strlen(dataBuffer),0);
if(-1 == errSend)
{
perror("send error!");
exit(1);
}
/*recv from server information*/;
bzero(dataBuffer,BUFFER_SIZE+1);
int recv_bytes = recv(sockClient,dataBuffer,BUFFER_SIZE,0);
if(recv_bytes < 0)
{
perror("receive error");
exit(1);
}
printf("recv from server:%s\n",dataBuffer);
close(sockClient);
return 0;
}
utils.h文件
[root@centos6 pusher-0.1]# cat utils.h
#ifndef _UTILS_H_
#define _UTILS_H_
void ul_daemon();
void ul_signal( int signum, void(*handler)(int));
#endif
utils.c文件
[root@centos6 pusher-0.1]# cat utils.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "utils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void ul_daemon()
{
int pid;
int fd;
int i = 0;
pid = fork();
if(pid < 0)
{
perror("fork fail!\n");
exit(-1);
}
if(pid > 0)
exit(0);
if( 0 > setsid())
{
perror("setsid fail\n");
exit(-1);
}
for(i=0;i<3;i++)
close(i);
if(0>(fd=open("/dev/null", O_RDWR)))
{
perror("open:/dev/null fail\n");
exit(-1);
}
dup(fd);
dup(fd);
ul_signal( SIGCHLD,SIG_IGN);
}
void ul_signal(int signum, void (*handler)(int))
{
struct sigaction sa;
if( 0 > sigfillset( &sa.sa_mask))
{
perror("sigfillset\n");
exit(-1);
}
sa.sa_flags=0;
sa.sa_restorer=0;
sa.sa_handler=handler;
if( 0 > sigaction( signum, &sa, NULL))
{
perror("sigaction fail\n");
exit(-1);
}
}
Makefile文件
[root@centos6 pusher-0.1]# cat Makefile
installdir=/usr/local/pusher
CFLAGS=-Wall -g -O0
all: pusher push
pusher: pusher.o utils.o
gcc $(CFLAGS) $^ -o $@
push: push.o
gcc $(CFLAGS) $^ -o $@
%.o: %.c
gcc $(CFLAGS) -c $< -o $@
.PHONY: clean
clean:
rm *.o pusher push -f