2011-11-06 74 views
1

我一直在这台服务器,我使用memset的()来清除结构addrinfo中MEMSET似乎冻结我的程序C++和其它问题,我没有看到

#include <iostream> 
#include <string> 
#include <string.h> 
#include <errno.h> 
#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <ncurses.h> 
#include <vector> 

#include <netdb.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 

#define MAX_CONNECTIONS 10 
#define MAX_SRSIZE 500 

using namespace std; 

struct bcpackage{ 
    string * message; 
}; 
struct clientval{ 
    int fd; 
}; 

vector<int> file_descriptors; 
WINDOW * console, * input; 

void * handleClient(void * arg); 
void * broadcast(void * arg); 

int main(int argc, char **argv) 
{ 
    int myfd, * status; 
    status = new int(); 
    struct addrinfo myaddrinfo, *res; 

    /****************SETUP NCURSES UI******************/ 
    initscr(); 
    int y, x; 
    getmaxyx(stdscr, y, x); 
    console = subwin(stdscr,y - 1, x, 0, 0); 
    input = subwin(stdscr,1,x,y-1,0); 
    wrefresh(console); 
    wprintw(input,">"); 
    /**************************************************/ 
    string port = "25544"; 
    wprintw(console,"port: %s\n", port.c_str()); 
    memset(&myaddrinfo, 0, sizeof(myaddrinfo));//Problem I think with this memset() 
    myaddrinfo.ai_family = AF_UNSPEC; 
    myaddrinfo.ai_socktype = SOCK_STREAM; 
    myaddrinfo.ai_flags = AI_PASSIVE; 
    wprintw(console,"Starting Server\n"); 
    int aistat = getaddrinfo(NULL, "25544", &myaddrinfo, &res); 
    if(aistat == 0){ 
     wprintw(console,"Host Information Retrieved\n"); 
    } 
    else{ 
     wprintw(console, "Error : %d\n%s\n", aistat, gai_strerror(aistat)); 
     getch(); 
     endwin(); 
     exit(1); 
    } //We now have our address now we create a socket 
    myfd = socket(res->ai_family, res->ai_socktype,res->ai_protocol); 
    if(myfd==-1){ 
     wprintw(console, "Socket Creation Failed\n"); 
     wprintw(console,"Error: %d\n%s\n", errno, strerror(errno)); 
     getch(); 
     endwin(); 
     exit(2); 
    } 
    //If all went well, we now have a socket for our server 
    //we will now use the bind() function to bind our socket 
    //to our program. I think that is what it does at least. 
    *status = bind(myfd, res->ai_addr, res->ai_addrlen); 
    //wprintw(console, "Status: %d\n", *status); 
    if((*status) < 0){ 
     wprintw(console, "Bind failed\n"); 
     wprintw(console,"Error: %d\n%s\n", errno, strerror(errno)); 
     getch(); 
     endwin(); 
     exit(3); 
    } 
    else{ 
     wprintw(console, "Bind success\n"); 
    } 
    //Now that we are bound, we need to listen on the socket 
    *status = listen(myfd, MAX_CONNECTIONS); 
    if(status>=0){ 
     wprintw(console, "Listening on socket\n"); 
    } 
    else{ 
     wprintw(console, "Listen failed\n"); 
     wprintw(console,"Error: %d\n%s\n", errno, strerror(errno)); 
     getch(); 
     endwin(); 
     exit(4); 
    } 

    //Everything is setup now we send the server into a loop that will pass 
    //each client to a new pthread. 
    while(true){ 
     int *clientfd = new int(); 
     pthread_t * cliPID = new pthread_t(); 
     struct sockaddr_in * cliaddr = new struct sockaddr_in(); 
     socklen_t *clilen = new socklen_t(); 
     *clilen = sizeof(*cliaddr); 
     *clientfd = accept(myfd, (struct sockaddr *)cliaddr, clilen); 
     file_descriptors.push_back(*clientfd); 
     pthread_create(cliPID, NULL, handleClient, clientfd); 
    } 
    getch(); 
    endwin(); 
    return 0; 
} 

void * handleClient(void * arg){//Reads and writes to the functions 
    int filedesc = *((int *)arg); 
    string * rcvmsg = new string(); 
    while(!read(filedesc, rcvmsg, MAX_SRSIZE)<=0){ 
     if(rcvmsg->compare("")!=0){ 
      wprintw(console, "Client> %s\n", rcvmsg->c_str()); 
      broadcast(rcvmsg); 
     } 
     rcvmsg->clear(); 
    } 
    delete rcvmsg; 
    pthread_exit(NULL); 
} 

void * broadcast(void * arg){ 
    string * message = (string *)arg; 
    int num_fds = file_descriptors.size(); 
    for(int i = 0; i < num_fds; i++){ 
     write(file_descriptors.at(i), message, MAX_SRSIZE); 
    } 

} 

注意,这写的Linux,你需要添加这些链接命令编译时,-lpthread -lncurses。 最大的问题是,当我使用memset()行时,程序甚至不会在该行之前执行任何操作。它只是坐在那里。当我评论这条线时,它实际上运行。当我乘坐的memset()线路输出,的getaddrinfo()当我使用gai_strerror()找到与的getaddrinfo错误()给出了ai_socktype不支持错误 另一个错误是。请帮帮我。我真的陷入困境,我看不出有什么问题。

在此先感谢。

+0

您正在使用'memset'。问题在别的地方。 – dragonroot

+0

你绝对需要'memset()'行或'myaddrinfo'中的字段没有正确初始化。当'memset()'存在时,你的输出可能有问题。 – stardt

+0

问题在于你的用户界面,但我不熟悉ncurses。如果我使用'cout'而不是'wprintw',代码适用于我。 – stardt

回答

2

它运行得很好。发生什么事是,当程序退出时,你的ncurses窗口正在被销毁,并且所有的数据都消失了。

在你的return 0之前添加一个getch(),它会神奇的工作。

+0

我只是把我所有的代码,我使用getch()之前,我返回0,但它似乎仍然不工作。 –

+0

其实我认为这可能是因为msandiford的说法,我没有刷新屏幕。 –

相关问题