The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Server client program pip3r High Level Programming 9 04-17-2008 01:15 AM
Client and Server program gen by Makefile wongalan48 High Level Programming 0 03-05-2007 01:09 PM
Client - server program mathu High Level Programming 4 09-17-2006 12:56 PM
program to transfer a file from client machine to server nathgopi214 High Level Programming 3 07-04-2006 06:16 AM
multiuser chat server closes when one client closes. code included dooker High Level Programming 0 01-06-2004 12:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-04-2006
powermind powermind is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 3
Chat client-server program

Good day everyone,

I'm doing a chat client-server program:server is to receive messages from clients through a TCP port and multicast them back to all clients through a UDP port.

This is my client program. I'd not know why it just sends and receives msg from server once, then it stops.

Is there anything wrong in my code?

Please help me. I'd appreciate it.


Code:
//Program client_distri.c.
//Get  and display distributed message 
#include <sys/types.h>   // for type  
#include <sys/socket.h>  // for socket API  
#include <netinet/in.h>  // for address  
#include <arpa/inet.h>   // for sockaddr_in 
#include <stdio.h>       // for printf() 
#include <stdlib.h>      // for atoi() 
#include <string.h>      // for strlen() 
#include <unistd.h>      // for close() 
////////////////////////////////////////////
char *multiHostAddress = "239.255.10.10";

void getEcho (int sock) 
{
	struct sockaddr_in read_EP; 
 	int stop = -1;
 	char *msg;

	printf ("Waiting for message... \n");
	while (stop < 0) 
	{     
	   msg = (char *)recvfromDST (&read_EP, sock);
	   if (msg != NULL) 
		printf ("%s==>%s\n",inet_ntoa(read_EP.sin_addr),msg);
	   else 
		stop = 0;
	}
}
////////////////////////////////////////////
int main(int argc, char **argv) 
{
  int sockMulti;           
  int readPort;  
  struct sockaddr_in EP_multi; 
  struct ip_mreq request; 
  unsigned char TTL=3;   

//-------------
   int MC_socket;
   struct sockaddr_in server_EP, sender_EP;
   char  msg [60];
   char *readmsg;
   int port;
   int stop = 0;
//--------------
	

//--------------
	if (argc != 4)
	{
	  	IO_error(1,"Need host, send port and receive port.(e.g localhost 9000 9001)");
	} 
//--------------

	else 
	{

//-----------------------------
//Send msg to server
		port = atoi (argv[2]);
	  	printf ("TCP Client to send message to Server %s at Port %d \n", argv[1], port);
   	 	setHostByName (&server_EP,  argv[1],  port);
  	  	MC_socket= sockTCP_create();
  	  	sock_connect(MC_socket,  &server_EP);
		
	  	while (stop == 0) 
		{ 			
	
	  		printf ("Enter message -without space- to send or 'QUIT'  \n");
	        	printf ("or 'SerEnd$$' to stop server:  ");
			//Read msg from user
	  		scanf ("%s", msg);
			//Send msg to server
	  		sendtoDST (sender_EP, MC_socket, msg);
	  		printf ("Message %s sent \n", msg);
			stop = (strncmp (msg, "QUIT", 4) == 0);
                	if (stop == 0) 
			{
		   		stop = (strncmp (msg, "SerEnd$$", 8) == 0);
               	}

//Receive msg from server

	  		readPort = atoi (argv[3]);
	  		printf ("Multicasting receiver at ports %d \n", readPort);

	  		//Initialisation
	  		sockMulti = sockUDP_create();
	  		sockUDP_reuse (sockMulti);
	  		setHostByAddr (&EP_multi, "any", readPort);
	  		sock_bind (sockMulti, readPort);
	  		sockUDP_join (sockMulti, multiHostAddress,&request);

	  		//Use service
          		getEcho (sockMulti);

	  		//Finalisation
	  		sockUDP_drop (sockMulti, request);
	  		close(sockMulti);

//----------------------------------
		  }
     	 	 sock_shutdown(MC_socket, 1);
	 	 readmsg = NULL;
	 	 while  (readmsg != NULL) 
			readmsg = (char *)readMessage(MC_socket);

//-----------------------------------

	}
}

Last edited by blowtorch; 09-04-2006 at 12:03 PM.. Reason: put in code tags... cheers jim
  #2 (permalink)  
Old 09-04-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Please put your code in code tags [ c o d e ].... [/ c o d e ]
(no spaces in the the tags).
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0