![]() |
| ||
| UDP-Sockets chat application question Hi to everyone! I want to create a simple chat application using udp sockets. I want to have 2 applications that will be able to talk to each other.In particular app1 will send msgs to p2 and p2 will display them in stdout. Then maybe p2 sends a msg to p1... etc.. for the initialization part i have the following: -->> server part initialization. if (argc != 4)-->> client part initialization memset( &hisaddr, 0, sizeof(hisaddr) );-->> fork so that the application will be able to send and receive messages. if((childpid = fork()) == 0) here are some extra functions: void peer_dg_receive(int sockfd, struct sockaddr * pcliaddr, socklen_t clilen)also the functions with the first letter capitalized are from stevens's "unix network programming". I have to questions in the above scheme: First, is this the "best" way to tackle the chat problem? or am i missing something in the big picture Second, it fails to work in the following sense: each time i try to send something from app1 to app2. app2 never receives the msg... but instead app1 does! why? and how can i fix it... any ideas {even if not complete} are welcome! thanks for your help, nicolas |
| ||
| Re: UDP-Sockets chat application question 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. write two separate programs; the server and the client. which can be run on different machines. > each time i try to send something from app1 to app2. > app2 never receives the msg... but instead app1 does! why? i've only had a cursory look, but: the forked child process has its own copy of the parent's descriptors. but these descriptors reference the same underlying objects as the parent. both the parent and child processes are using the same socket, same stdin etc. |
| ||
| Re: UDP-Sockets chat application question Quote:
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: +---------------+ +---------------+ Where each peer will fork and run like this if((childpid = fork()) == 0) 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: +-------------------------------+ +-----------------------+ 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: +-------------------------------+ any ideas are welcome, even if not complete.... -nicolas |
| ||
| Re: UDP-Sockets chat application question > i read somewhere that sockets are full duplex, so initially i tried to build an architecture like this: a socket is *one* end of a two-way communications link. a BSD socket maintains a separate send buffer and a receive buffer, and is full-duplex (you can both send and receive data on it). to send or receive data, you need two sockets (one at either end). in your first example, you are trying to use the *same* socket at both ends. create *two* sockets, one for the client and another for the server. (eg. socketpair does this for UNIX sockets. http://www.freebsd.org/cgi/man.cgi?query=socketpair) after forking, close the client socket in the server, and the server socket in the client. and things should be ok. +---------------+ +---------------+ > Could this diversion be "easily" done, by using ncurses yes. |
| ||||||
| Re: UDP-Sockets chat application question Thanks for your answers vijayan, Quote:
but: >http://en.wikipedia.org/wiki/Duplex_(telecommunications) Quote:
Quote:
Quote:
Quote:
Quote:
Thanks again for your effort and your time, Although i have a found a kind of solution to this problem {the one i posted in my second post with the use of 2 sockets per peer}, i keep asking because either i don't understand the concept of a socket, port,etc,... or i am doing something terribly wrong... with regards, nicolas PS: as a side effect question what is the relation between a socket and a port. I mean a socket is a special kind of file descriptor, since when i use file descriptors i don't use ports, why we have to use ports when we use a socket? |
| ||
| Re: UDP-Sockets chat application question > what is the relation between a socket and a port. > I mean a socket is a special kind of file descriptor > when i use file descriptors i don't use ports > why we have to use ports when we use a socket? a socket has no relation to a port. a socket is created with the socket function has no name. this is no different from other file descriptors. a file descriptor also has no name. for example, stdin, stdout, stderr are file descriptors. you use the open function to create a descriptor, the name that you give is the name of a file in the filesystem (which has an existence independant of file descriptors). a remote process has no way to refer to a socket until a name (an address) is bound to it. an address is given to a socket via the bind function call. the address serves as a unique name using which a socket can be referred to. In the UNIX domain, an address is simply a filesystem path. eg. sockaddr_un addr;The file name referred to is created as a socket in the system file name space. (write permission in the directory is required). these can be deleted with unlink(). in the Internet domain, the name is a tuple consisting of the IP address and a port number. a connection is a *unique* ordered tuple ( protocol, local ip address, local port, remote ip address, remote port). ports are an artifact of the addressing scheme to facilitate distinguishing between different connections on the same host (by composing unique tuples - at least the port is different - for each connection). the same socket can be used to send and receive data at the same time. the following example would work, though we use the same socket: #include <sys/types.h> ps1: run with peer address "127.0.0.1" (localhost) for testing. ps2: if you have follow-up questions that you want me to answer, do not post them in daniweb. i keep away from web sites that behave as if Internet Explorer is the only browser that i should use. (this post is from IE/Wine). you could use http://forums.devx.com/forumdisplay.php?s=&forumid=110 instead, which is the (only) other C++ student's forum that i look at on a somewhat regular basis. |
| All times are GMT -4. The time now is 1:37 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC