473,372 Members | 859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 8950
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gregory Hassett | last post by:
Hello, I want to periodically send a TCP packet to a peer, always from the same source port. That is, each packet will come from my local ip address, port 8801, and go to the peer's ip address,...
1
by: Hasan O | last post by:
hi , I have a problem with socket.send mesajlar = Encoding.ASCII.GetBytes("UGIR"); socketResultValue = socket.Send(mesajlar,0,mesajlar.Length, SocketFlags.None); socketResultValue =...
1
by: Hasan O | last post by:
hi , I have a problem with socket.send mesajlar = Encoding.ASCII.GetBytes("UGIR"); socketResultValue = socket.Send(mesajlar,0,mesajlar.Length, SocketFlags.None); socketResultValue =...
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
2
by: yvan | last post by:
Hi, Here is my client/server scenario: Step1: Client connects to server and sends data sucessfully (using Socket.Send()). Step2: Server gracefully exists (calls Socket.Shutdown() and...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
1
by: danfolkes | last post by:
Hey Everyone, I am trying to send repeated messages from a "Node" to a "Server". It works the first time I send the from the Node to Server, but after that it either errors, or does not do...
3
by: danfolkes | last post by:
Why does the message send only once? The node sends once, then fails after that. <code> import socket import thread import time def Node(nodeAddress):
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.