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
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.
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:
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..
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.
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 :
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
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.
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)
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)
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)
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)
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)
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)