multiuser chat server closes when one client closes. code included


 
Thread Tools Search this Thread
Top Forums Programming multiuser chat server closes when one client closes. code included
# 1  
Old 01-06-2004
Question multiuser chat server closes when one client closes. code included

I have been trying to write a very basic chat program but at the moment I am having problems getting it to be multiuser as it closes all connections when one client shutsdown. I have also been having problems trying to get the program to display a list of usernames to the clients. I have tried setting the msg_type to '2' so that it will process the usernames list with a different function but the message type appears to be 512 on the server program and 7 on the client. I would be very grateful if someone could help me as I am new to this. Thanks.

Code for Server:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>

#define MAX_CLIENTS 64

struct sockaddr_in sv_addr;
int addr_len;
struct sockaddr addr;
struct sockaddr_in *ptr;
int cl_addr_len;
struct sockaddr_in cl_addr;
char in_buf[100], out_buf[100];
char names [10][30];
char message[150];
int num_names = 0;

typedef struct cl_info
{
char cl_id[30];
int cl_status;
}cl_info;

cl_info clients[MAX_CLIENTS];

int main(int argc, char **argv)
{
int newsockfd;
int sockfd;
int nbytes;
int max_fd;
int fd;
short func_num;

fd_set test_set, ready_set;

sockfd = socket(AF_INET, SOCK_STREAM, 0);
sv_addr.sin_family - AF_INET;
sv_addr.sin_port = 0; /*choose an unused port at random */
sv_addr.sin_addr.s_addr = INADDR_ANY; /* auto fill with my IP address */
bind(sockfd, &sv_addr, sizeof(sv_addr));
addr_len = sizeof(addr);
getsockname(sockfd, &addr, &addr_len);
ptr = (struct sockaddr_in *)&addr;
printf("\nServer on port# %d\n", ptr->sin_port);
listen(sockfd, 5);
max_fd = sockfd;
FD_ZERO(&test_set); /*clear the listening socket */
FD_SET(sockfd, &test_set);

while(1)
{
memcpy(&ready_set, &test_set, sizeof(test_set));
select(max_fd+1, &ready_set, NULL, NULL, NULL);
if(FD_ISSET(sockfd, &ready_set)) /*if listening sock has data its a call from new client */
{
/*create a new socket */
cl_addr_len = sizeof(cl_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cl_addr, &cl_addr_len);
printf("\nA new connection has been made to socket %d\n", newsockfd);
FD_SET(newsockfd, &test_set); /* add new socket to list */
if(newsockfd > max_fd)
max_fd = newsockfd; /*update max_fd with new socket file discriptor */
} /*end if*/
for(fd=0; fd <= max_fd; fd++) /*loop through connected socket file discriptors */
{
if((fd !=sockfd) && FD_ISSET(fd, &ready_set))/*bypass listening sock if sock has data*/
if(process_call(fd, max_fd) == 0) /*if socket is closed */
{
printf("\nThe listening socket is now closed\n");
close(newsockfd);
printf("\nClient on socket %d has left the room\n", sockfd);
FD_CLR(fd, &test_set); /*remove socket from list */
/*decrement user count */
} /*end if */
} /* end for loop */
} /*end while loop */
return 0;
} /*end main */
int add_name(int sockfd)
{
int nbytes;
short name_len;
short n_name_len;

printf("\nAdding name .....\n");
nbytes = read(sockfd, (char *)&name_len, sizeof(short));
name_len = ntohs(name_len); /*convert name_len from network to host short */
memset(clients[sockfd].cl_id, 0, 30); /*set all bytes to zero */
nbytes = read(sockfd, clients[sockfd].cl_id, name_len);
num_names ++; /*increment user count */
printf("\n%s has joined the chat room\n", clients[sockfd].cl_id);

/*need to broadcast user has joined plus user id to connected clients here */

} /*end add_name function */
int how_many(int sockfd, int max)
{
int sock;
short user_len;
char username[30];

for(sock = 4; sock <= max; sock++)
{
strcpy(username, clients[sock].cl_id);

user_len = htons(strlen(username));
write(sockfd, (char *)&user_len, sizeof(short));
write(sockfd, username, strlen(username));
send_user_list(sockfd, username);
}
} /*end how_many function */
int send_user_list(int sockfd, char *users)
{
short msg_type;
short user_len;

msg_type = htons(2);
printf("msg_type is : %u\n", msg_type);
write(sockfd, &msg_type, sizeof(short));

user_len = htons(strlen(users));
write(sockfd, (char *)&user_len, sizeof(short));
write(sockfd, users, strlen(users));
}
int find_name(int sockfd, int max)
{
char search_name[30];
int sock;
int found = 0;
short len, name_len;
char sender[30];
short sender_len;
char msg[150];
short msg_len;

memset(search_name, 0, 30); /*set all bytes to zero */
printf("\nReceiving userid......\n");
read(sockfd, (char *)&name_len, sizeof(short));
len = ntohs(name_len);
read(sockfd, search_name, len);
read(sockfd, &msg_len, sizeof(short));
msg_len = ntohs(msg_len);
read(sockfd, msg, msg_len);
msg[msg_len] = '\0';

strcpy(sender, clients[sockfd].cl_id);

for(sock = 4; sock <= max; sock++)
{
printf(" %d is held in max\n", max);
printf("\ncomparing search request for %s with %s username\n",
search_name, clients[sock].cl_id);
if(strcmp(search_name, clients[sock].cl_id) ==0)
{
printf("\nuserid found\n");
found = 1;
send_async_msg(sock, msg,sender);
}/*end if */
} /*end for loop */

if(! found)
{
printf("\nuserid not found\n");
} /*end if */
}/*end find_name function */
int process_call(int newsockfd, int max_fd)
{
int nbytes;
short func_num;

nbytes = read(newsockfd, (char *)&func_num, sizeof(short));
if(nbytes ==0)
return 0;
if (nbytes != sizeof(short))
{
perror("\nUnknown menu option!\n");
exit(1);
} /* end if */

func_num = ntohs(func_num);
switch(func_num)
{
case(1):
add_name(newsockfd);
break;
case(2):
how_many(newsockfd, max_fd);
break;
case(3):
find_name(newsockfd, max_fd);
break;
case(4):
send_global_msg(newsockfd, max_fd);
break;
case(5):
printf("\nShutting down Server...\n");
exit(0);
default:
printf("\nUnknown function number!\n");
break;
} /*end switch */
} /*end process_call function */
int send_async_msg(int sockfd, char * msg, char *sender)
{
short msg_type;
short msg_len;
short sender_len;

msg_type = htons(1);
printf("msg_type is : %u\n", msg_type);
write(sockfd, &msg_type, sizeof(short));
msg_len = htons(strlen(msg));
write(sockfd, (char *)&msg_len, sizeof(short));
write(sockfd, msg, strlen(msg));

sender_len = htons(strlen(sender));
write(sockfd, (char *)&sender_len, sizeof(short));
write(sockfd, sender, strlen(sender));
}/* end of send_async_msg function */
int send_global_msg(int sockfd, int max_fd)
{
int nbytes;
int sock;
short len;
short msg_type;
char msg[150];
char sender[30];
short sender_len;

strcpy(sender, clients[sockfd].cl_id);

msg_type = 1;
memset(in_buf, 0, sizeof(in_buf)); /*set all to zero */
nbytes = read(sockfd, &len, sizeof(short));
len = ntohs(len);
read(sockfd, msg, len);
message[len] = '\0';
for(sock = 4; sock <= max_fd; sock++)
{
send_async_msg(sock, msg, sender);
} /*end for loop */
} /* end of send_global_msg function */

CODE for Client: is in attached txt file
These 2 Users Gave Thanks to dooker For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Telnet connection closes before i execute GET command

I tried the below steps for telnet command from a remote server: 1. telnet myservice.com 443 2. GET / HTTP/1.0 3. Press enter key twice Please see the output below: bash-3.2$ hostname remoteserver1 bash-3.2$ telnet myservice.com 443 Trying 191.172.172.133... Connected to... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

New bash menu printing errors but closes too quickly

I am beginning to write a new version of the bash menu below. The previous version worked great and now when I start the updated bash it opens and a some lines print in the terminal and it closes quickly. I know there are errors but how can I see them or fix them, I tried set -x with the same... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Bash menu opens and closes

Ever since I added these two code blocks to my bash menu it just opens and closes right away. I use a batch file that worked fine until these codes were added and I am not sure what is wrong. Basically, what I am trying to do in the additional section is if the answer is "Y" then it goes back... (13 Replies)
Discussion started by: cmccabe
13 Replies

4. Solaris

sftp connection closes if idle for around 10minutes

does sftp connection closes by default if it stays idle for a prolonged time ? i have checked sshd_config files , there is no time out value set. but still the connection closes after certain period of time. Please help. verbose mode output : sftp> debug1: channel 0: free:... (10 Replies)
Discussion started by: chidori
10 Replies

5. AIX

SSH session closes after typing correct password

hi guys need some help. when ever i'm login ssh to aix server session always closed. when trying t0 type wrong password the session still continues, but we tried the correct password it automatically ends. what could be the problem to this please see .profile details ... (6 Replies)
Discussion started by: bocha
6 Replies

6. UNIX for Dummies Questions & Answers

xterm -e closes when script is done

I want to open multiple xterm windows, run different programs in each and have each xterm window stay open - to use as I wish. I have written a ksh script that uses "xterm -e scriptname" (multiple times). The xterm windows do open successfully. However, when "scriptname" completes, the xterm... (4 Replies)
Discussion started by: lazaret
4 Replies

7. Solaris

socket in listen state disappears/closes automatically

Hi, I am using solaris 10. I have opened a socket connection using java in solaris 10 operating system, the port went to LISTEN state and able to create new socket connection and the new connections went to ESTABLISHED state. If I issue the command "netstat -an | grep <<portnumber>>", I... (10 Replies)
Discussion started by: kumar3k
10 Replies

8. SuSE

NoMachine NX window closes after establishing connection

Hi, I am trying to use nomachine nx server and client. But somehow it doen't work. What happens is the following: 1.- Client starts up 2.- Client authenticates with Server 3.- The NoMachine window appears for 2-4 seconds 4.- The NoMachine window exists Somehow a "closeEvent" is sent.... (3 Replies)
Discussion started by: blackicecube
3 Replies

9. UNIX for Advanced & Expert Users

SSH closes connection when using arrow keys

Hi, I'm having a problem with my ssh link to various Sun servers running Solaris 9 and 10 from an Windows XP box running Cygwin/X. I am using ssh to connect to a number of different Sun servers. My problem is that when editing a remote shell command line with the arrow direction keys the... (0 Replies)
Discussion started by: stv_t
0 Replies

10. UNIX for Dummies Questions & Answers

Server/client chat

I want to make the following programm. Using the server/client model I want 2 client to connect to the server then the server sends back to the clients the ip address and a number of a poort in order to open a udp connection between clients without using the server? What I have done since now is... (2 Replies)
Discussion started by: kasma
2 Replies
Login or Register to Ask a Question