Chat Server


 
Thread Tools Search this Thread
Top Forums Programming Chat Server
# 1  
Old 10-23-2005
Chat Server

I had recently been in a computer/business class and decided to have some fun since I was way ahead of the class(JAVA). So I first started off by creating a VB Chat Server and Client I had many features like kick, admin login, bans, bad word filters, private messages, etc. Anyway I noticed a bit of lag in the server so I decided to remake the server in C and keep the client in VB. I have been making the server and I seem to be going fine until I hit something that would compromise the whole project, a function called sendtoall, that sends to all the open sockets.

Code:
#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>


int socks[500], ind;
void dostuff(int);
void error(char *msg){
perror(msg);
exit(1);
}

int main(int argc, char *argv[]){
int sockfd, newsockfd, portno, clilen, pid;
struct sockaddr_in serv_addr, cli_addr;

if (argc <2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
ind = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
   error("ERROR opening socket");
bzero((char * ) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)
   error("ERROR on binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
while(1){
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd < 0) 
   error("ERROR on accept");
socks[ind]=newsockfd;
ind = ind + 1;
pid = fork();
if(pid<0)
   error("ERROR on fork");
if (pid==0) {
close(sockfd);
dostuff(newsockfd);
exit(0);
}
else close(newsockfd);
}
return 0;
}



void dostuff (int sock)
{
   int n;
   char buffer[256];   
   bzero(buffer,256);
   n = read(sock,buffer,255);
   if (n < 0) error("ERROR reading from socket");
   sendtoall(buffer);
   printf("Here is the message: %s\n",buffer);
   n = write(sock,"I got your message",18);
   if (n < 0) error("ERROR writing to socket");
}


void sendtoall(char* msg){
int i, n;
i = 0;
printf("Message:%s \n", msg);
printf("Sent to all..or not\n");
printf("I:%d", i);
for(i=0;500<=i;i++){
printf("I:%d", i);
n = write(socks[i],msg,sizeof(msg));
if (n < 0) error("ERROR writing to socket"); 
}
}


void parsemsg(char *msg){
}

This is my code so far, yet the problem is a logical error. When it runs sendtoall if displays everything outside of the for loop, yet it never executes any commands inside it. Please help me.

Thanks in advance.

, Lazyshot
# 2  
Old 10-23-2005
Quote:
Originally Posted by Lazyshot
Code:
for(i=0;500<=i;i++){

When i = 0, 500 <= i is false, so the loop will break without running anything inside.
# 3  
Old 10-23-2005
oops, stupid mistake
# 4  
Old 10-23-2005
i now have another problem being that it won't send the message to all the sockets.....
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Help with server client chat system

request create a chat system in linux where a user type smth and all the other users connected to server get the message.then a user have to create join leave or delete a channel of chat if he created it.i did the server and the client but i dont know how to implemt the chat rooms.i was ... (1 Reply)
Discussion started by: demonmind
1 Replies

2. UNIX for Dummies Questions & Answers

Server-client chat with a bit more

The task is to create a server client chat that contains a few basic safeguards against floods etc and which is capable of issuing at least one or two commands on the client computer. Working samples of such arrangements abound but freeware/shareware samples are not readily available. A catch in... (3 Replies)
Discussion started by: Bobby
3 Replies

3. 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

4. Solaris

Looking for truly freeware chat server for solaris

I am running solaris 9 on x86 server. I am looking to find an actual freeware chat server that I can install and let 10-20 users on my network use... I was directed towards "dbabble" which is great but not freeware and trial expires after 30 days.... Anyone know a reasonably decent freeware... (1 Reply)
Discussion started by: frustrated1
1 Replies

5. Programming

chat server project

Hi All i am a developing a chat server using C in Linux.Now i am just a beginner in Networking so i need help from someone in the project so please help me and reply me fast at email address deleted thanking you all Regards (4 Replies)
Discussion started by: arjunjag
4 Replies

6. Programming

Chat client-server program

Good day everyone, I'm doing a chat client-server program:server is to receive messages from clients through a TCP port and multicast them back to all clients through a UDP port. This is my client program. I'd not know why it just sends and receives msg from server once, then it stops. Is... (1 Reply)
Discussion started by: powermind
1 Replies
Login or Register to Ask a Question