我一直在這臺服務器,我使用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不支持錯誤 另一個錯誤是。請幫幫我。我真的陷入困境,我看不出有什麼問題。
在此先感謝。
您正在使用'memset'。問題在別的地方。 – dragonroot
你絕對需要'memset()'行或'myaddrinfo'中的字段沒有正確初始化。當'memset()'存在時,你的輸出可能有問題。 – stardt
問題在於你的用戶界面,但我不熟悉ncurses。如果我使用'cout'而不是'wprintw',代碼適用於我。 – stardt