Hey all!
So i've been working on a unix chat server and I think I've done a good job having no idea what I'm doing :-p. The only issue I'm running into is that I can't get it to go 1 to multiple. For instance, the idea is that any number of my friends can attach to the server and then the server will spit the message back to all of them. Can you help me with this?
My thoughts were to store the client's connections in a vector somehow and then that way it could always add new clients but I'm not sure how to do that.
Server Portion:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 500
#define PORT 10122
int main(int argc,char * argv[]) {
int s;
int rtn;
char buff[BUF_SIZE];
struct sockaddr_in addr;
int incoming_s;
socklen_t incoming_len;
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1) {
perror("Failed to get socket");
exit(EXIT_FAILURE);
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
rtn = bind(s, (struct sockaddr*)&addr, sizeof(addr));
if (rtn == -1) {
perror("Failed to get socket");
exit(EXIT_FAILURE);
}
rtn = listen(s, 10);
if (rtn == -1) {
perror("Failed to listen on socket");
exit(EXIT_FAILURE);
}
incoming_len = sizeof(addr);
memset(&addr, 0, sizeof(addr));
incoming_s = accept(s, (struct sockaddr *)&addr, &incoming_len);
if (incoming_s == -1) {
perror("Failed to accept incoming connection");
exit(EXIT_FAILURE);
}
printf("Accepted incoming connection\n");
for (;;) {
ssize_t len = read(incoming_s, buff, BUF_SIZE);
/* read returns -1 on error. Don't want to index an array with -1! */
if (len < 0) {
fprintf(stderr, "Failed to read network data\n");
}
else if (len == 0) {
fprintf(stderr, "No response data read\n");
}
else {
buff[len] = '\0';
printf("client says : %s", buff);
printf("speak ");
}
/*gets(buff);*/ /* gets is BAD!!! */
if (fgets(buff, BUF_SIZE, stdin) == NULL) {
fprintf(stderr, "Failed to read input\n");
exit(EXIT_FAILURE);
}
len = write(incoming_s, buff, strlen(buff));
if (len == -1) {
perror("Failed to write\n");
exit(EXIT_FAILURE);
}
}
return 0;
}
Client Portion:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 500
#define PORT 10122
int main(void) {
int s;
int rtn;
char buff[BUF_SIZE];
struct sockaddr_in addr;
puts("Entering program");
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1) {
perror("Failed to get socket");
exit(EXIT_FAILURE);
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
rtn = connect(s, (struct sockaddr * )&addr, sizeof(addr) );
if (rtn == -1) {
perror("Failed to connect to server");
exit(EXIT_FAILURE);
}
for(;;) {
ssize_t len;
printf("speak ");
fflush(stdout);
/*gets(buff);*/ /* gets is BAD!!! */
if (fgets(buff, BUF_SIZE, stdin) == NULL) {
fprintf(stderr, "Failed to read input\n");
exit(EXIT_FAILURE);
}
len = write(s, buff, strlen(buff));
if (len == -1) {
perror("Failed to write\n");
exit(EXIT_FAILURE);
}
len = read(s, buff, BUF_SIZE);
/* read returns -1 on error. Don't want to index an array with -1! */
if (len < 0) {
fprintf(stderr, "Failed to read network data\n");
}
else if (len == 0) {
fprintf(stderr, "No response data read\n");
}
else {
buff[len] = '\0';
printf("server says : %s\n", buff);
}
}
return 0;
}
Namaste,
-Ray-