Sponsored Content
Top Forums Programming Write the source code for messenger Post 302094272 by Corona688 on Thursday 26th of October 2006 11:07:28 AM
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..
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. Linux

Source code

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

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

8. 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
All times are GMT -4. The time now is 02:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy