473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

getline() function behaves strangly

I am trying to emulate telnet functionality by writing a socket listener program, and an application redirecting its console to the listener over sockets.

On the server side I opened a socket on my local ip, a defined port, and started listening on it like this
Expand|Select|Wrap|Line Numbers
  1. sockfd = socket(AF_INET,SOCK_STREAM,0);
  2. portno = atoi(argv[1]);
  3. serv_addr.sin_family = AF_INET;
  4. serv_addr.sin_addr.s_addr = INADDR_ANY;
  5. serv_addr.sin_port = htons(portno);
  6. bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr);
  7. listen(sockfd,5);
  8. neewsockfd = accept(sockfd,(struct sockaddr*)&cli_addr,&clien);
  9.  
Then I started reading the incoming data in a seperate thread like this
Expand|Select|Wrap|Line Numbers
  1.  ...
  2. while(1)
  3. {
  4.   bzero(buffer,256);
  5.   n= read(newsockfd,buffer,255);
  6.   if(n>0)
  7.      printf("%s",buffer);
  8. }  
  9.  
And in the main routine after the listneing code I added the socket data sending part like this.
Expand|Select|Wrap|Line Numbers
  1.  ....
  2. while(1)
  3. {
  4.    bzero(buffer,256);
  5.    getline(&buffer,&t,stdin);
  6.    n=send(newsockfd,buffer,strlen(buffer),MSG_EOR);
  7. }
  8.  
And for the client part I had connected to the server like this.
Expand|Select|Wrap|Line Numbers
  1.  ....
  2. sockfd = socket(AF_INET,SOCK_STREAM,0);
  3. portno = atoi(argv[1]);
  4. serv_addr.sin_family = AF_INET;
  5. inet_pton(AfF_INET,hostip,&serv_addr.sin_addr.s_addr)
  6. serv_addr.sin_port = htons(portno);
  7. connect(sockfd,serv_addr,sizeof(serv_addr));
  8.  
And then I duped the stdin, stdout and stderr to sockfd descriptor for redirecting the console as follows.
Expand|Select|Wrap|Line Numbers
  1.  ....
  2.  dup2(sockfd,STDIN_FILENO);
  3.  dup2(sockfd,STDOUT_FILENO);
  4.  dup2(sockfd,STDERR_FILENO);
  5.  
  6.  close(sockfd);
  7.  
  8.  
And finally I have something like this in client to test the console redirecting
Expand|Select|Wrap|Line Numbers
  1.  ...
  2. while(1)
  3. {
  4.    bzero(mystring,256);
  5.    i = getline(&string,&t,stdin);
  6.    printf("Input:%s-%d\n",mystring,i);
  7. }
  8.  
  9.  
In the above code I expect the getline to be blocked till I enter some characters on the server application console, but for my agony it comes out with i value 0 repeatedly.

Any idea why this behavior.
Aug 3 '11 #1
0 1402

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Koyan | last post by:
Hallo everbody, I am using the following function: function MultimediaOpenWnd (ids,action1,captions,wd,ht) { var features = "width=" + wd + ",height=" + ht + "scrollbars=yes,menubar=no"; ...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
4
by: Tcc | last post by:
Hi all, I have a question about using the function getline().. e.g. I have defined a structure: struct s { string *a; Queue *b; }; then in my main function:
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
1
by: l46kok | last post by:
Just wondering, for character of arrays, you can set the delimiter manually like cin.getline(character array, size, delimeter) But for the STL string, apparently, you have to use the following...
6
by: ankit.kumar.agarwal | last post by:
I am facing a problem with getline I am reading a text file with a getline function the lines can have '|' as separator. everything works OK but in case if i have 2 delimitors in file '234||...
8
by: sabby | last post by:
I want to use the getline() so that i can enter a entire name in on line. (with spaces) The prob is that i am initializing the variable as "N/A" and saving it to a text file. it is declared as a...
19
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi... I have a php script which has a function call and function returns a value. But when i open it in browser and refresh the page the return value will get added to the old value. Code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.