LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Closed Thread
 
Search this Thread
Old 06-19-2009, 10:26 AM   #1
goldeneagle1234
LQ Newbie
 
Registered: Sep 2008
Posts: 10

Rep: Reputation: 0
Unhappy 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, 11:24 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,369
Blog Entries: 54

Rep: Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870Reputation: 2870
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread should be closed because it is a duplicate of http://www.linuxquestions.org/questi...amming-734204/.
 
  


Closed Thread


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
Socket Programming Problem htabesh Programming 8 06-04-2009 12:28 PM
socket programming problem muskvar Programming 14 10-16-2007 09:35 AM
Socket Programming problem lucky6969b Programming 4 12-27-2005 12:44 AM
socket programming problem...??? arunka Programming 4 10-11-2005 08:08 AM
Socket programming problem krivi Programming 3 01-14-2002 02:04 AM


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

Main Menu
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