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 01:03 PM..
Reason: put in code tags... cheers jim
hello everyone,
i am making chat server in linux using c. i have made programs in which group chat can take place between multiple clients but i am not able to understand how to make 2 particular clients chat with each other. please help!!! (1 Reply)
Hi,
I am trying to embed Skype or any other video chat/chat program into a webpage. Has anyone had success doing this? or know how?
Thanks
Phil (2 Replies)
request
create a chat system in linux where a user type smth and all the other users connected to server get the message.then a user have to create join leave or delete a channel of chat if he created it.i did the server and the client but i dont know how to implemt the chat rooms.i was ... (1 Reply)
The task is to create a server client chat that contains a few basic safeguards against floods etc and which is capable of issuing at least one or two commands on the client computer. Working samples of such arrangements abound but freeware/shareware samples are not readily available. A catch in... (3 Replies)
I want to make the following programm.
Using the server/client model I want 2 client to connect to the server then the server sends back to the clients the ip address and a number of a poort in order to open a udp connection between clients without using the server?
What I have done since now is... (2 Replies)
hi guys,
I need the code for a server client registration form.The server must ask for authentication .Then the client would send in data. This is stored in a file .The server sends back a receipt to the client as part of the payment done.
plz can some 1 get me the code... (9 Replies)
i came acors this coding when surfin the net.this code works perfectly.but as i am new to this socket programming i need sm coments quoted on it or explanation regarding this source code.
i have prb understanding the server.c i have posted it below
can u guys help me !!!!
cheerZ
The... (4 Replies)
I have been trying to write a very basic chat program but at the moment I am having problems getting it to be multiuser as it closes all connections when one client shutsdown. I have also been having problems trying to get the program to display a list of usernames to the clients. I have tried... (0 Replies)