468,503 Members | 2,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,503 developers. It's quick & easy.

How to send a uint8_t through a socket

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

Jan 23 '08 #1
9 7664
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;

}


Jan 23 '08 #2
the words in the bracket are supposed to read
[from the first post so htons/ntonl is
NOT used]
Jan 23 '08 #3

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 ...

Jan 23 '08 #4
On Jan 22, 9:32 pm, iceman <jegan...@gmail.comwrote:
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.

Jan 23 '08 #5
iceman <je******@gmail.comwrote in
news:e5**********************************@i29g2000 prf.googlegrou
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
Jan 23 '08 #6
On Jan 23, 3:59 pm, Martin Vuille <jpm...@yahoo.comwrote:
iceman <jegan...@gmail.comwrote innews:e5**********************************@i29g20 00prf.googlegrou
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
yep..that was the prob..

thanks for your help..
Jan 23 '08 #7
On 2008-01-23 00:58:37, Christopher wrote:
This post is OT here.
and
>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.
In regards to this:
>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.
Is this really OT here?

Gerhard
Jan 23 '08 #8
On Jan 23, 12:38 pm, Gerhard Fiedler <geli...@gmail.comwrote:
On 2008-01-23 00:58:37, Christopher wrote:
This post is OT here.
and
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.
In regards to this:
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.
Is this really OT here?
No. Nor are some of the other issues with his code (like the
undefined padding the struct might contain). But his original
question was formulated in a way which would probably make it
seem system dependent.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 23 '08 #9
iceman <je******@gmail.comwrote:
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?
Jan 24 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Hasan O | last post: by
11 posts views Thread by hazz | last post: by
3 posts views Thread by danfolkes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.