Im working on a program and one of the functions writes data to a socket. Everything is working except one part that Im having trouble figuring out.

If I create a variable and assign a hex value to it inside the program everything works. (e.g. char variable[] = '\xff\xff\xff\xff\xff'). But if I put the hex in a file and have the program read it from the file into char variable[] then it doesn't work.

Now what happens is the first way gets written to the socket as hex (how I want it.) but the second way gets written to the network as ascii (how I don't want it.). Now whats confusing to me is that they both are stored in the same variable just in different ways.

So how can I specify to the write function, to write my data as hex instead of ascii to the socket?

Recommended Answers

All 30 Replies

Write the file as binary instead of text.

Well im not using the program to write the file. Just to read it. Although I guess I could try to have it read the file, then write a new file as binary, then read the new file. But that just seems.... dirty. lol

Here is my main function. I have the char buff[] = '\xff\xff\xff....' commented out. Using that works but I really want it to be able to read in the hex from a file. so I can make packet templates.

int main (int argc, char *argv[])
	{
		int i = 0;
		int p = 1;
		int t, f;
		FILE *fp;
		char *pfile, *device;
		/*char buff[] = "\xff\xff\xff\xff\xff\xff\x00\x15\xc5\xf6\x25\x28\x08\x06\x00\x01\x08\x00\x06\x04\x00\x01\x00\x15\xc5\xf6\x25\x28\x0a\x00\x00\x02\x00\x00\x00\x00\x00\x00\x0a\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";*/


		if (argc == 1)
			{
				usage(argv[p]);
				exit(0);
			}
		while (p != argc)
			{
				if (strcmp(argv[p], "-i") == 0)
					{
						p++;
						device = argv[p];
					}
				else if (strcmp(argv[p], "-p") == 0)
					{
						p++;
						pfile = argv[p];
					}
				else if (strcmp(argv[p], "-t") == 0)
					{
						p++;
						t = atoi(argv[p]);
					}
				else if (strcmp(argv[p], "-f") == 0)
					{
						p++;
						f = atoi(argv[p]);
					}
				else if (strcmp(argv[p], "-sm") == 0)
					{
						p++;
						rp.smac = argv[p];
						rp.smflag = 1;
					}
				else if (strcmp(argv[p], "-dm") == 0)
					{
						p++;
						rp.dmac = argv[p];
						rp.dmflag = 1;
					}
				else if (strcmp(argv[p], "-si") == 0)
					{
						p++;
						rp.sip = inet_addr(argv[p]);
						rp.siflag = 1;
					}
				else if (strcmp(argv[p], "-di") == 0)
					{
						p++;
						rp.dip = inet_addr(argv[p]);
						rp.diflag = 1;
					}
				else
					{
						usage(argv[0]);
						exit(0);
					}
				p++;
			}
		if (pfile == NULL)
			{
				printf("You need to specify a packet file to send.\n");
				exit(1);
			}
		else if (device == NULL)
			{
				printf("Please specify a network interface.\n");
				exit(1);
			}
		else if (t == 0)
			{
				printf("Number of packet(s) to send not specified. Sending one packet.\n");
				t = 1;
			}
		else if (f == 0)
			{
				printf("No duration specified for sending packet(s). Sending 10 packets a second.\n");
				f = 100000;
			}

		/* Open Packet File and Read it into buff */
		fp = fopen(pfile, "r");
		fseek(fp, 0L, SEEK_END);
		sk.sz = ftell(fp);
		fseek(fp, 0L, SEEK_SET);
		char buff[sk.sz];
		fread(buff, 1, sk.sz, fp);
		memcpy(&buff[sk.sz-1], "", 1);
		fclose(fp);


		/*if (packet(buff) != 0)
			{
				printf("Error: Unable to parse packet file.\n");
				exit(1);
			}	*/	

		if (sock(device) != 0)
			{
				printf("Error: Unable to create socket.\n");
				exit(1);
			}

		while (i < t)
			{
				write(sk.sock,buff,sizeof(buff));
				i++;
				usleep(f);
			}
		close(sk.sock);
		printf("Wrote %u packets to network!\n", i);
		return 0;
	}

Well im not using the program to write the file. Just to read it. Although I guess I could try to have it read the file, then write a new file as binary, then read the new file. But that just seems.... dirty.

That would be dirty, not that you've given us that information...

Then read the file and convert the 'string' into hex.

That would be dirty, not that you've given us that information...

Then read the file and convert the 'string' into hex.

I tried converting the string to hex, but it converts the already hex (char data type) to hex. So the hex \xff\xff gets converted to \x66\x66. Which is not what I want. ;(

Since you didn't bother to show us what you did, we have no idea what is wrong.

If you have your data in the format you want, then don't convert it any further - C'mon! ;)

You can read your hex data in as a string using
fgets(buffer, sizeof(buffer), filePointer);

Make sure that buffer has at least two char's more than the longest string of hex. One more char for the newline fgets will add, and one char more for the end of string char, also.

If you want to write out the data, don't write out the string - that has the two extra chars in it still. Instead, if your strlen(buffer) was 70. You will want to write out 69 chars only, (strlen() doesn't count the end of string char), so use a loop:

for(i=0;i<69;i++) {
  printf("%c", buffer[i]);
}

You can also truncate output directly with

printf("%69s", buffer);

iirc.

Since you didn't bother to show us what you did, we have no idea what is wrong.

My bad man. I think I had done sprintf into another variable using %x and %02x.

I have the hex stored in the file like this. But without the quotes.

"ffffffffffff0015c5f62528080600010800060400010015c5f625280a0000020000000000000a00001e000000000000000000000000000000000000"

If you have your data in the format you want, then don't convert it any further - C'mon! ;)

You can read your hex data in as a string using
fgets(buffer, sizeof(buffer), filePointer);

Make sure that buffer has at least two char's more than the longest string of hex. One more char for the newline fgets will add, and one char more for the end of string char, also.

If you want to write out the data, don't write out the string - that has the two extra chars in it still. Instead, if your strlen(buffer) was 70. You will want to write out 69 chars only, (strlen() doesn't count the end of string char), so use a loop:

for(i=0;i<69;i++) {
  printf("%c", buffer[i]);
}

You can also truncate output directly with

printf("%69s", buffer);

iirc.

Thanks for your response. Although that option also converts the already hex data into hex. So for example ff ff ff ff is converted into 66 66 66 66.

We're getting close.

Thanks for all the help guys.

Maybe Im asking the wrong questions. Maybe the question I should be asking is how can I write hex in a variable to a socket?

Change your hex array from char to unsigned integer type. Then print out your numbers, using %x (for lower case letters), or %X (for Upper Case letters) for your hex output.

I'd get this working just the way you want it, to a file, before working it into packets to send.

I think that the question that so far has been unasked and needs to be is:
The file you are reading, is it a binary file with binary values or a text file with hex characters in it?

I think that the question that so far has been unasked and needs to be is:
The file you are reading, is it a binary file with binary values or a text file with hex characters in it?

Its a text file with hex in it.

This is a first for me, but how about:

1) use fgets() to read in the entire line of text, as a string.

2) Now go through the buffer, and add the extra byte (or two) that an unsigned int needs, (which sizeof(int) will tell you. So you add sizeof(int) - the sizeof(char), [which is almost always 1 byte]), and recreate the hex unsigned int numbers, in the buffer.

3) Then use sscanf() with %x, to get each of the now rejuvenated hex numbers from the buffer, into your variable for output to a file, or to a port.

Isn't it possible for the program that is producing the hex text, to output the data as hex unsigned int's, with %x? That would surely be a big convenience.

Is it possible that the "text" just looks like chars, and is in fact, unsigned int's, already? You couldn't tell just by looking, but you could tell by using scanf() or sscanf(), etc.

This is a first for me, but how about:

1) use fgets() to read in the entire line of text, as a string.

2) Now go through the buffer, and add the extra byte (or two) that an unsigned int needs, (which sizeof(int) will tell you. So you add sizeof(int) - the sizeof(char), [which is almost always 1 byte]), and recreate the hex unsigned int numbers, in the buffer.

3) Then use sscanf() with %x, to get each of the now rejuvenated hex numbers from the buffer, into your variable for output to a file, or to a port.

Isn't it possible for the program that is producing the hex text, to output the data as hex unsigned int's, with %x? That would surely be a big convenience.

Is it possible that the "text" just looks like chars, and is in fact, unsigned int's, already? You couldn't tell just by looking, but you could tell by using scanf() or sscanf(), etc.

Ok Im getting closer. I dont fully understand how to do #2 but this is what Im currently doing. and its working except when it gets written to the network six extra zeros are getting added between each hex.

here is what i'm doing now.

while (c != 62)
			{
				fscanf(fp, "%x", &buff[c]); /* Using fscanf instead of fread*/
				printf("%x", &buff[c]); /* The output from this isnt exactly right, but im not sure if its printf or fscanf. Either way it doesnt really matter at the moment.*/
				c++;
			}

This is what is being written to the network.

0000 | ff 00 00 00 ff 00 00 00 ff 00 00 00 ff 00 00 00
0010 | ff 00 00 00 ff 00 00 00 00 00 00 00 15 00 00 00
0020 | c5 00 00 00 f6 00 00 00 25 00 00 00 28 00 00 00
0030 | 08 00 00 00 06 00 00 00 00 00 00 00 01 00

As you can see, three sets of "00" are being added between each hex, which doesnt happen when I do the printf above.

Here is my entire main function.

int main (int argc, char *argv[])
	{
		int i = 0;
		int p = 1;
		int t, f;
		FILE *fp;
		char *pfile, *device;
		/*char buff[] = "\xff\xff\xff\xff\xff\xff\x00\x15\xc5\xf6\x25\x28\x08\x06\x00\x01\x08\x00\x06\x04\x00\x01\x00\x15\xc5\xf6\x25\x28\x0a\x00\x00\x02\x00\x00\x00\x00\x00\x00\x0a\x00\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";*/


		if (argc == 1)
			{
				usage(argv[p]);
				exit(0);
			}
		while (p != argc)
			{
				if (strcmp(argv[p], "-i") == 0)
					{
						p++;
						device = argv[p];
					}
				else if (strcmp(argv[p], "-p") == 0)
					{
						p++;
						pfile = argv[p];
					}
				else if (strcmp(argv[p], "-t") == 0)
					{
						p++;
						t = atoi(argv[p]);
					}
				else if (strcmp(argv[p], "-f") == 0)
					{
						p++;
						f = atoi(argv[p]);
					}
				else if (strcmp(argv[p], "-sm") == 0)
					{
						p++;
						rp.smac = argv[p];
						rp.smflag = 1;
					}
				else if (strcmp(argv[p], "-dm") == 0)
					{
						p++;
						rp.dmac = argv[p];
						rp.dmflag = 1;
					}
				else if (strcmp(argv[p], "-si") == 0)
					{
						p++;
						rp.sip = inet_addr(argv[p]);
						rp.siflag = 1;
					}
				else if (strcmp(argv[p], "-di") == 0)
					{
						p++;
						rp.dip = inet_addr(argv[p]);
						rp.diflag = 1;
					}
				else
					{
						usage(argv[0]);
						exit(0);
					}
				p++;
			}
		if (pfile == NULL)
			{
				printf("You need to specify a packet file to send.\n");
				exit(1);
			}
		else if (device == NULL)
			{
				printf("Please specify a network interface.\n");
				exit(1);
			}
		else if (t == 0)
			{
				printf("Number of packet(s) to send not specified. Sending one packet.\n");
				t = 1;
			}
		else if (f == 0)
			{
				printf("No duration specified for sending packet(s). Sending 10 packets a second.\n");
				f = 100000;
			}

		/* Open Packet File and Read it into buff */
		fp = fopen(pfile, "r");
		fseek(fp, 0L, SEEK_END);
		sk.sz = ftell(fp);
		fseek(fp, 0L, SEEK_SET);
		unsigned int buff[92];
		/*fread(buff, sizeof(buff), 1, fp);*/
		int c = 0;
		printf("%d\n", sk.sz);
		while (c != 62)
			{
				fscanf(fp, "%x", &buff[c]);
				printf("%x", &buff[c]);
				c++;
			}
		memcpy(&buff[sk.sz-1], "", 1);
		fclose(fp);

		/*if (packet(buff) != 0)
			{
				printf("Error: Unable to parse packet file.\n");
				exit(1);
			}	*/	

		if (sock(device) != 0)
			{
				printf("Error: Unable to create socket.\n");
				exit(1);
			}

		while (i < t)
			{
				write(sk.sock,buff,62);
				i++;
				usleep(f);
			}
		close(sk.sock);
		printf("Wrote %u packets to network!\n", i);
		return 0;
	}

Have you tried changing buff to data type unsigned int, and just reading the hex data in, as hex numbers?

It appears it would work.

Have you tried changing buff to data type unsigned int, and just reading the hex data in, as hex numbers?

It appears it would work.

Yes I have, What it does is it converts the already hex ascii digits to hex.

The original hex values, were good, but this isn't hex:
ffffffffffff0015c5f62528080600010800060400010015c5f625280a0000020000000000000a00001e000000000000000000000000000000000000

Hex would start with 0x.

Is this an absolutely HUGE hex number being represented, but lacking it's 0x?

If so, you can handle it as plain ascii, and prepend the required start to it, when you are ready to send it to your output. The rest of the time, it's just a string of char's, and you don't need hex anything for it.

So now, I believe that is the answer - leave it as ascii char's, and be careful how you work with it. For instance, input with fgets() will add or include, both a newline char, and an end of string char, to the end of the data. If you use fgets(), you'll want to remove those last two char's before the data is finally ready to use.

Do a little test with it, because I'm not sure if default chars will suffice for this. You may need unsigned chars, instead. But not hex ANYTHING. ;)

The original hex values, were good, but this isn't hex:
ffffffffffff0015c5f62528080600010800060400010015c5f625280a0000020000000000000a00001e000000000000000000000000000000000000

Hex would start with 0x.

Is this an absolutely HUGE hex number being represented, but lacking it's 0x?

If so, you can handle it as plain ascii, and prepend the required start to it, when you are ready to send it to your output. The rest of the time, it's just a string of char's, and you don't need hex anything for it.

So now, I believe that is the answer - leave it as ascii char's, and be careful how you work with it. For instance, input with fgets() will add or include, both a newline char, and an end of string char, to the end of the data. If you use fgets(), you'll want to remove those last two char's before the data is finally ready to use.

Do a little test with it, because I'm not sure if default chars will suffice for this. You may need unsigned chars, instead. But not hex ANYTHING. ;)

Thanks for your reply, So please help me understand, I should be appending 0x to the hex digits? so instead of ffffffff it should read 0xff0xff0xff0xff?

Not appending, but prepending!

0123 is an octal value, has a prepended 0
0x123 is a hexadecimal number, has a prepended 0x (or 0X).

0x123U is a hex number that is unsigned
0x123L is a hex integer that is a long.
0x123UL is a hex number that is unsigned and a long

The zero x prepends each hex NUMBER, but not each hex digit in the number, of course.

You are making this so hard on yourself, and the responses you are getting don't help any.

1) You say the file is binary. So if you read the first byte of the file you should have 11111111 in the variable, not 01000110 or 01100110 (the letter F or f), correct?
2) If so, read the file properly. What you are doing is atrocious:

/* Open Packet File and Read it into buff */
    fp = fopen(pfile, "r");
    fseek(fp, 0L, SEEK_END);
    sk.sz = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    unsigned int buff[92];
    /*fread(buff, sizeof(buff), 1, fp);*/
    int c = 0;
    printf("%d\n", sk.sz);
    while (c != 62)
        {
            fscanf(fp, "%x", &buff[c]);
            printf("%x", &buff[c]);
            c++;
        }
    memcpy(&buff[sk.sz-1], "", 1);
    fclose(fp);

Make it simple:

/* Open Packet File and Read it into buff */
    fp = fopen(pfile, "r");
    byresRead= fread(buff, sizeof(buff), 1, fp);
    fclose(fp);

You now have your file read in, it's binary, and you can send it in binary. Is this what you need.

Or do you now need to convert the binary from the file into ASCII (HEX) characters so that your byte value 11111111 (binary) becomes 'FF' and the ASCII FF is sent?

You are making this so hard on yourself, and the responses you are getting don't help any.

1) You say the file is binary. So if you read the first byte of the file you should have 11111111 in the variable, not 01000110 or 01100110 (the letter F or f), correct?
2) If so, read the file properly. What you are doing is atrocious:

/* Open Packet File and Read it into buff */
    fp = fopen(pfile, "r");
    fseek(fp, 0L, SEEK_END);
    sk.sz = ftell(fp);
    fseek(fp, 0L, SEEK_SET);
    unsigned int buff[92];
    /*fread(buff, sizeof(buff), 1, fp);*/
    int c = 0;
    printf("%d\n", sk.sz);
    while (c != 62)
        {
            fscanf(fp, "%x", &buff[c]);
            printf("%x", &buff[c]);
            c++;
        }
    memcpy(&buff[sk.sz-1], "", 1);
    fclose(fp);

Make it simple:

/* Open Packet File and Read it into buff */
    fp = fopen(pfile, "r");
    byresRead= fread(buff, sizeof(buff), 1, fp);
    fclose(fp);

You now have your file read in, it's binary, and you can send it in binary. Is this what you need.

Or do you now need to convert the binary from the file into ASCII (HEX) characters so that your byte value 11111111 (binary) becomes 'FF' and the ASCII FF is sent?

Im just doing that for testing purposes. I originally had it setup using the fread function.

I need the hex FF, I can send the ASCII FF just fine, but thats not what I want. I want to write the hex FF to the socket. Just like if I had a variable set to equal hex at compile time.

Setting a variable to hex like this (char variable[] = '\xFF\xFF\xFF...') and then writing it to the socket; Writes the hex value. Which is what I want.

BUT, If I read the hex from a text file (stored in the file as text.) and then write it to the socket; It gets written as ASCII which is what I dont want.

I want to be able to read hex from a text file (again stored as text) and assign it to a variable just like if i where to assign the hex to the variable in the source at compile time. (char variable[] = '\xFF\xFF\xFF...').

I appreciate everyone's help and patience. My understanding of C is self taught so I dont have a solid foundation on how alot of things C related are handled.

Thanks again.

Sorry, I misrememberd what you said about the file.

We need to use the same basic terminology. Everyone seems to be getting confused with ambiguous information being tossed around.
1) Do not mention HEX unless you are talking about the characters '0' thru 'F'. HEX is not binary. Actually, from here on out, don't mention HEX again. I think you're confusing the issue and/or we are being confused by the term.
2) Use the terms BINARY and CHAR/CHARACTER for the values being read or sent or converted or whatever.
3) ff/FF is ambiguous, causing confusion. Use "BINARY FF" and "the chars FF" to avoid ambiguity and confusion.

So, updating my algo:

/* Open Packet File and Read it into buff */
    fp = fopen(pfile, "r");
    byresRead= fread(buff, sizeof(buff), 1, fp);
    fclose(fp);
    convert the characters in buff to binary.

What you have, is a mashup of data types - it's not just hex, and it's not ASCII either, but a bit of both.

That puts you WAY out in left field. If it were me, I'd take the data in, however you want, and then convert it into what I really wanted - AS A STANDARD DATA TYPE.

Probably use fgets() to get the string into a buffer, and then "coerce it" into the standard type I wanted, then use sscanf() to "lift the data, out of the buffer, and put it into a file, probably. Newlines and end of string chars, would be lost in the process.

I would definitely NOT leave the data in this mashup of two types of data, that it now is set in. There are many perfectly fine and standard data types in C. As you see here, there's a big advantage to using one of them, and a rookie mistake not to, imo.

Sorry, I misrememberd what you said about the file.

We need to use the same basic terminology. Everyone seems to be getting confused with ambiguous information being tossed around.
1) Do not mention HEX unless you are talking about the characters '0' thru 'F'. HEX is not binary. Actually, from here on out, don't mention HEX again. I think you're confusing the issue and/or we are being confused by the term.
2) Use the terms BINARY and CHAR/CHARACTER for the values being read or sent or converted or whatever.
3) ff/FF is ambiguous, causing confusion. Use "BINARY FF" and "the chars FF" to avoid ambiguity and confusion.

So, updating my algo:

/* Open Packet File and Read it into buff */
    fp = fopen(pfile, "r");
    byresRead= fread(buff, sizeof(buff), 1, fp);
    fclose(fp);
    convert the characters in buff to binary.

Ok I tried that, and they print out perfectly! but when I send them its all messed up????

Here is what Im doing.

/* Open Packet File and Read it into buff */
		fp = fopen(pfile, "r");
		fseek(fp, 0L, SEEK_END);
		sk.sz = ftell(fp);
		fseek(fp, 0L, SEEK_SET);
		char buff[sk.sz];

		fread(buff, sizeof(buff), 1, fp);

		/*fgets(buff, sizeof(buff), fp);*/
		memcpy(&buff[sk.sz-1], "\0", 1);
		fclose(fp);
		long newbuff[sk.sz];
		int c = 0;
		while (c != sk.sz)
			{
				newbuff[c] = strtol(&buff[c], NULL, 16);
				printf("%lx\n", newbuff[c]);
				c=c+3;
			}

		/*if (packet(buff) != 0)
			{
				printf("Error: Unable to parse packet file.\n");
				exit(1);
			}	*/	

		if (sock(device) != 0)
			{
				printf("Error: Unable to create socket.\n");
				exit(1);
			}

		while (i < t)
			{
				write(sk.sock,newbuff,sk.sz);
				i++;
				usleep(f);
			}
		close(sk.sock);
		printf("Wrote %u packets to network!\n", i);

My text file looks like this without the quotes.

"ff ff ff ff ff ff 00 15 c5 f6 25 28 08 06 00 01 08 00 06 04 00 01 00 15 c5 f6 25 28 0a 00 00 02 00 00 00 00 00 00 0a 00 00 1e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"

But when it gets written to the socket. It comes out like this. ;(

0000 | ff 00 00 00 f0 00 00 00 06 00 00 00 ff 00 00 00 ................
0010 | 04 00 00 00 74 01 00 00 ff 00 00 00 74 01 00 00 ....t.......t...
0020 | 44 00 00 00 ff 00 00 00 04 00 00 00 04 00 00 00 D...............
0030 | ff 00 00 00 c4 a1 15 00 c4 b1 15 00 ff 00 00 00 ................
0040 | 08 00 00 00 40 00 00 00 00 00 00 00 04 00 00 00 ....@...........
0050 | 50 e5 74 64 15 00 00 00 2c fe 13 00 2c fe 13 00 P.td....,...,...
0060 | c5 00 00 00 3c 33 00 00 04 00 00 00 f6 00 00 00 ....<3..........
0070 | 51 e5 74 64 00 00 00 00 25 00 00 00 00 00 00 00 Q.td....%.......
0080 | 00 00 00 00 28 00 00 00 06 00 00 00 04 00 00 00 ....(...........
0090 | 08 00 00 00 c4 a1 15 00 c4 b1 15 00 06 00 00 00 ................
00a0 | 3c 1e 00 00 f4 bf 1f 00 00 00 00 00 00 00 00 00 <...............
00b0 | ec f1 a6 bf ....

Any Ideas?

At this point, I don't believe I can help you. Good luck.

char buff[sk.sz];
		fread(buff, sizeof(buff), 1, fp);

You seem to believe that sizeof(buff) equals to sk.sz . It is not.

As a side note, always convert your longs with htonl() prior to transmission.

char buff[sk.sz];
		fread(buff, sizeof(buff), 1, fp);

You seem to believe that sizeof(buff) equals to sk.sz . It is not.

As a side note, always convert your longs with htonl() prior to transmission.

When using htonl I still get garbage written to the network. ;(

uint32_t newbuff[sk.sz];
		int c = 0;
		uint32_t test[sk.sz];
		while (c != sk.sz)
			{
				newbuff[c] = strtol(&buff[c], NULL, 16);
				test[c] = htonl(newbuff[c]);
				c=c+3;
			}

I get this written to the network.

0000 | 00 00 00 ff 05 00 00 00 00 b0 15 00 00 00 00 ff ................
0010 | 9c d9 15 00 08 0a 16 00 00 00 00 ff 03 00 00 00 ................
0020 | 00 00 00 00 00 00 00 ff 00 00 00 00 00 00 00 00 ................
0030 | 00 00 00 ff a1 bc 1a 00 f4 3f 1b 00 00 00 00 ff .........?......
0040 | 72 02 00 00 18 c7 f5 bf 00 00 00 00 08 00 00 00 r...............
0050 | f4 3f 1b 00 00 00 00 15 18 49 1b 00 30 c7 f5 bf .?.......I..0...
0060 | 00 00 00 c5 08 00 00 00 1e 00 00 00 00 00 00 f6 ................
0070 | 6c c7 f5 bf 8e 1a 1a 00 00 00 00 25 38 88 71 b7 l..........%8.q.
0080 | 1e 00 00 00 00 00 00 28 7a 16 1a 00 01 00 00 00 .......(z.......
0090 | 00 00 00 08 cd 17 1a 00 fc 44 1b 00 00 00 00 06 .........D......
00a0 | f4 3f 1b 00 00 00 00 00 00 00 00 00 98 c8 f5 bf .?..............
00b0 | 04 d8 19 00 ....

*Sigh*......Im about to put a bounty on this.

When using htonl I still get garbage written to the network. ;(

Of course. First sort out the sizeof(buff) issue.

Of course. First sort out the sizeof(buff) issue.

Why wouldnt it be the same as sk.sz? Its an array.

Anyways I changed it and even did a printf() for sk.sz and sizeof(buff) and they are both the same size. ????

fread(buff, sk.sz, 1, fp);

		/*fgets(buff, sizeof(buff), fp);*/
		memcpy(&buff[sk.sz-1], "\0", 1);
		fclose(fp);
		printf("%d %d\n", sk.sz, sizeof(buff));

Anyone? I got $10 for your paypal account to anyone that can help me solve it.

commented: Offering payment to volunteers is somewhat insulting. -4
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.