LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 06-19-2009, 11:28 AM   #1
goldeneagle1234
LQ Newbie
 
Registered: Sep 2008
Posts: 10

Rep: Reputation: 0
Arrow I/O problem in socket programming


the program below is to print the IP address of the connected client on server side
well guys my problem is that when i use cout in for(; loop no result is displayed each time a client connects....
Only when i use write() func result is displayed each time a client is connected...

above problem persists only when cout or any of C++ stream object is used in a loop like below
plzzz help

Code:
#include<sys/socket.h>
#include<errno.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<iostream>
#define TIME_TO_REFRESH_LOG 60
using namespace std;
int main()
{
	int listenfd=socket(AF_INET,SOCK_STREAM,0);
	if(listenfd==-1)
	{
		perror("server socket");
		exit(1);
	}
	struct sockaddr_in servaddr;
	bzero(&servaddr,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	servaddr.sin_port=htons(9001);
	servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
	if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1)
	{
		perror("server bind");
		exit(1);
	}
	if(listen(listenfd,128)==-1)
	{
		perror("server listen");
		exit(1);
	}
	int connfd;
	struct sockaddr_in cliaddr;
	socklen_t clilen;
	for(;;)
	{
		bzero(&cliaddr,sizeof(cliaddr));
		clilen=sizeof(cliaddr);
		connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen);		
		if(connfd==-1)
		{
			perror("server accept");	
			//continue;
		}
		char buf[100];
		inet_ntop(AF_INET,&cliaddr.sin_addr,buf,sizeof(buf));
		cout<<buf;//not working in loop
		write(1,buf,100);//working
		close(connfd);
	}	
	return 0;
}
 
Old 06-19-2009, 12:47 PM   #2
bannock
LQ Newbie
 
Registered: Jun 2009
Location: Toronto
Distribution: Ubuntu, Fedora
Posts: 24

Rep: Reputation: 18
Probably cout isn't getting flushed, try this:

Code:
cout << buff << endl;
or
Code:
cout << buff;
flush(cout);
 
Old 06-19-2009, 01:06 PM   #3
goldeneagle1234
LQ Newbie
 
Registered: Sep 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Smile thanks

thanks man,
the problem had almost bewildered me
 
  


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
I/O problem in socket programming goldeneagle1234 Linux - Networking 1 06-19-2009 12:24 PM
Socket Programming Problem htabesh Programming 8 06-04-2009 01:28 PM
Socket Programming problem lucky6969b Programming 4 12-27-2005 01:44 AM
socket programming problem...??? arunka Programming 4 10-11-2005 09:08 AM
Socket programming problem krivi Programming 3 01-14-2002 03:04 AM


All times are GMT -5. The time now is 03:10 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration