LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-16-2008, 12:16 PM   #1
zeinab
LQ Newbie
 
Registered: May 2008
Posts: 3

Rep: Reputation: 0
Smile I write chat program with select but how to use nchurses?!


this is my program;
how to use nchurses to complete my program
CLIENT :

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>

#define SERVER_PORT 5433
#define MAX_LINE 256
int
main(int argc, char * argv[])
{
FILE *fp;
struct hostent *hp;
struct sockaddr_in sin;
char *host;
char buf[MAX_LINE];
int s;
int len;
if (argc==2)
{
host = argv[1];
}
else
{
fprintf(stderr, "usage: simplex-talk host\n");
_exit(1);
}
/* translate host name into peer’s IP address */
hp = gethostbyname(host);
if (!hp)
{
fprintf(stderr, "simplex-talk: unknown host: %s\n", host);
_exit(1);
}
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
sin.sin_port = htons(SERVER_PORT);
/* active open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("simplex-talk: socket");
_exit(1);
}
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
perror("simplex-talk: connect");
close(s);
_exit(1);
}
/* main loop: get and send lines of text */
while (fgets(buf, sizeof(buf), stdin))
{
buf[MAX_LINE-1]='\0';
len = strlen(buf) + 1;
send(s, buf, len, 0);
}

}

SERVER:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>

#define SERVER_PORT 5433
#define MAX_PENDING 5
#define MAX_LINE 256
int
main()
{
struct sockaddr_in sin;
char buf[MAX_LINE];
int len;
int s, new_s;
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(SERVER_PORT);
/* setup passive open */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("simplex-talk: socket");
_exit(1);
}
if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0)
{
perror("simplex-talk: bind");
_exit(1);
}
listen(s, MAX_PENDING);

fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
//struct sockaddr_in myaddr; // server address
struct sockaddr_in remoteaddr; // client address
int fdmax;

FD_SET(s, &master);
fdmax = s; // so far, it's this one
socklen_t addrlen;
int i,j;
int nbytes;

/* wait for connection, then receive and print text */
while(1)
{
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
{
perror("select");
_exit(1);
}
for(i = 0; i <= fdmax; i++)
{
if (FD_ISSET(i, &read_fds))
{ // we got one!!
if (i == s)
{
// handle new connections
addrlen = sizeof remoteaddr;
if ((new_s = accept(s,(struct sockaddr *)&remoteaddr, &addrlen)) == -1)
{
perror("accept");
}
else
{
FD_SET(new_s, &master); // add to master set
if (new_s > fdmax)
{ // keep track of the maximum
fdmax = new_s;
}
printf("selectserver: new connection from %s on socket %d\n", \
inet_ntoa(remoteaddr.sin_addr), new_s);
}//end accept
}//end if(i==s)
else
{
// handle data from a client
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0)
{

// got error or connection closed by client
if (nbytes == 0)
{
// connection closed
printf("selectserver: socket %d hung up\n", i);
}
else
{
perror("recv");
}
//close(i); // bye!
//FD_CLR(i, &master); // remove from master set
}
else
{

fputs(buf, stdout);
// we got some data from a client
for(j = 0; j <= fdmax; j++)
{
// send to everyone!
if (FD_ISSET(j, &master))
{
// except the listener and ourselves
if (j != s && j != i)
{
if (send(j, buf, nbytes, 0) == -1)
{
perror("send");
}
}
}
}//end for
}//end else recv
}//end else i==s
}//end if FD_ISSET
}//end for

}//end while

}
 
Old 08-17-2008, 12:25 PM   #2
mjmwired
Member
 
Registered: Apr 2004
Distribution: CentOS6, CentOS5, F16, F15, Ubuntu, OpenSuse
Posts: 620

Rep: Reputation: 39
Did you try the Ncurses howto page?
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
 
Old 09-08-2008, 04:33 AM   #3
zeinab
LQ Newbie
 
Registered: May 2008
Posts: 3

Original Poster
Rep: Reputation: 0
thanks

thanks mjmwired...I wrote the program.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
chat system in linux with NCHURSES zeinab Linux - Newbie 2 05-23-2008 05:17 AM
chat program in c++ enth Programming 2 08-31-2007 04:10 AM
program to select services amon Linux - Software 2 07-20-2005 04:14 PM
Chat Program derick.schmidt Linux - Software 4 11-13-2004 08:19 AM
inability to write a working chat script.... Slestak Linux - Networking 8 05-30-2002 02:26 AM


All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration