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 -
sockfd = socket(AF_INET,SOCK_STREAM,0);
-
portno = atoi(argv[1]);
-
serv_addr.sin_family = AF_INET;
-
serv_addr.sin_addr.s_addr = INADDR_ANY;
-
serv_addr.sin_port = htons(portno);
-
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr);
-
listen(sockfd,5);
-
neewsockfd = accept(sockfd,(struct sockaddr*)&cli_addr,&clien);
-
Then I started reading the incoming data in a seperate thread like this - ...
-
while(1)
-
{
-
bzero(buffer,256);
-
n= read(newsockfd,buffer,255);
-
if(n>0)
-
printf("%s",buffer);
-
}
-
And in the main routine after the listneing code I added the socket data sending part like this. - ....
-
while(1)
-
{
-
bzero(buffer,256);
-
getline(&buffer,&t,stdin);
-
n=send(newsockfd,buffer,strlen(buffer),MSG_EOR);
-
}
-
And for the client part I had connected to the server like this. - ....
-
sockfd = socket(AF_INET,SOCK_STREAM,0);
-
portno = atoi(argv[1]);
-
serv_addr.sin_family = AF_INET;
-
inet_pton(AfF_INET,hostip,&serv_addr.sin_addr.s_addr)
-
serv_addr.sin_port = htons(portno);
-
connect(sockfd,serv_addr,sizeof(serv_addr));
-
And then I duped the stdin, stdout and stderr to sockfd descriptor for redirecting the console as follows. - ....
-
dup2(sockfd,STDIN_FILENO);
-
dup2(sockfd,STDOUT_FILENO);
-
dup2(sockfd,STDERR_FILENO);
-
-
close(sockfd);
-
-
And finally I have something like this in client to test the console redirecting - ...
-
while(1)
-
{
-
bzero(mystring,256);
-
i = getline(&string,&t,stdin);
-
printf("Input:%s-%d\n",mystring,i);
-
}
-
-
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.
0 1402 Sign in to post your reply or Sign up for a free account.
Similar topics
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";
...
|
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...
|
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:
|
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...
|
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");
|
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...
|
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||...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |