Originally Posted by
vijayan121
First, is this the "best" way to tackle the chat problem?
no. it is unlikely that someone would want to set up a chat between two processes on the same machine, sharing stdin and stdout between them.
sorry i didn't express myself correctly.
University assignment dictates that we do a chat "system" with 2 udp connected peers. In this sense we can't have
a single server, but a lot of peers. So i figured that each peer will be both a server for the other peer,
and a client inorder to talk to some other peer.
i read somewhere that sockets are full duplex, so initially i tried to build an architecture like this:
+---------------+ +---------------+
| | | |
| peer1 | | peer2 |
| |port1 port2| |
| sock----|---------------|----sock |
| | | |
| | | |
+---------------+ +---------------+
Where each peer will fork and run like this
if((childpid = fork()) == 0)
{
//sending messages to other peer
}
else
{
//receiving message from other peer
}
Unfortunatelly this didn't work. The reason was that no message ever reached the other peer. i.e. if i tried to send
from peer1 to peer2, then peer1 sent the message and peer1-child received it.
So one question is:
is it possible to have a full duplex communication using only one process?
If yes, how?
If not, why?
So i decided to change the architecture and create 2 sockets. The scheme i created resembled this:
+-------------------------------+ +-----------------------+
| | | |
| peer1 | | peer2 |
| |port1 port2| |
| listening socket -------|---------------|----sending socket |
| | | |
| | | |
| |port2 port1| |
| sending socket----------|---------------|----listening socket |
| | | |
| | | |
+-------------------------------+ +-----------------------+
this way when the process forks.The child is responsible for sending and the parent for listening.
With this scheme everything works great! There is only one small problem, concerning the peer as whole.
PeerX process runs on a terminal, so the terminal is used for stdout {receiving messages}
and stdin {writing messages}. So there are problems while you are writing a message and at the same time
you receive one.
In this point i have to more questions:
1) Is there anyway to divert stdin {or stdout} to another terminal. I know that i can divert it to a file
but because i want live communication, it isn't convienient...
2) Could this diversion be "easily" done, by using ncurses and creating something like this:
+-------------------------------+
| |
| |
| stdout |
| {messages received} |
| |
+-------------------------------+
| |
| |
| stdin |
| {messages i type} |
| |
+-------------------------------+
any ideas are welcome, even if not complete....
-nicolas