Chat Program


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Chat Program
# 1  
Old 07-25-2006
Question Chat Program

i need chat program in c. know sockets to some extent.
# 2  
Old 07-25-2006
i had the c program to transfer files. can anybody tell me the procedure to change it into chat program.
# 3  
Old 07-25-2006
check out this link: http://beej.us/guide
# 4  
Old 07-27-2006
hai friends,
the following is my code. i am not getting the right output. kindly help me.
Code:
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <stdio.h>
#include <readline/readline.h>
#define max 4096
int main()
{
int cpid,sd,nsd,i,port=5200;
char fname[30],line[max];
struct sockaddr_in ser;
struct sockaddr_in cli;
ssize_t n;

if ((sd=socket(AF_INET,SOCK_STREAM,0))<0)
        {
        printf("\nError:Socket Creation");
        return 0;
        }
bzero((char*)&ser,sizeof(ser));
printf("\nPort Address is %d",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);

if (bind(sd,(struct sockaddr*)&ser,sizeof(ser))<0)
        {
        printf("\nError:Binding");
        return 0;
        }
i=sizeof(cli);    
listen(sd,1);

printf("Server Module\n");

nsd=accept(sd,(sd,(struct sockaddr*)&cli),&i);
if (nsd==-1)
        {
        printf("\nError:Client accepts the problem");
        return 0;
        }
printf("Client Accepted");
printf("Start Chatting");

if (cpid=(fork()<0)) { // this is the child process
    close(sd); // child doesn't need the listener
    scanf("%s",line);
        if (send(nsd, line,sizeof(line), 0) == -1)
        printf("send");
    close(nsd);

}
if(cpid>0)
{
//receive the message steps
}
close(sd);

return 0;
}   

// client program
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define max 4096

int main()
{
int n,sd,nsd,i,port=5200;
char line[max],file[30];
struct sockaddr_in ser;
int childpid;
if ((sd=socket(AF_INET,SOCK_STREAM,0))<0)
        {
        printf("\nError:Socket Creation");
        return 0;
        }
bzero((char *)&ser,sizeof(ser));
printf("\nPort Address is %d",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if (connect(sd,(struct sockaddr*)&ser,sizeof(ser))==-1)
        {
        printf("\nError:Binding");
        return 0;
        }
printf("Client Module");
printf("\nClient:");
if (fork()) { // this is the child process
    close(sd); // child doesn't need the listener
        if ((n=recv(sd, line, max, 0)) == -1) {
        printf("recv");
        exit(1);
    }

    line[n] = '\0';

    printf("Received: %s",line);
close(nsd);
}
close(sd);

return 0;
}


Last edited by blowtorch; 07-27-2006 at 06:12 AM.. Reason: to put in code tags.
# 5  
Old 07-27-2006
how to read strings and send it to server or to client. actually in my chat program, iam gng to use fork(). using child process i'm gng to send the message and using parent process i'm gng to receive the message. hw i can do.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Chat program between two soft terminals of my Linux machine

here i havent tried to make a communication between two terminals of different machines not either any socket program my simple aim is use ipc for chat between two soft terminals(tty's) here is my view two terminals with two applications working on same fifo one fifo b/w two terminals try... (3 Replies)
Discussion started by: shyam.sunder91
3 Replies

2. Web Development

Can you embed Skype or any other video chat/chat program into a webpage?

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)
Discussion started by: phil_heath
2 Replies

3. UNIX for Dummies Questions & Answers

Secure Chat program for UNIX

I am wanting to know if you have ever found an extremely secure chat program for UNIX (Solaris 7), such that I could control which users can chat with which users, ie John (teacher) can talk to Suzy (student) or Sam (student) and they can talk back to their teacher BUT there is absolutely ... (1 Reply)
Discussion started by: ixeye
1 Replies

4. Programming

a simple chat program

any suggestions on how i could create a simple chat program between two terminals using pipes? thanks (1 Reply)
Discussion started by: kelogs1347
1 Replies

5. Programming

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... (1 Reply)
Discussion started by: powermind
1 Replies

6. IP Networking

Implementation of chat program

Hello there! Can anybody help me out with the steps required in implementing a chat program using sockets? Bye! (2 Replies)
Discussion started by: hufs375
2 Replies

7. UNIX for Dummies Questions & Answers

Chat program

Hi to all small question for someone extremely new to the subject. Could anyone point me in the direction of the source code for a client server, message passing program? Thanx for all your help (6 Replies)
Discussion started by: Mrchat
6 Replies
Login or Register to Ask a Question