Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

PLEASE HELP !!! URGENT

tollitolli Member Posts: 44
hiii

please if u dont know about pc networking please reply by guiding me where to post the question below. else, kindly answer it...

am doing a chat program in C using linux KDevelopC/C++

i want any client to chat with the server that listens on port 51717 and both the server and the client can end the chat conversation by sending "EXIT". but when the client exits the server should keep connected so that other clients can connect to it...

my prgm does the chat and when the client initiates an EXIT, it exits while server keeps running as needed.

HOWEVER, when the server sends an EXIT, it waits the client to send smthing before the client can stop ...

how do i resolve the issue??

below is my program code:

client:

[code]

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define SERVER_PORT 51717 /*port number on which server is listening agreed upon by both client
and server*/

#define BUF_SIZE 4096 /*block transfer size*/



int main(int argc, char **argv)
{
int c, s, bytes;
char buf[BUF_SIZE]; /*buffer for incoming file*/
char BUF_CLIENT[BUF_SIZE];
struct hostent *h; /*info about server*/
struct sockaddr_in channel; /*holds IP address*/

if(argc!=2) fatal("Usage:client server-name file name");
h = gethostbyname(argv[1]); /*look up host's IP address*/
if(!h) fatal("gethostbyname failed");

s = socket(PF_INET, SOCK_STREAM,IPPROTO_TCP);
/*AF_INET= internet type socket, SOCK_STREAM= identifies TCP*/

if(s<0) fatal("socket");
memset(&channel, 0, sizeof(channel));
channel.sin_family = AF_INET;
memcpy(&channel.sin_addr.s_addr, h->h_addr, h->h_length);
channel.sin_port = htons(SERVER_PORT);

c = connect(s, (struct sockaddr *) &channel, sizeof(channel));
if(c<0) fatal("connect failed");

while(1)
{
printf("Client says: ");
//cin
fgets(BUF_CLIENT,sizeof BUF_CLIENT, stdin);//read input from prompt

//send
write(s, BUF_CLIENT, strlen(BUF_CLIENT)+1);

if(memcmp(BUF_CLIENT,"EXIT",4)==0)
break;

//else
memset(BUF_CLIENT,'',BUF_SIZE);
/*Sets the first BUF_SIZE bytes of the block of memory pointed by BUF_CLIENT to
the specified value '' (interpreted as an unsigned char).*/


//recieve
read(s, buf, BUF_SIZE); // read from socket

//cout
printf("Server says: %s",buf,"
");

if(memcmp(buf,"EXIT",4)==0)
break;

//else
memset(buf,'',BUF_SIZE);

}

close(c);
exit(1);

}

fatal(char *string)
{
printf("%s
", string);
exit(1);
}

[/code]

server:

[code]

#include<sys/types.h>
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define SERVER_PORT 51717 /*port number on which server is listening agreed upon by both
client and server*/
#define BUF_SIZE 4096 /*block transfer size*/
#define QUEUE_SIZE 10




int main(int argc, char *argv[])
{
int s, b, l, fd, sa, bytes, on=1;
char buf[BUF_SIZE]; /*buffer for outgoing file*/
char BUF_CLIENT[BUF_SIZE];
struct sockaddr_in channel; /*hold's IP address*/


/*Build address structure to bind to socket */
memset(&channel, 0, sizeof(channel)); /* zero channel*/
channel.sin_family = AF_INET;
channel.sin_addr.s_addr = htonl(INADDR_ANY);
/*The symbolic constant INADDR_ANY is used to refer to the internet address of the machine
on which the process is running. htonl: host to network long*/
channel.sin_port = htons(SERVER_PORT);/*host to network short*/

/*Passive open. Wait for connection. */
s= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /*creates an unbounded socket*/
/*AF_INET= internet type socket, SOCK_STREAM= identifies TCP*/

if (s<0) fatal("socket failed");
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));

b= bind(s, (struct sockaddr *) &channel, sizeof(channel));
if (b<0) fatal("bind failed");

l=listen(s, QUEUE_SIZE); /*specify queue size*/
/*Once a socket is created and bound to an address, a server socket has to listen for
connections. s is a socket which is bound to an address.QUEUE_SIZE = length of the
backlog queue. (This is the number of connections that can be waiting to be accepted)*/

if(l<0) fatal("listen failed");


/*Socket is now set up and bound. Wait for connection and process it*/
while(1) /* Server enters an infinite loop to wait for clients
to connect*/

{
sa =accept(s,0,0); /*call to accept_block for connection request*/
/*s=socket, 0 is a zero pointer to struct sockaddr and the third argument is a
zero size of struct sockaddr*/

if (sa<0) fatal("accept failed");


while(1)
{
//recieve
read(sa,buf,BUF_SIZE); /*read file name from socket*/

//cout
printf("Client says: %s",buf,"
");

/*memcmp compares two memory regions. 4 is the number of bytes to compare*/
if(memcmp(buf,"EXIT",4) == 0 )
break;
//else
memset(buf,'',BUF_SIZE);
/*Sets the first BUF_SIZE bytes of the block of memory pointed by buf to
the specified value '' (interpreted as an unsigned char).*/

//cin
printf("Server says: ");
fgets(BUF_CLIENT,sizeof BUF_CLIENT, stdin);//read input from prompt

write(sa,BUF_CLIENT,strlen(BUF_CLIENT)+1);//sending

if(memcmp(BUF_CLIENT,"EXIT",4)==0) //server can also end the chat
break;
//else
memset(BUF_CLIENT,'',BUF_SIZE);

}
close(sa);
}
}

fatal(char *string)
{
printf("%s
", string);
exit(1);
}
[/code]

thanks a lot :):)

please reply asap

Comments

  • unixfreakunixfreak Member Posts: 9
    please look at my entry at
    urgent help on sockets
    in the same forum;
    i transmitted certain defined messages but you can have these messages from STDIN and then send them;
    if doesnt help themn please make a post
    i will be happy to help u
Sign In or Register to comment.