Chat server: Difference between revisions

Content added Content deleted
m (→‎{{header|C}}: mixing space and tabulation in indentation is always a bad idea!)
m (→‎{{header|C}}: more readable code)
Line 121: Line 121:
{
{
connections[handle] = malloc(sizeof(struct client));
connections[handle] = malloc(sizeof(struct client));
connections[handle]->buffer[0]='\0';
connections[handle]->buffer[0] = '\0';
connections[handle]->pos=0;
connections[handle]->pos = 0;
connections[handle]->name[0]='\0';
connections[handle]->name[0] = '\0';
}
}


Line 137: Line 137:
sprintf(buf, "* Disconnected: %s\r\n", connections[handle]->name);
sprintf(buf, "* Disconnected: %s\r\n", connections[handle]->name);


for (j=0; j<FD_SETSIZE; j++)
for (j = 0; j < FD_SETSIZE; j++)
{
{
if (handle !=j && j!=tsocket && FD_ISSET(j, &status))
if (handle != j && j != tsocket && FD_ISSET(j, &status))
{
{
if (write(j, buf, strlen(buf))<0)
if (write(j, buf, strlen(buf)) < 0)
{
{
CloseConnection(j);
CloseConnection(j);
Line 162: Line 162:
char *x;
char *x;
x=strchr(buf, '\n');
x = strchr(buf, '\n');
if (x) {*x='\0';}
if (x) { *x='\0'; }
x=strchr(buf, '\r');
x = strchr(buf, '\r');
if (x) {*x='\0';}
if (x) { *x='\0'; }
}
}


Line 171: Line 171:
{
{
char *begin, *end;
char *begin, *end;
int ret=0;
int ret = 0;
begin = connections[handle]->buffer;
begin = connections[handle]->buffer;
if (connections[handle]->pos==4000)
if (connections[handle]->pos == 4000)
{
{
if (begin[3999]!='\n')
if (begin[3999] != '\n')
begin[4000]='\0';
begin[4000] = '\0';
else {
else {
begin[4000]='\n';
begin[4000] = '\n';
begin[4001]='\0';
begin[4001] = '\0';
}
}
} else {
} else {
begin[connections[handle]->pos]='\0';
begin[connections[handle]->pos] = '\0';
}
}
end = strchr(begin, '\n');
end = strchr(begin, '\n');
while (end!=NULL)
while (end != NULL)
{
{
char output[8000];
char output[8000];
output[0]='\0';
output[0] = '\0';
if (!connections[handle]->name[0])
if (!connections[handle]->name[0])
{
{
strncpy(connections[handle]->name, begin, 31);
strncpy(connections[handle]->name, begin, 31);
connections[handle]->name[31]='\0';
connections[handle]->name[31] = '\0';
strip(connections[handle]->name);
strip(connections[handle]->name);
sprintf(output, "* Connected: %s\r\n", connections[handle]->name);
sprintf(output, "* Connected: %s\r\n", connections[handle]->name);
ret=1;
ret = 1;
} else
} else
{
{
sprintf (output, "%s: %.*s\r\n", connections[handle]->name,
sprintf(output, "%s: %.*s\r\n", connections[handle]->name,
end-begin, begin);
end-begin, begin);
ret=1;
ret = 1;
}
}
Line 208: Line 208:
{
{
int j;
int j;
for (j=0; j<FD_SETSIZE; j++)
for (j = 0; j < FD_SETSIZE; j++)
{
{
if (handle !=j && j!=tsocket && FD_ISSET(j, &status))
if (handle != j && j != tsocket && FD_ISSET(j, &status))
{
{
if (write(j, output, strlen(output))<0)
if (write(j, output, strlen(output)) < 0)
{
{
CloseConnection(j);
CloseConnection(j);
}
}
}
}

}
}

}
}
begin=end+1;
begin = end+1;
end = strchr(begin, '\n');
end = strchr(begin, '\n');
}
}
Line 232: Line 230:
void ClientText(int handle, char *buf, int buf_len)
void ClientText(int handle, char *buf, int buf_len)
{
{
int i,j;
int i, j;
if (!connections[handle])
if (!connections[handle])
return;
return;
j=connections[handle]->pos;
j = connections[handle]->pos;
for (i=0; i<buf_len; ++i, ++j)
for (i = 0; i < buf_len; ++i, ++j)
{
{
connections[handle]->buffer[j]=buf[i];
connections[handle]->buffer[j] = buf[i];
if (j==4000)
if (j == 4000)
{
{
while (RelayText(handle));
while (RelayText(handle));
j=connections[handle]->pos;
j = connections[handle]->pos;
}
}
}
}
Line 250: Line 248:
while (RelayText(handle));
while (RelayText(handle));
}
}


Line 256: Line 253:
int ChatLoop()
int ChatLoop()
{
{
int i,j;
int i, j;
FD_ZERO(&status);
FD_ZERO(&status);


Line 264: Line 261:
while(1)
while(1)
{
{
current=status;
current = status;
if (select(FD_SETSIZE, &current, NULL, NULL, NULL)==-1)
if (select(FD_SETSIZE, &current, NULL, NULL, NULL)==-1)
{
{
Line 270: Line 267:
return 0;
return 0;
}
}
for (i=0; i<FD_SETSIZE; ++i)
for (i = 0; i < FD_SETSIZE; ++i)
{
{
if (FD_ISSET(i, &current))
if (FD_ISSET(i, &current))
{
{
if (i==tsocket)
if (i == tsocket)
{
{
struct sockaddr_in cliinfo;
struct sockaddr_in cliinfo;
socklen_t addrlen = sizeof(cliinfo);
socklen_t addrlen = sizeof(cliinfo);
int handle;
int handle;
handle=accept(tsocket, &cliinfo, &addrlen);
handle = accept(tsocket, &cliinfo, &addrlen);
if (handle==-1)
if (handle == -1)
{
{
perror ("Couldn't accept connection");
perror ("Couldn't accept connection");
} else if (handle>FD_SETSIZE)
} else if (handle > FD_SETSIZE)
{
{
printf ("Unable to accept new connection.\n");
printf ("Unable to accept new connection.\n");
Line 290: Line 287:
else
else
{
{
if (write(handle, "Enter name: ", 12)>=0)
if (write(handle, "Enter name: ", 12) >= 0)
{
{
printf ("-- New connection %d from %s:%hu\n",
printf("-- New connection %d from %s:%hu\n",
handle,
handle,
inet_ntoa (cliinfo.sin_addr),
inet_ntoa (cliinfo.sin_addr),
Line 307: Line 304:
int b;
int b;


b=read(i, buf, 500);
b = read(i, buf, 500);
if (b<=0)
if (b <= 0)
{
{
CloseConnection(i);
CloseConnection(i);
Line 326: Line 323:
tsocket = socket(PF_INET, SOCK_STREAM, 0);
tsocket = socket(PF_INET, SOCK_STREAM, 0);


tsockinfo.sin_family=AF_INET;
tsockinfo.sin_family = AF_INET;
tsockinfo.sin_port=htons(7070);
tsockinfo.sin_port = htons(7070);
if (argc>1)
if (argc > 1)
{
{
tsockinfo.sin_port = htons(atoi(argv[1]));
tsockinfo.sin_port = htons(atoi(argv[1]));
Line 335: Line 332:
printf ("Socket %d on port %hu\n", tsocket, ntohs(tsockinfo.sin_port));
printf ("Socket %d on port %hu\n", tsocket, ntohs(tsockinfo.sin_port));


if (bind (tsocket, &tsockinfo, sizeof(tsockinfo))==-1)
if (bind(tsocket, &tsockinfo, sizeof(tsockinfo)) == -1)
{
{
perror("Couldn't bind socket");
perror("Couldn't bind socket");
Line 341: Line 338:
}
}


if (listen(tsocket, 10)==-1)
if (listen(tsocket, 10) == -1)
{
{
perror("Couldn't listen to port");
perror("Couldn't listen to port");