LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-30-2008, 10:34 AM   #1
lrios
Member
 
Registered: Mar 2008
Location: Arg
Distribution: Cent0S
Posts: 48

Rep: Reputation: 15
write and read socket


Hi all,
In some situations my program respond, but in other situations the program don't respond for long time period, because client received more data. I am using bzero() and fflush() too.

What's wrong with the code ??

server
Code:
#define MAXL_NICK 256
char buffer_s[MAXL_NICK];		
char *welcome1 = "Write your nickname";
char *welcome2 = "Doing procedures . . .";
char *welcome3 = "please wait 5 second . . . ";

if((newsocket = accept(listenfd, (struct sockaddr *) &struc_server, &t_server)) < 0) { 

	         printf("error en el accept()\n");
		exit(-1);
		}

		else {			
	fflush(stdout);
	write(newsocket, welcome1, strlen(welcome1));
	bzero(buffer_s, MAXL_NICK);
 	read(newsocket, users[n_user].username, sizeof(users[n_user].username));
	printf("intented connected %s\n", users[n_user].username);
 	write(newsocket, welcome2, strlen (welcome2));
	write(newsocket, welcome3, strlen (welcome3));
	printf("connected %s \n", users[n_user].username);
	fflush(stdout);
	close(newsocket);
}}

}


client
Code:
#define MAXL_NICK 256
char buffer_c[MAXL_NICK];
	fflush(stdout);

	bzero(buffer_c, MAXL_NICK);
	read(sockfd, buffer_c, MAXL_NICK-1);
	printf("%s\n", buffer_c);
	fgets(buffer_c, sizeof(buffer_c), stdin);
	size_nick = strlen (buffer_c); 
	write(sockfd, buffer_c, size_nick-1);
	printf("sending to server the nick . . . %s", buffer_c);
	bzero(buffer_c, MAXL_NICK);
	read(sockfd, buffer_c, MAXL_NICK-1);
	printf("%s\n", buffer_c);
	bzero(buffer_c, MAXL_NICK);
	read(sockfd, buffer_c, MAXL_NICK-1);
	printf("%s\n", buffer_c);
	fflush(stdout);	

	close(sockfd);
	exit (0);

}

Can someone tell me what's wrong with this code
Thanx

Last edited by lrios; 05-01-2008 at 09:44 AM.
 
Old 05-01-2008, 01:19 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Your server reads data the size of its entire username buffer, but the client only writes the amount of data typed in. You should write the entire buffer; not just strlen. Same with the server writes: they both use strlen but the client reads ask for a greater amount of data.
ta0kira

PS setlinebuf(stdout); will save you all of those fflush(stdout);s.

PPS If you don't want to write the entire buffer size, you could use fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK); to set non-blocking mode, then use select right before your reads. This will allow your reads to read only the amount of data that was written instead of blocking for the entire requested size.

Last edited by ta0kira; 05-01-2008 at 01:27 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Read/write locks using semaphores with write priority arunka Programming 1 06-28-2007 07:04 PM
socket programming function to read/write integers? ashlesha Linux - Networking 4 04-25-2007 10:45 PM
Why can I not write to a SAMBA share when read/write is enabled? eric m Linux - General 4 08-21-2006 09:22 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Open office read only, K-write read/write mode lwtvh Linux - Newbie 1 07-19-2003 11:33 AM

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

All times are GMT -5. The time now is 04:23 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