| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2434 人关注过本帖
标题:求助高手帮助移植C程序.linux移植到windows.内有源码
取消只看楼主 加入收藏
gujie007
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-2-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
求助高手帮助移植C程序.linux移植到windows.内有源码
我是新人,最近在linux下编写了一款游戏的ip转发程序,用来实现多个服务器在线人数合计的,一个小程序.现在需要把他运行在windows平台下,发现头文件和linux的完全不兼容,修改了几天,没有进展.前来求助论坛的大神们,有没有办法可以让我这个小程序运行在windows下.小弟感激不尽.

下面附上源码:


程序代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ev.h>

#define BACKLOG 256
#define MAXLEN 1024

char **m_argv;
int m_argc;

struct ev_portfw
{
    struct ev_io *ev_listen;
    
    unsigned long ip_addr;
    int connected;
};

volatile long count;
struct ev_thread
{
    char *server;
    int user;
    int maxuser;
    char motd[256];
    int motdlen;
    int ok;
};

// 设置socket为非阻塞式
void SetNonBlocking(int fd)
{
    int flags = fcntl(fd, F_GETFL);
    flags |= O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);
}

void SetOption(int s, int level, int optname, const void *optval, socklen_t optlen)
{
    if (setsockopt(s, level, optname, optval, optlen) < 0)
    {
        perror(strerror(errno));
    }
}

void release(struct ev_loop *loop, ev_io *w)
{
    struct ev_portfw *pf;
    pf = w->data;
    
    ev_io_stop(loop, pf->ev_listen);
        
    close(pf->ev_listen->fd);
    free(pf->ev_listen);
    free(pf);
}

int getserver(char *server, char *motd, int *motdlen, int *user, int *maxuser)
{
    char server_ip[256];
    int port = 0;
    char *p;
    if (NULL==(p=strstr(server,":"))) return 1;
    memset(server_ip, 0, sizeof(server_ip));
    memcpy(server_ip, server, p-server);
    port = atoi(p+1);

    int connect_fd;
    if((connect_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
        perror(strerror(errno));
        return 2;
    }
    
    int nReuseAddr = 1;
    SetOption(connect_fd, SOL_SOCKET, SO_REUSEADDR, &nReuseAddr, sizeof(int));
    
    struct sockaddr_in s_in;
    bzero(&s_in, sizeof(s_in));
    s_in.sin_family = AF_INET;
    s_in.sin_port = htons(port);
    s_in.sin_addr.s_addr = inet_addr(server_ip);
    
    if (connect(connect_fd, (struct sockaddr *)&s_in,sizeof(struct sockaddr)) < 0)
    {
        printf("connect to server failure\n");
        perror(strerror(errno));
        close(connect_fd);
        return 3;
    }
    
    unsigned char buf[2048];
    buf[0] = 0xfe;
    if (send(connect_fd, buf, 1, 0) != 1)
    {
        printf("send packet to server failure\n");
        perror(strerror(errno));
        close(connect_fd);
        return 4;
    }
    
    int recvlen = recv(connect_fd, buf, sizeof(buf), 0);
    if (recvlen <= 0)
    {
        printf("recv packet from server failure\n");
        perror(strerror(errno));
        close(connect_fd);
        return 4;
    }
    close(connect_fd);
    
    int len = htons(*(unsigned short *)(buf+1));
    int i;
    *motdlen = 0;
    for (i=3; i<recvlen; i++)
    {
        motd[i-3] = buf[i];
        if (buf[i] == 0x00 && buf[i+1] == 0xa7){
            *motdlen = i-3;
            break;
        }
    }
    if (*motdlen == 0)
    {
        printf("get motd len error\n");
        return 5;
    }
    *user = 0;
    *maxuser = 0;
    int mod = 0;
    for (i=3+*motdlen+3; i< recvlen; i+=2)
    {
        if (mod == 0)
        {
            *user = *user * 10 + buf[i]-48;
        }else
        {
            *maxuser = *maxuser * 10 + buf[i]-48;
        }
        if (buf[i+1] == 0x00 && buf[i+2] == 0xa7){
            mod++;
            i+=2;
        }
    }
    printf("Server: %s  Port: %d  user: %d  max: %d\n", server_ip, port, *user, *maxuser);
    return 0;
}

void send_ping(int sock, char *motd, int motdlen, int user, int maxuser)
{
    unsigned char buf[2048];
    char szuser[10];
    char szmaxuser[10];
    int extlen = 0;
    int userlen = 0;
    int maxuserlen = 0;
    int i;
    
    sprintf(szuser, "%d", user);
    userlen = strlen(szuser);
    extlen+=userlen;
    
    sprintf(szmaxuser, "%d", maxuser);
    maxuserlen = strlen(szmaxuser);
    extlen+=maxuserlen;
    buf[0] = 0xff;
    *(unsigned short *)(buf+1) = htons(motdlen/2 + 2 + extlen);
    memcpy(buf+3, motd, motdlen);
    buf[motdlen+3] = 0x00;
    buf[motdlen+3+1] = 0xa7;
    
    for (i=0;i<userlen;i++)
    {
        buf[motdlen+5+i*2] = 0;
        buf[motdlen+5+i*2+1] = szuser[i];
    }
    
    buf[motdlen+userlen*2+5] = 0x00;
    buf[motdlen+userlen*2+6] = 0xa7;
    
    for (i=0;i<maxuserlen;i++)
    {
        buf[motdlen+userlen*2+7+i*2] = 0;
        buf[motdlen+userlen*2+7+i*2+1] = szmaxuser[i];
    }
    
    send(sock, buf, motdlen+extlen*2 + 7, 0);
}

void *get_routine(void *parg)
{
    struct ev_thread *arg = parg;
    int rc;
    rc = getserver(arg->server, arg->motd, &arg->motdlen, &arg->user, &arg->maxuser);
    if (rc == 0)
    {
        arg->ok = 1;
    }
    __sync_add_and_fetch(&count, 1);
}

void on_recv(struct ev_loop *loop, ev_io *w, int revents)
{
    struct ev_portfw *pf;
    pf = w->data;
   char szaddr[32];
   struct in_addr addr;
    
    unsigned char buffer[1024]={0};
    int ret =0;
    //ev_io write_event;
loop:
    ret=recv(w->fd,buffer,MAXLEN,0);
    if(ret > 0)
    {
        if (buffer[0] == 0xFE)
        {
            char *motd;
            int motdlen;
            int totaluser = 0;
            int totalmax = 0;
            int *user;
            int *maxuser;
            user = (int *)malloc(sizeof(int)*(m_argc-2));
            maxuser = (int *)malloc(sizeof(int)*(m_argc-2));
            int i;
            
            struct ev_thread *arg;
            arg = (struct ev_thread *)malloc(sizeof(struct ev_thread)*(m_argc-2));
            
            count = 0;
            pthread_attr_t attr;
            pthread_attr_init (&attr);
            for (i=2;i<m_argc;i++)
            {
                pthread_t thread;
                pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
                
                arg[i-2].ok = 0;
                arg[i-2].user = 0;
                arg[i-2].maxuser = 0;
                arg[i-2].server = m_argv[i];
                arg[i-2].motdlen = &motdlen;
                
                pthread_create(&thread, &attr, get_routine, &arg[i-2]);
            }
            
            time_t st = time(NULL);
            
            while (count < m_argc - 2 && time(NULL)-st < 5)
            {
                usleep(1000);    //sleep 1ms
            }
            
            for (i=0;i<m_argc-2;i++)
            {
                if (arg[i].ok)
                {
                    motd = arg[i].motd;
                    motdlen = arg[i].motdlen;
                }
                totaluser+=arg[i].user;
                totalmax+=arg[i].maxuser;
            }
            
            send_ping(w->fd, motd, motdlen, totaluser, totalmax);

//            send(w->fd, init, init_len, 0);
        }
        release(loop, w);
    }
    else if(ret ==0)
    {
        printf("listen socket closed!socket fd: %d\n",w->fd);
        release(loop, w);
        return;
    }
    else
    {
        if(errno == EAGAIN ||errno == EWOULDBLOCK)
        {
            goto loop;
        }
        else
        {
            printf("ret :%d ,server close socket fd : %d\n",ret,w->fd);
            release(loop, w);
            return;
        }
    }
}

void on_accept(struct ev_loop *loop, ev_io *w, int revents)
{
    int newfd;
    struct sockaddr_in sin;
    struct ev_portfw *pf;
    ev_io* accept_watcher;
    
    socklen_t addrlen = sizeof(struct sockaddr);
    while ((newfd = accept(w->fd, (struct sockaddr *)&sin, &addrlen)) < 0)
    {
        if (errno == EAGAIN || errno == EWOULDBLOCK) 
        {
            //these are transient, so don't log anything.
            continue; 
        }
        else
        {
            printf("accept error.[%s]\n", strerror(errno));
            break;
        }
    }
    
    pf = malloc(sizeof(struct ev_portfw));
    pf->connected = 0;
    pf->ip_addr = sin.sin_addr.s_addr;
    
    accept_watcher=malloc(sizeof(ev_io));
    accept_watcher->data = pf;
    
    pf->ev_listen = accept_watcher;
    
    ev_io_init(accept_watcher,on_recv,newfd,EV_READ);
    ev_io_start(loop,accept_watcher);
}

void usage()
{
    printf("Usage:\n  portfw <Listen Port> <Server address>:<Server Port> ...\n\n");
    exit(1);
}

int main(int argc, char *argv[])
{
    struct ev_loop *loop = EV_DEFAULT;
    
    struct ev_io ev_accept;

    int listen_fd;
    struct sockaddr_in server_addr;

    struct rlimit rt;
    /* set max limit */
    rt.rlim_max = rt.rlim_cur = 65536;
    if (setrlimit(RLIMIT_NOFILE, &rt) == -1) {
      perror("setrlimit");
      exit(1);
    }else
    {
        fprintf(stdout,"set rlimit success: %d\n",65536);
    }
    m_argv = argv;
    m_argc = argc;
    
    if (argc < 2 || atoi(argv[1])==0)
    {
        usage();
    }
    
    printf("\e[32mListen port: %d \e[0m\n", atoi(argv[1]));
    
    if((listen_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
        perror(strerror(errno));
        exit(-1);
    }
    printf("socket ok\n");

    int nReuseAddr = 1;
    //设置socket选项,允许重用本地地址
    SetOption(listen_fd, SOL_SOCKET, SO_REUSEADDR, &nReuseAddr, sizeof(int));

    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(atoi(argv[1]));
    server_addr.sin_addr.s_addr = INADDR_ANY;

    if(bind(listen_fd,(struct sockaddr*)&server_addr,sizeof(struct sockaddr))==-1)
    {
        perror(strerror(errno));
        exit(-1);
    }

    if(listen(listen_fd,BACKLOG)==-1)
    {
        perror(strerror(errno));
        exit(-1);
    }

    SetNonBlocking(listen_fd);

    printf("start listen\n");
    ev_io_init (&ev_accept, on_accept, listen_fd, EV_READ);
    ev_io_start (loop, &ev_accept);
    
    ev_loop(loop,0);
    ev_loop_destroy(loop);
    
    return 0;
}


-----------------------------分割线-----------------------------------------
以下是Makefile,虽然我知道应该和他没关系,以防万一,贴出来...拜托大家了

程序代码:
IDIR=/usr/include
LDIR=/usr/lib
OBJS=portfw.c
CC=gcc
CFLAGS=-c -g -Wall -march=i686
LDFLAGS=-O3 -D_FILE_OFFSET_BITS=64 -lev -lpthread
TARGET=portfw

all: $(TARGET)
    @echo done.
    
$(TARGET): $(OBJS)
    $(CC) $(LDFLAGS) -L$(LDIR) -I$(IDIR) $(OBJS) -o $@

.c.o:
    $(CC) $(CFLAGS) -o $@ $<
    
.cpp.o:
    $(CC) $(CFLAGS) -o $@ $<

clean:
    rm *.o -f
    rm $(TARGET) -f

install:
    cp $(TARGET) /usr/local/sbin/$(TARGET)



[ 本帖最后由 gujie007 于 2012-2-21 17:59 编辑 ]
搜索更多相关主题的帖子: windows linux 在线人数 color 
2012-02-21 17:55
gujie007
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-2-21
收藏
得分:0 
我把linux下的头文件搬到windows下了,依然不行....有没有高手帮我改一下.我愿意出钱,联系方式QQ:100299100
2012-02-22 23:34
快速回复:求助高手帮助移植C程序.linux移植到windows.内有源码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025699 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved