How to send a uint8_t through a socket

I

iceman

Hi,
I am having a varible in a structure as uint8_t
eg
struct eg
{
uint8_t a;
uint16_t b;
}
I send this I use htons for uint16 to align it .I left uint8_as it
is.But, when I sent it through the socket.
but I get only garbage on the other side

Cheers,
Jegan
 
C

Christopher

Hi,
I am having a varible in a structure as uint8_t
eg
struct eg
{
uint8_t a;
uint16_t b;}

I send this I use htons for uint16 to align it .I left uint8_as it
is.But, when I sent it through the socket.
but I get only garbage on the other side

Cheers,
Jegan

Standard C++ doesn't know what a socket is. Additionally, I don't
think the host to network order family of functions is either. Try one
of the newsgroups dedicated to the networking library you are using
such as alt.winsock.programming or one of the linux groups for BSD
sockets. I'd also recommend you include a lot more information.
"Garbage on the other side" doesn't say much at all and could be any
number of things. For example, are you using tcp or udp? are you
sending a byte at a time or the whole struct? I recommend you include
your entire sending and receiving functions when you post there.

This post is OT here.
 
I

iceman

Hi,
Here is the code.The value of b in the struc is going through but not
a .when i print it in the server side..nothing is getting printed.
I changed the code a little[from the first post so htons/ntonl is
used]


struct exam
{
uint8_t b;
uint32_t a;
};

////////////////////////////////////server


int main()
{
int listenSocket, connectSocket, i;
unsigned short int listenPort;
socklen_t clientAddressLength;
struct sockaddr_in clientAddress, serverAddress;
char line[LINE_ARRAY_SIZE];

cout << "Enter port number to listen on (between 1500 and 65000): ";
cin >> listenPort;

// Create socket for listening for client connection requests.
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
cerr << "cannot create listen socket";
exit(1);
}

serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(listenPort);

if (bind(listenSocket,
(struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "cannot bind socket";
exit(1);
}
`
// Wait for connections from clients.
listen(listenSocket, 5);

while (1) {
cout << "Waiting for TCP connection on port " << listenPort <<
" ...\n";

clientAddressLength = sizeof(clientAddress);
connectSocket = accept(listenSocket,
(struct sockaddr *) &clientAddress,
&clientAddressLength);
if (connectSocket < 0) {
cerr << "cannot accept connection ";
exit(1);
}

// Show the IP address of the client.
cout << " connected to " << inet_ntoa(clientAddress.sin_addr);

char x[123];
exam *o;
while (recv(connectSocket,x,sizeof(exam), 0) > 0) {

cout <<((exam *)x)->a<<"\n";
cout <<((exam *)x)->b<<"\n";
}
}

////////////////////client

int main()
{
int socketDescriptor;
unsigned short int serverPort;
struct sockaddr_in serverAddress;
struct hostent *hostInfo;
char buf[MAX_LINE + 1], c;
uint8_t flag=3;
buf[0]=flag;

cout << "Enter server host name or IP address: ";
cin.get(buf, MAX_LINE, '\n');


hostInfo = gethostbyname(buf);
if (hostInfo == NULL) {
cout << "problem interpreting host: " << buf << "\n";
exit(1);
}

cout << "Enter server port number: ";
cin >> serverPort;
cin.get(c); // dispose of the newline

socketDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (socketDescriptor < 0) {
cerr << "cannot create socket\n";
exit(1);
}

serverAddress.sin_family = hostInfo->h_addrtype;
memcpy((char *) &serverAddress.sin_addr.s_addr,
hostInfo->h_addr_list[0], hostInfo->h_length);
serverAddress.sin_port = htons(serverPort);

if (connect(socketDescriptor,
(struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "cannot connect\n";
exit(1);
}


exam *t1,t2;
t1=&t2;;
t2.a=21;

t2.b=1;


unsigned char x[10];
char *y=(char *)&t2;



// Stop when the user inputs a line with just a dot.

// Send the line to the server.
if (send(socketDescriptor,y,sizeof(exam), 0) < 0) {
cerr << "cannot send data ";
close(socketDescriptor);
exit(1);
}

}

return 0;

}
 
I

iceman

the words in the bracket are supposed to read
[from the first post so htons/ntonl is
NOT used]
 
I

iceman

here is the output from the server side

Enter port number to listen on (between 1500 and 65000): 1234
Waiting for TCP connection on port 1234 ...
connected to 127.0.0.1:33508
21

Waiting for TCP connection on port 1234 ...
 
C

Christopher

here is the output from the server side

Enter port number to listen on (between 1500 and 65000): 1234
Waiting for TCP connection on port 1234 ...
connected to 127.0.0.1:33508
21

Waiting for TCP connection on port 1234 ...

That's good stuff. Now all you have to do is consolidate your 4 posts
and start a thread on one of the other newsgroups I mentioned.
I think you missed the _T_ in the "post there" part of my original
reply.
 
M

Martin Vuille

ps.com:
cout <<((exam *)x)->b<<"\n";

If I am not mistaken this will cause b to be output
as an ASCII character rather than as a decimal value.
Since ASCII 1 is not a printable character, you see
nothing in the output.

MV
 
I

iceman

If I am not mistaken this will cause b to be output
as an ASCII character rather than as a decimal value.
Since ASCII 1 is not a printable character, you see
nothing in the output.

MV

yep..that was the prob..

thanks for your help..
 
S

Sam

iceman said:
yep..that was the prob..

You also have another problem. Try running the sender on Linux, or the
receiver program on Solaris. Your homework assignment is to figure out why
you get garbage, in that case.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHl9rfx9p3GYHlUOIRAnAyAJ0QvJhjU3JJB7qGP7k38LCBIzurwQCeNTV9
yKa7K0LDwu317Kio8C63foM=
=yHXP
-----END PGP SIGNATURE-----
 
D

Daniel T.

iceman said:
I am having a varible in a structure as uint8_t
eg
struct eg
{
uint8_t a;
uint16_t b;
}
I send this I use htons for uint16 to align it .I left uint8_as it
is.But, when I sent it through the socket.
but I get only garbage on the other side

Here's something to think about...

int main() {
cout << sizeof( eg ) << '\n';
cout << sizeof( uint8_t ) << '\n';
cout << sizeof( uint16_t ) << '\n';
}

On my machine the output is 4, 1 and 2... What does that tell us?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top