LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2018, 08:06 AM   #1
t_shit
LQ Newbie
 
Registered: Jun 2011
Posts: 15

Rep: Reputation: Disabled
Using select() to make a simple chat app


I've been learning about sockets and also learned about the select() system call which makes one able to accept and handle multiple connections. On the process of making a simple chat (in C) I noticed that every time one user of the chat writes something, the app prints everything the user writes plus what the user wrote. Like this:

[user1] hello

[user2] hello
[user2] good morning

[user1] hello
[user1] have a nice day

[user2] hello (this was already printed)
[user2] good morning (this too was already printed)
[user2] thank you

[user1] hello (already printed)
[user1] have a nice day (already printed)
[user1] you're welcome

Is this a consequence of using the select function?

Here's the source code of the server:
Code:
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <time.h>

struct peer {
	char * nick;
	int socketfd;

};
int main(int argc, char *argv[]){
	struct peer peers[100];
	int sockfd,newsockfd,port;
	socklen_t clilen;
	char buffer[255];
	struct sockaddr_in serv_addr,cli_addr;
	int n;
	sockfd=socket(AF_INET,SOCK_STREAM,0);
	
     	bzero((char *) &serv_addr, sizeof(serv_addr));	
	port=atoi(argv[1]);
	
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=INADDR_ANY;
	serv_addr.sin_port=htons(port);
	
	if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0){ 
        	
	}
	listen(sockfd,5);
	fd_set rfds,afds;
	int size,npeer=0,nfds=getdtablesize();
	FD_ZERO(&afds);
	FD_SET(sockfd,&afds);int status;
	for(;;){	
		memcpy(&rfds,&afds,sizeof(rfds));
		status=select(nfds,&rfds,(fd_set *)0,(fd_set *)0,(struct timeval *) 0);
		if(status < 0 )
			perror("select: ");
			

			
		if(FD_ISSET(sockfd,&rfds)) {
			clilen=sizeof(cli_addr);
	
			newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
			FD_SET(newsockfd,&afds);
			
		}	
		
		    		
		for(int fd=0;fd<nfds;fd++){
			if(fd != sockfd && FD_ISSET(fd,&rfds)){
					
					
				bzero(buffer,255);
				n=read(fd,buffer,255);
				for(int i=0;i<nfds;i++) {
					if(i != sockfd && FD_ISSET(i,&afds)){		
						write(i,buffer,255);
							
				       	}
				}			
			       printf("%s\n",buffer);
			}	
		}
	}

Last edited by t_shit; 07-26-2018 at 03:09 AM. Reason: clarity
 
Old 07-25-2018, 11:35 AM   #2
t_shit
LQ Newbie
 
Registered: Jun 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
the problem doesn't seem to be with select but with the socket. every read that is made on the socket just returns data that was already received;
 
Old 07-25-2018, 02:56 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You shouldn’t ignore the return-value of 'read': it might be positive, zero or negative, all of those have importance.
 
Old 07-25-2018, 04:04 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@OP: To preserve formatting place your code snippets inside [CODE]...[/CODE] tags. You may type those yourself or click the "#" button in the edit controls.
 
Old 07-26-2018, 05:34 AM   #5
t_shit
LQ Newbie
 
Registered: Jun 2011
Posts: 15

Original Poster
Rep: Reputation: Disabled
I figured out what was wrong. The problem was with client. I didn't call bzero() every time i had to use the string variable I was going use to send text so the string was allways growing. my bad
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleted applet for UbuntuOne / Chat, and power down app. is MIA??? buccaneere Linux - Desktop 1 12-07-2012 02:55 PM
I write chat program with select but how to use nchurses?! zeinab Linux - Newbie 2 09-08-2008 03:33 AM
win compatible video chat app in kde? gub Linux - Software 5 05-09-2006 09:00 AM
another simple question about select() djgerbavore Programming 10 04-15-2006 03:29 PM
Using Select to make a simple directory menu why doesn't it work ctrimble Programming 5 06-07-2004 01:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration