UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » client/server interaction

#1 2007-03-16 02:57 AM

vijraj
Member
Registered: 2006-10-23
Posts: 23

Re: client/server interaction

Hi all! 

I have a lot of questions right now.
I am writing a small client server program.

1) I was given a protocol .It says first bit -  0/1(failure/success)
    next four bits - command
    etc.
    so,how can I set the bits in a byte in c accordingly? What type of data type should we use for that?

2)the message also contains integer types(for lenght of message.transaction number etc) .So can i use htons/htonl or ntohs/ntohl for converting them to the network format? Should I do converting individually or Can I do it after I format it into the string(forming a big message containing all of the message)?

I will ask more when I am there. :smile: :oops: :cool: :???:

Thanks for the help.

Offline

#2 2007-03-16 10:37 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: client/server interaction

#include <stdio.h>

int main()
{
	unsigned int bit_mask=0;
	int nCountBits	= (sizeof(bit_mask)*8);
	while(true)
	{
		int nBitPos	= 0;
		while(true)
		{
			printf("Enter bit number to change : ");
			scanf("%u",&nBitPos);
			if (nBitPos<0 && nBitPos>nCountBits)
			{
				printf("incorrect bit position, enter value between 0 and 7\n");
			} else {
				break;
			}
		}
		unsigned int nNewValue=0;
		while(true)
		{
			printf("Enter new bit value : ");
			scanf("%u",&nNewValue);
			if (!(nNewValue==0 || nNewValue==1))
			{
				printf("incorrect bit value, enter value 0 and 1\n");
			} else {
				break;
			}
		}
		if (nNewValue==1)
		{
			printf("bit_mask OR %d\n",(1 << nBitPos));
                         //create new mask with all except first bit set to 1
                         //than shift it left in the position you need
                         //and join old a new mask
                         // if there is 0 bit in the postion you | with 1
                         // and will get 1,if there 1 you | with 1 and
                         // and will get 1 again
			bit_mask	|= (nNewValue << nBitPos);
		} else {
			printf("bit_mask AND %d\n",~(nNewValue << nBitPos));
                         //create new mask with all except first bit set to 1
                         //than shift it left in the position you need
                         // invert all bits via ~ operator
                         //and join old a new mask
                         // if there is 0 bit in the postion you & with )
                         // and will get 0,if there 1 you | with 0 and
                         // and will get 0 again
			bit_mask	&= ~(1 << nBitPos);
		}
		int nBitMaskCopy	= bit_mask;
		for (int i=0;i<nCountBits-1;++i)
		{
			printf("%u",(nBitMaskCopy%2));
			nBitMaskCopy	= (nBitMaskCopy-(nBitMaskCopy%2))/2;
		}
		printf("\nbit_mask as int %u\n",bit_mask);
	}
	return 0;
}

I'm not sure you need to convert data.It seems that it depends on platform you use

Offline

#3 2007-03-16 12:15 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: client/server interaction

Offline

#4 2007-03-16 01:11 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: client/server interaction

The byte order should be specified by the protocol you were given. If nothing is mentioned then it's most likely network byte order.

Offline

#5 2007-03-16 03:13 PM

vijraj
Member
Registered: 2006-10-23
Posts: 23

Re: client/server interaction

Thank you.That was great help yurec,Rob and i3839.
Thanks for posting code for me.Thanks all for ur time.Can you please put a few comments in the code.Appreciate ur help always.
Vijraj.:grin:

Offline

#6 2007-03-16 04:17 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: client/server interaction

I've putted comments into the code.Probably comments are not good enough.
Some days ago I heard very interesting joke about english and c++:
Once a programmer decided to learn english.He took lessons for a month and ended the course successful.While another studetns had to learn for three month more.A friend asked him:"How could you study it so fast?" He answered:"It's so easy!They took all words from c++!"

Offline

#7 2007-03-16 04:40 PM

vijraj
Member
Registered: 2006-10-23
Posts: 23

Re: client/server interaction

HAHAHA..good one..
Anyway, I got it.
thanks.

Offline

#8 2007-03-19 07:01 PM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: client/server interaction

Sorry but i really had to react about that..."joke":

I bet my 2 hands that you guys both don't have a girlfriend ;-)


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#9 2007-03-20 07:32 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: client/server interaction

c++ is simpler

Offline

  • Index
  • » C
  • » client/server interaction

Board footer

Powered by FluxBB