我意识到这种担忧几乎总是由于一个简单的错误,但我不能为我的生活找到这个错误的原因。我已多次浏览括号并检查我是否遗漏了任何内容,但没有看错。在我初始化数组时,令人作呕的方式令人厌恶,出了什么问题?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <sys/poll.h>
#define BufferLength 100
#define SERVPORT 5011
int main(int argc, char * argv[])
{
int sd, sd2, rc, length = sizeof(int);
int totalcnt = 0, on = 1;
char temp;
char buffer[BufferLength];
char init[10];
struct sockaddr_in serveraddr;
struct sockaddr_in their_addr;
fd_set read_fd;
struct timeval timeout;
timeout.tv_sec = 60;
timeout.tv_usec = 0;
int new_sd;
int nfds = 1; int len;
//..........................SOCKET SETUP..............................................||
if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket error");
exit (-1);
}
if((rc = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) < 0)
{
perror("socket setup error");
close(sd);
exit (-1);
}
memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("Using %s, listening at %d\n", inet_ntoa(serveraddr.sin_addr), SERVPORT);
//............................BINDING...............................................||
if((rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
{
perror("server binding error");
close(sd);
exit(-1);
}
//............................LISTENING.............................................||
if((rc = listen(sd, 8)) < 0)
{ perror("listen error"); close(sd); exit (-1);}
else
printf("Ready for up to eight connections...\n");
int sin_size = sizeof(struct sockaddr_in);
//............................ACCEPTING..............................................||
if((sd2 = accept(sd, (struct sockaddr *)&their_addr, &sin_size)) < 0)
{ perror("Server-accept() error"); close(sd); exit (-1); }
printf("Got connection from the accursed client: %s\n", inet_ntoa(their_addr.sin_addr));
FD_ZERO(&read_fd);
FD_SET(sd2, &read_fd);
//..................................POLL STUFF.......................................||
int i,n;
int m = 0;
char ** init0 = (char**)malloc(9);
for( m; m<10; m+=1)
init0[m] = (char*)malloc(100);
char * left = (char*)malloc(100);
char * enter = (char*)malloc(100);
char ** Tinit0 = (char**)malloc(9);
for(m = 0; m<10; m+=1)
Tinit0[m] = (char*)malloc(100);
memset(left,0,100);
for(m = 0; m<10; m+=1)
memset(Tinit0[m],0,100);
for(m = 0; m<10; m+=1)
memset(init0[m],0,100);
memset(enter, 0, 100);
strcpy(enter,"Entered:_ ");
strcpy(left,"Left: ");
struct pollfd ufds[9];
ufds[0].fd = sd;
ufds[0].events = POLLIN;
ufds[1].fd = sd2;
ufds[1].events = POLLOUT;
ufds[2].fd = sd2;
ufds[2].events = POLLOUT;
ufds[3].fd = sd2;
ufds[3].events = POLLOUT;
ufds[4].fd = sd2;
ufds[4].events = POLLOUT;
ufds[5].fd = sd2;
ufds[5].events = POLLOUT;
ufds[6].fd = sd2;
ufds[6].events = POLLOUT;
ufds[7].fd = sd2;
ufds[7].events = POLLOUT;
ufds[8].fd = sd2;
ufds[8].events = POLLOUT;
//timeout = (6000);
n = poll(ufds, 9, 40000);
do{
for (i = 0; i < 9; i++)
{
if(ufds[i].revents == 0)
continue;
if (ufds[i].fd == sd)
{
do
{
new_sd = accept(sd2, NULL, NULL);
ufds[nfds].fd = new_sd;
ufds[nfds].events = POLLIN;
nfds++;
} while (new_sd != -1);
}
else
{
do
{
rc = recv(ufds[i].fd, buffer, sizeof(buffer), 0);
len =rc;
if(strcmp(init0[i],"") == 0) // IF INITAL HAS NOT BEEN SET UP
{ // (DURING FIRST CONNECTION FROM CLIENT)
if((rc == 1) && (FD_ISSET(sd2, &read_fd)))
{
printf("initial from client is: %s\n",buffer);
strcpy(init0[i],buffer);
printf("init0 is: %s\n", init0[i]); Tinit0[i] = init0[0];
}
strcat(enter, init0[i]);
printf("string enter is: %s , about to try write enter\n",enter);
for(m = 1; m <9; m+=1)
rc = send(ufds[m].fd, enter, 20, 0);
}
strcat(Tinit0[i],"__");
for(m = 1; m <9; m+=1)
rc = send(ufds[m].fd, Tinit0[i], 100, 0);
if(rc==0)
{
strcat(left, init0[i]);
printf("%s\n",left);
for(m = 1; m <9; m+=1)
rc = send(ufds[m].fd, left, 100, 0);
}
} while(1==1);
} //end of else
} //end of for
} //end of do
}
答案 0 :(得分:4)
您的do {} while()
循环错过了while
关键字。