- 论坛徽章:
- 0
|
基于socket win和unix的通讯问题
/*
* 外汇行情服务器端
* 作用:接收发来的行情数据
保存成本地文件
* 作者:netis netis@163.com
* 版本:
* 时间:2003-10-06
*
*/
#include <stdio.h>;
#include <strings.h>;
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/socket.h>;
#include <netinet/in.h>;
#include <arpa/inet.h>;
#include <curses.h>;
#define PORT 3131 /*端口*/
#define BACKLOG 2 /*允许的客户端*/
#define MAXDATASIZE 3000 /*每次传输的数据的最大长度 汉字为一半*/
#define FX_FLAG "fxdata.txt"/*行情数据包标志*/
#define MSG_FLAG "msg.txt" /*消息数据包标记*/
char lsCurTime[50];
int GetData(char *buf, int len);
int SaveData(char *filename, char *buf);
int main()
{
int listenfd, connectfd,numbytes;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size, opt=SO_REUSEADDR;
char buf[MAXDATASIZE], lsCurTime[50];
bzero(&buf,sizeof(buf));
bzero(&lsCurTime,sizeof(lsCurTime));
//system("clear" ;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Create socket failed\n" ;
exit(1);
}
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(listenfd,
(struct sockaddr *)&server,
sizeof(struct sockaddr)) == -1)
{
printf("Bind Error!\n" ;
exit(1);
}
if (listen(listenfd,BACKLOG) == -1)
{
printf("Listen Error!\n" ;
exit(1);
}
initial(); //初始化窗口
FunDispBaseWin("外汇宝行情服务器监控", "分行外汇宝交易室 2003" ;
FunShowTips(stdscr,"服务启动!", 0);
//printf("服务启动 等待查询请求!\n" ;
sin_size=sizeof(struct sockaddr_in);
while(1)
{
if ((connectfd = accept(listenfd,
(struct sockaddr *)&client,
&sin_size)) == -1)
{
exitwin();
printf("Accept Error!\n" ;
exit(1);
}
//printf("请求机器:[%s]\n",inet_ntoa(client.sin_addr));
sprintf(lsCurTime,"数据接收中 请求机器:[%s]",inet_ntoa(client.sin_addr));
FunShowTips(stdscr,lsCurTime, 0);
bzero(&lsCurTime,sizeof(lsCurTime));
send(connectfd, "OK", 2,0);
while (numbytes = recv(connectfd, buf, MAXDATASIZE,0))
{
buf[numbytes] = '\0';
if (!strncmp(buf,"***",3)){
bzero(&buf,sizeof(buf));
strcpy(buf,"服务器关闭!\n" ;
send(connectfd,buf,strlen(buf),0);
close(connectfd);
exitwin();
exit(1);
}
GetData(buf,numbytes);
//printf("收到请求信息:[%s]\n",buf);
send(connectfd,"OK",2,0);
bzero(&buf,sizeof(buf));
}
}
close(listenfd);
exitwin();
return 0;
}
/*
* 获取和分析数据
* 参数:
* char *buf 收到的数据包
*
*/
int GetData(char *buf, int len)
{
char tmpbuf[MAXDATASIZE], lsTmp[100];
bzero(&lsTmp,sizeof(lsTmp));
GetStrtime(1,lsCurTime);
if (memcmp(buf, FX_FLAG, 10) == 0)
{
//save fxdata file
bzero(&tmpbuf,sizeof(tmpbuf));
memcpy(tmpbuf, buf+strlen(FX_FLAG), len - strlen(FX_FLAG));
//printf("%s %s\n", lsCurTime, FX_FLAG);
sprintf(lsTmp, "时间[%s] 数据[%s]", lsCurTime, FX_FLAG);
FunCenterMsg(stdscr, lsTmp , 80, 9);
//FunShowTips(stdscr,FX_FLAG, 0);
SaveData(FX_FLAG, tmpbuf);
}else{
if (memcmp(buf, MSG_FLAG, 7) == 0)
{
//save msg file
bzero(&tmpbuf,sizeof(tmpbuf));
memcpy(tmpbuf, buf+strlen(MSG_FLAG), len - strlen(MSG_FLAG));
//printf("%s %s\n", lsCurTime, MSG_FLAG);
sprintf(lsTmp, "时间[%s] 数据[%s] ", lsCurTime, MSG_FLAG);
FunCenterMsg(stdscr, lsTmp , 80, 11);
//FunShowTips(stdscr,MSG_FLAG, 0);
SaveData(MSG_FLAG, tmpbuf);
}
}
return 0;
}
/*
* 保存收到的数据
* 参数:
*/
int SaveData(char *filename, char *buf)
{
FILE *fd;
if (( fd = fopen (filename,"w+" ) == NULL)
{
exitwin();
printf("%s%s","打开文件错:",filename);
exit( 1 );
}else{
fprintf(fd, "%s", buf);
fflush(fd);
}
fclose(fd);
return 0;
}
//使用的函数参考http://bbs.chinaunix.net/forum/viewtopic.php?t=88104&highlight=netis |
|