Hello everyone,
I'm having trouble with a code I need to write, I have never coded in C and I need to do this program in C. I have created my checksum code already and need to implement this:

I need to send a complete ASCII file in 128 byte packets including the checksum. The sender takes the ASCII file as input sends a 128 byte packet on the data queue then waits specified timeout on the control queue for an acknowledgement. If a timeout is incurred the sender sends the packet again - up to three times before giving up.

fsend <msggid1> <msgqid2> <filename>

Receiver Implementation

The receiver shall block on the data queue waiting for packets, once a packet is received the sender shall validate the internet checksum and send an acknowledgement back to the sender on the control queue. If the checksum is invalid the receiver shall request a retransmission.

frecv <msgqid1> <msgqid>

can anyone help?? I am stuck and don't know what to do. Please help me write the code in C. PLEASE!!

Recommended Answers

All 10 Replies

yeah, i hear ya, but youre gonna have to pony up some of your own code first.

since you're not there yet, you might want to take a look at Beej's Guide ... it is a great reference, full of valuable and relevant examples.

yeah, i hear ya, but youre gonna have to pony up some of your own code first.

since you're not there yet, you might want to take a look at Beej's Guide ... it is a great reference, full of valuable and relevant examples.

here's My checksum code that I took forever to figure out:

#include <stdlib.h>
#include<string.h>

unsigned short cal_chksum(char *msg)
{
  unsigned short lsw, msw;
  unsigned short *ptr;
  unsigned int c;
  int i;
  int len;
  len=strlen(msg)/2;
  c=0;
  ptr=(unsigned short *)msg;
  for(i=0; i<len;i++)
    {
      c+=ptr[i];
    }
	msw=(c>>16)& 0xffff;
      c+=msw;
      lsw=~(c&0xffff);
      
      if (lsw==0)
	{
	  lsw=0xffff;
	}
	return(lsw);
      }
//////////////////
commented: >10 posts and still no code tags - FAIL -4

here's my checksum code that i took forever to figure out:

#include <stdlib.h>
#include<string.h>

unsigned short cal_chksum(char *msg)
{
  unsigned short lsw, msw;
  unsigned short *ptr;
  unsigned int c;
  int i;
  int len;
  len=strlen(msg)/2;
  c=0;
  ptr=(unsigned short *)msg;
  for(i=0; i<len;i++)
    {
      c+=ptr[i];
    }
	msw=(c>>16)& 0xffff;
      c+=msw;
      lsw=~(c&0xffff);
      
      if (lsw==0)
	{
	  lsw=0xffff;
	}
	return(lsw);
      }
//////////////////

can anyone help!! Please!!

here's My checksum code that I took forever to figure out:

//Checksum.c
#include <stdlib.h>
#include<string.h>

unsigned short cal_chksum(char *msg)
{
  unsigned short lsw, msw;
  unsigned short *ptr;
  unsigned int c;
  int i;
  int len;
  len=strlen(msg)/2;
  c=0;
  ptr=(unsigned short *)msg;
  for(i=0; i<len;i++)
    {
      c+=ptr[i];
    }
	msw=(c>>16)& 0xffff;
      c+=msw;
      lsw=~(c&0xffff);
      
      if (lsw==0)
	{
	  lsw=0xffff;
	}
	return(lsw);
      }
//////////////////
//msgsend.c
// this is my message send file I need to implement the first part here and need help
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <fcntl.h>

struct msgbuf {
    long mtype;     /* message type, must be > 0 */
    char mtext[1];  /* message data */
};

int main(int argc, char *argv[])
{

	int msqid;               
	size_t len;
	int 	type;

    struct msgbuf *buf;
	
	if (argc != 4)
	{
		printf("Usage: %s <msgqid> <type> <msg> \n", argv[0]);
		exit(1);
	}
    
	msqid = atoi(argv[1]);
	type = atoi(argv[2]);
    len = sizeof(long) + strlen(argv[3]);

	buf = (struct msgbuf *)calloc(len, sizeof(char)); 
	buf->mtype = type;
    memcpy(buf->mtext, argv[3], strlen(argv[3]));
    

	if (msgsnd(msqid, buf, len, 0) == -1)
	{
		perror("msgsnd error");
		exit(1);
	}

	
	exit(0);

}
////Msgrcv.c
////Here is my message receive file. I need to implement the receive stuff here.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <fcntl.h>

#define MAX_MSG_LEN	(4096)

struct msgbuf {
    long int mtype;
    char     mtext[1];
};

int main(int argc, char *argv[])
{

    struct msgbuf  *buf;
	int msqid;               
	int n;     
	
	if (argc != 2)
	{
		printf("Usage: msgctrl <msgqid> \n");
		exit(1);
	}
	msqid = atoi(argv[1]);

	buf = (struct msgbuf *)malloc(MAX_MSG_LEN);

	if ((n = msgrcv(msqid, buf, MAX_MSG_LEN, 0, 0)) == -1)
	{
		perror("msgrcv error");
		exit(1);
	}

	printf("read %d  bytes, type = %ld \n", n, buf->mtype);
    printf("msg = %s \n", buf->mtext);
	
	exit(0);

}

Do you have an actual question or do you just like making new copies of your checksum code for the fun of it? You don't have to quote the same post over and over and over.

And if we don't know what you want, posting code with not details at all is not going to help.

Well I took along time coding these files since i don't know how to code in c and I don't really comment my files till the end but hey thanks for looking at my code...I guess. Hopefully someone else can give me more help.

well, i've got the same problem walt has: i don't know what your question is.

can you try to formulate a specific question, about something that's not working for you? what are you doing, what do you expect to happen, and what is actually happening? i'm not going to guess. if you pose some specific issues, i can look for obvious problems, and if needed, then tomorrow evening i can try and run it on my linux box.

we'll help you out but you need some patience. the delay in help will also encourage you to poke around at it yourself in the meantime. personally, i find that's the best way to learn :)


P.S. have you looked at Beej's Guide yet? i've found that almost every question about using C language to set up network hosts and clients can be answered there.

Im not using sockets for this program.
I basically need to implement this (NO SOCKETS!):
Ive been working on it all week and I think my code is all bad right now except my checksum.
I just need to implement this but I don't know how. Im not asking for someone to do it for me just get me started. Im a NOOB to c.

I need to send a complete ASCII file in 128 byte packets including the checksum. The sender takes the ASCII file as input sends a 128 byte packet on the data queue then waits specified timeout on the control queue for an acknowledgement. If a timeout is incurred the sender sends the packet again - up to three times before giving up.

//this is in the command I need to type in my terminal
// fsend <msggid1> <msgqid2> <filename>

Receiver Implementation

The receiver shall block on the data queue waiting for packets, once a packet is received the sender shall validate the internet checksum and send an acknowledgement back to the sender on the control queue. If the checksum is invalid the receiver shall request a retransmission.

frecv <msgqid1> <msgqid>

look, i'm tellin' ya, dood, everything you're trying to do is in Beej.

if only you'd bother to read it, you'd see for yourself, there's an entire chapter on message queues.

.there's no point in me trying to explain this to you at this point, because i haven't found a single introductory text that explains it better than Beej. i would just essentially be repeating everything he says. since he's already said it, why should i copy him.

just read the chapter already.

hey thanks, I thought that Beej was only for sockets. It answered all my questions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.