[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

TCP Socket Programming

codefest11

4/18/2011 6:58:00 AM

Hi,

I am writting a c program to send tcp packet (packet is a string) over socket.Please tell me how to form a string which contains NUL char.


8 Answers

Martin Ambuhl

4/18/2011 7:12:00 AM

0

On 4/18/2011 2:58 AM, Ajit Teli wrote:
> I am writting a c program to send tcp packet (packet is a string) over socket.
> Please tell me how to form a string which contains NUL char.

Your TCP documentation may tell you that a packet is a string, but it is
not, at least not a string as defined in C. It is a series of values,
probably storable in chars, which is not the same thing.

A string in C is terminated by a char with the value 0, and ASCII NUL is
such a value. Now you might store the values in an array of char.
Every string is an array of char, but not every array of char is a
string. Remember that a string is terminated by a 0-valued char. If
your array has none, then it is not a string. If it has such a char
embedded in data, then the initial string ends there.

To send a sequence of values stored in a char array, the simplest way is
to simply send each of the values until you reach the end of the array.

jacob navia

4/18/2011 7:45:00 AM

0

Le 18/04/11 08:58, Ajit Teli a écrit :
> Hi,
>
> I am writting a c program to send tcp packet (packet is a string) over socket.Please tell me how to form a string which contains NUL char.
>
>

char mystring[25];

That is an array of characters of 25 positions.

memset(mystring,0,25);

Now, "mystring" contains ONLY zeroes. You can set a single character to
zero by using:

mystring[12]=0;

Note that you should NOT use string functions with the array of
characters because they will get lost since they expect a
terminating zero.


Martin Ambuhl

4/18/2011 7:51:00 AM

0

On 4/18/2011 3:12 AM, Martin Ambuhl wrote:
> On 4/18/2011 2:58 AM, Ajit Teli wrote:
>> I am writting a c program to send tcp packet (packet is a string) over
>> socket.
> > Please tell me how to form a string which contains NUL char.
>
> Your TCP documentation may tell you that a packet is a string, but it is
> not, at least not a string as defined in C. It is a series of values,
> probably storable in chars, which is not the same thing.
>
> A string in C is terminated by a char with the value 0, and ASCII NUL is
> such a value. Now you might store the values in an array of char. Every
> string is an array of char, but not every array of char is a string.
> Remember that a string is terminated by a 0-valued char. If your array
> has none, then it is not a string. If it has such a char embedded in
> data, then the initial string ends there.
>
> To send a sequence of values stored in a char array, the simplest way is
> to simply send each of the values until you reach the end of the array.
>

I should add:

If you are doing TCP/IP programming, you should have a library of
functions which handles packets. Perhaps those are in your sockets
library if not in a separate TCP/IP library. You should have routines
to which you pass the address of the packet and perhaps its length as
well, and that routine should handle all the necessary work for you.

eBooks

4/18/2011 11:47:00 AM

0

Am 18.04.2011 08:58, schrieb Ajit Teli:
> Hi,
> I am writting a c program to send tcp packet (packet is a string) over socket.Please tell me how to form a string which contains NUL char.

Here's a practical introduction to socket-programming in C:
http://www.filesonic.com/file/18335153/0123745403TCPS...

Barry Briggs

4/18/2011 12:25:00 PM

0

Ajit Teli wrote:

> I am writing a C program to send TCP packet (packet is a string) over socket

http://beej.us/gu...

Keith Thompson

4/18/2011 3:47:00 PM

0

jacob navia <jacob@spamsink.net> writes:
> Le 18/04/11 08:58, Ajit Teli a écrit :
>> I am writting a c program to send tcp packet (packet is a string)
>> over socket.Please tell me how to form a string which contains NUL
>> char.
>
> char mystring[25];
>
> That is an array of characters of 25 positions.
>
> memset(mystring,0,25);
>
> Now, "mystring" contains ONLY zeroes. You can set a single character to
> zero by using:
>
> mystring[12]=0;
>
> Note that you should NOT use string functions with the array of
> characters because they will get lost since they expect a
> terminating zero.

jacob, did you miss a step there? You explicitly stored null characters
in mystring, then warned about using string functions on it because they
expect null characters.

Of course you're correct that you shouldn't apply string functions
to an array of characters *unless* you're certain that the array
contains a terminating '\0'.

(And arrays that are to be sent as TCP packets will not necessarily
contain any zero bytes.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

China Blue Veins

4/18/2011 5:11:00 PM

0

In article <9fednbMwFJVjfTbQnZ2dnUVZ_sudnZ2d@giganews.com>,
Ajit Teli <user@compgroups.net/> wrote:

> Hi,
>
> I am writting a c program to send tcp packet (packet is a string) over
> socket.Please tell me how to form a string which contains NUL char.

Outside of the str... functions, a NUL is just another char, treated the same as
any other char. Depending on where you want it and how you construct your packet
contents, you can sometimes trick str...functions to place it for you. Otherwise
just assign 0 or '\0' to the char array. And use the mem... functions instead of
str... to preserve embedded nulls.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. I am in the Nile.

Bill Cunningham

4/22/2011 7:03:00 PM

0

Martin Ambuhl wrote:

> I should add:
>
> If you are doing TCP/IP programming, you should have a library of
> functions which handles packets. Perhaps those are in your sockets
> library if not in a separate TCP/IP library. You should have routines
> to which you pass the address of the packet and perhaps its length as
> well, and that routine should handle all the necessary work for you.

This would be done with ntos() and ntol() and such wouldn't it? I have
UNIX network programming and it is a very good book. That is what I've read
so far. Beeg's guide mentioned later in this thread is a very excellent
tutorial too.

Bill