Write the source code for messenger


 
Thread Tools Search this Thread
Top Forums Programming Write the source code for messenger
# 1  
Old 10-22-2006
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 Smilie
# 2  
Old 10-22-2006
http://beej.us/guide/bgnet/ helped me out alot, you could even use libgaim to http://gaim.sourceforge.net/api/ .
# 3  
Old 10-24-2006
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.
# 4  
Old 10-26-2006
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.
Code:
  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.
# 5  
Old 10-26-2006
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:
Code:
// 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__*/

Code:
// 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)
{
 ...
}

Code:
// 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);  
}

Code:
// 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);
}

Code:
$ 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.

Last edited by Corona688; 10-26-2006 at 12:15 PM..
# 6  
Old 10-28-2006
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. Smilie

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 :

Code:
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 Smilie
# 7  
Old 10-31-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Source code

hii... i am a biginner....and i have linux source code ,downloaded from some website ,a compressed file on windows and dont know how do compile them..... (4 Replies)
Discussion started by: M K Raju
4 Replies

2. Shell Programming and Scripting

Source code compilation

Need assistance in Source code compilation . When installing a software compiling a source code . Whatever the output that prints on the screen i want to log it into a file. How can i see output and store the output to file ./configure make make install Is there other way of seeing output... (5 Replies)
Discussion started by: ajayram_arya
5 Replies

3. Linux

Source code

I need the source code of fedora. plz plz plz help me........... (1 Reply)
Discussion started by: neh
1 Replies

4. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

5. UNIX for Dummies Questions & Answers

Where can i get unix source code?

Sir please tell me where can i get source code for some unix kernal and shell also. (1 Reply)
Discussion started by: VIPUL15
1 Replies

6. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

7. Shell Programming and Scripting

write page source to standard output

I'm new to PERL, but I want to take the page source and write it to a file or standard output. I used perl.org as a test website. Here is the script: use strict; use warnings; use LWP::Simple; getprint('http://www.perl.org') or die 'Unable to get page'; exit 0; ... (1 Reply)
Discussion started by: wxornot
1 Replies

8. UNIX for Dummies Questions & Answers

Where can I review the source code?

A very n00b question: After compiling and installing software, where does the original source code reside? I'd like to study the source code of some of the ports I've installed. Thanks! :D (1 Reply)
Discussion started by: Aaron Van
1 Replies
Login or Register to Ask a Question