View Single Post
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

UDP-Sockets chat application question

 
0
  #1
Nov 16th, 2008
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.
  1. if (argc != 4)
  2. err_quit("usage: udpcli <IPaddress> <his-Port> <mine-Port>\n");
  3. sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
  4.  
  5. memset( &servaddr, 0, sizeof(servaddr) );
  6. servaddr.sin_family = AF_INET;
  7. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  8. servaddr.sin_port = htons(atoi( argv[3]) );
  9. //servaddr.sin_port = htons( SERVER_PORT );
  10. Bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr) );
  11.  
  12. printf("Peer-Server initiated...listening in %s and port %d\n\t waiting for clients\n",
  13. argv[1], ntohs(servaddr.sin_port));
-->> client part initialization
  1. memset( &hisaddr, 0, sizeof(hisaddr) );
  2. hisaddr.sin_family = AF_INET;
  3. hisaddr.sin_port = htons( atoi(argv[3]) );
  4. Inet_pton( AF_INET, argv[1], &hisaddr.sin_addr);
-->> fork so that the application will be able to send and receive messages.
  1. if((childpid = fork()) == 0)
  2. {
  3. //send message to peer
  4. peer_dg_send(stdin, sockfd, (struct sockaddr *) &hisaddr, sizeof(hisaddr));
  5. }
  6. else
  7. {
  8. //receive message from peer
  9. peer_dg_receive(sockfd, (struct sockaddr *) &hisaddr, sizeof(hisaddr) );
  10.  
  11. }

here are some extra functions:
  1. void peer_dg_receive(int sockfd, struct sockaddr * pcliaddr, socklen_t clilen)
  2. {
  3. int n;
  4. socklen_t len;
  5. char mesg[MAXLINE];
  6.  
  7. for(;;)
  8. {
  9. len = clilen;
  10. n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
  11.  
  12. mesg[n] = 0; /* null terminate */
  13. //Fputs(mesg, stdout);
  14. printf("Received: %s", mesg);
  15. }
  16. }
  17.  
  18. void peer_dg_send(FILE *fp, int sockfd, struct sockaddr *pservaddr, socklen_t servlen)
  19. {
  20. int n;
  21. char sendline[MAXLINE];
  22.  
  23. while (Fgets(sendline, MAXLINE, fp) != NULL)
  24. {
  25. Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
  26. }
  27.  
  28. }
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
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote