Write the source code for messenger

I have a few questions for the ones who know.

I started to "learn" for a project.I wanna write the source code for a messenger like Gaim.I started learning about ncurses , GTK+ and UNIX socket programming.But I'm a bit stucked.

Most of the books' I get , like : The Art of Unix programming , Linux socket programming by example , etc .. are really complex for me to understand . I need something easier , for me to understand the complex principles of UNIX socket programming.

Maybe in a few months , I'll be able to write the source code , to a mini-messenger programm , like Gaim , but not so complex .

If anyone can give me some clues , tutoriarls , or something else to manage understanding complex socket progrmamming , be my guest :smiley:

Beej's Guide to Network Programming helped me out alot, you could even use libgaim to http://gaim.sourceforge.net/api/ .

Try taking it in the other direction -- build a commandline client, then once you have that working, add GTK windows and such. Building it top-down will mean you'll be stuck changing the GUI every 5 minutes, and the code will end up scattered in callbacks instead of ordered rationally.

I'm actually trying to do the same thing (write a simple instant messenger program) but am running into a few problems.

Right now I am just focusing on the "communication" portion, using a command-line interface. I plan to add the GUI stuff later.

I am using C++ and the sockets api and have taken the approach of building both a "Server" and "Client" class. I have designed my instant messaging application to have both the server and client in one executable program.

So, in my main() function, I will:
1) Create an instance of Server ( for example, "Server my_serv( ); " )
2) Create an instance of Client ( for example, "Client my_cli( ); " )
3) Start the server ( for example, my_serv.start() )
4) Client stuff... ( connect to remote computer, enter text data to send, etc ).

One problem I have run into with this approach:

I attempt to start the server then use the client functionality, but then my program hangs, because the socket program is listening for a connection. ( I've tried non-blocking IO with the fcntl call below ).
This bit just gets all current flags for the file descriptor sd1, then adds the O_NONBLOCK flag to it.

  rc |= fcntl( sd1 , F_SETFL , ((fcntl( sd1 , F_GETFL ))|O_NONBLOCK) );

I believe I might need to use the select() function, but I'm not sure if I'm headed down the wrong path.

Would it be best to split this up into 2 programs ( i.e. one as a server and the other as a client )? Or could (should?) I use fork(), start the server in one process, then the client in the other? (I've tried the fork()ing approach, but had some problems.)

I'm trying to get an idea of how the program would be best designed. Any ideas are appreciated.

This could be done with forking, but you'd need to remember that, aside from pre-existing files and sockets, every forked program becomes completely seperate. Variables changed in one won't be reflected in another.

Threads, on the other hand, share the same process and memory, running more or less "at the same time" thanks to multitasking. You can use the same variables, but this can cause strange synchronization problems unless you're careful to control access to these things with thread-control devices like semaphores and mutexes...

Imagine two threads doing a memcpy to one area of memory, one starts a memcpy and is interrupted, then the second one starts and finishes a memcpy to the same area of memory, then the first one finishes, leaving the memory half one thing and half the other. A mutex would make one wait for the other one. Threads are also more portable; both Windows and UNIX have threads, but Windows does not and will never have fork().

But this is a lot of stuff to learn simultaneously if these are all new concepts to you, and designs that have to be complete for them to work are really hard to build. Try simplifying it even further. Forget about the client/server objects for now and just hardcode the making and breaking of connections in main(), which in turn, would call communications functions that use these sockets without worrying about how to make or break them. Pseudocode:

// comm.h
// Including this more than once won't hurt
#ifndef __COMM_H__
#define __COMM_H__

// Lets include file work in C *and* C++
#ifdef __cplusplus
extern "C" {
#endif

int recieve_message(int fd, char *buf, int maxlen);
int send_message(int fd, const char *buf);

#ifdef __cplusplus
}
#endif
#endif/*__COMM_H__*/
// comm.c
#include "comm.h"
// Return of 0 means success, return <0 means error
int recieve_message(int fd, char *buf, int maxlen)
{
  ...
}

// Return of 0 means success, return <0 means error
int send_message(int fd, const char *buf)
{
 ...
}
// server.c
#include "comm.h"

int main()
{
  char buf[512];
  int serversock=open_server_socket();
  int client=accept(serversock); // will block, so what?
  recieve_message(client,buf,512);
  fprintf(stderr,"%s\n",buf);
  send_message(client,"hey guys alj af MY FACE IS A ROTTORN BANANA");
  close(client);
  close(serversock);
  return(0);  
}
// client.c
#include "comm.h"
int main()
{
  char buf[512];
  int server=connect_to_server("localhost");
  send_message(server,"How is your face doing?");
  recieve_message(server,buf,512);
  fprintf(stderr,"%s\n",buf);  
  return(0);
}
$ gcc -c server.c client.c comm.c
$ gcc server.o comm.o -o server
$ gcc client.o comm.o -o client
$ ./server &
$ ./client &
...

Once you get the send/recieve functions working how you want them, then you can build a system to hold it all together. Don't get ahead of yourself. The right way to do this might not be apparent until you've actually done some work with the communication itself.

Thank's guy's for answering . I found your word's useful. I'm getting over the project , everithing seem's more clearly now.
I have a few things to acommodate and everithing seem's to work just fine.

I only have a question for you.I found myself in a situation , I cannot resolve . Maybe I haven't think for the right choice yet , but you can help me ..

I written the server and client code , for some sort of messenger ( mini-version ) , and I don't know ( I may know , but not having the right idea ) , how to implement something. :mad:

I want to make an easy thing.In the source code for the client , I want to implement someting like this :

-when the client want's to transmit data , it gets the buffer ( getstr(buffer) )
-when something arive at the client , some data , it 's print's the string's arived on main window.

I cannot implement this.How can I implement this in C ? I write some sort of code , down :


int nbytes;
for (;;)
{
if (nbytes=recv(listener,buf,sizeof(buf),0) >0 )
{
        printw("%s\n",buf);
        refresh();
}
else{

getstr(s);
send(listener,s,sterlen(s),0);}

}


The problem , is that this code , is how you see , non-useful and childish.
I want something simple , when I write data , on the client ( getstr(s) ) , to be sent ( to listener -> server ) and when I receive some data ( string's ) , to be printed on the main window.This is all.
The problem is that I cannot make this ..it block's .If I don't enter a string to be sent to the ( listener -> socket server ) , it won't print ( received data ).

Should I use files ?.. or something like that ..

A help , may be needed , if you guy's get the idea , what I want :cool:

You're getting ahead of yourself again. If you do that before you have good, abstracted communications functions, which from your snippet I'm guessing you don't, your code will become a mess.

Once you do, you can divide up the work with threads that will work independently; one thread waits for messages from the server, one waits for user input... look for pthreads examples.

Roger that dude !

Next time , I have to look better.I saw that pthreads is the solution I wanted.And your right man , my code just suck's , I have to code it better.

Oo.. But what about POSIX , u mention POSIX threads , what is this POSIX , on the another hand .

Cheer's ! :slight_smile:

pthreads means POSIX threads. POSIX is a standard which means that POSIX threads, among many other things, work the same way across many different UNIX and UNIX-like systems.

For the third time, I'd suggest simplifying the problem before you even begin to think about concurrency. Don't build a chat server, build a communication library. Build sets of tiny, tiny example programs; one pair where the server sends and the client recieves, another where the client sends and the server recieves, another where the client connects to what you name instead of anything builtin, etc. These don't have to be interactive, so you don't have to worry about user input and network input at the same time; you can get them working without worrying about threads. Once you perfect sending, recieving, and connecting, then you can put the puzzle pieces together in a multithreaded application.

I understand the temptation to skip all that and build a simple something without any framework, but when you get into it, these "simple" things inevitably become way more complicated than they first appear. If you don't break it down, you won't be able to understand what you've written after leaving it for a week.

Your totaly right man ..

I want to make/write the code fast , but I forget about the simple things , which do the business.

I already practice , and make some examples of this one's :wink: And the good thing , after many tries , I understand the point. :smiley:

I understand , how pthreads work , google it , and I can now just mofigy my source code and pthread it .

:cool:

EDIT : I finished to write the source code , for a multithreaded chat server , and it's working fine.Now I have to add more line's just for fun.

I have a question , how can I license , this software ( I want someting big ) , with GPL ? ( GNU ) , I saw many licensed source code .. Just a question :smiley:

Can anyone send me the chat code in C in unix using threading in a single program.like we have to assume there is no server..plz.. i need it badly