473,657 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9042
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 clientAddressLe ngth;
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.s in_family = AF_INET;
serverAddress.s in_addr.s_addr = htonl(INADDR_AN Y);
serverAddress.s in_port = htons(listenPor t);

if (bind(listenSoc ket,
(struct sockaddr *) &serverAddre ss,
sizeof(serverAd dress)) < 0) {
cerr << "cannot bind socket";
exit(1);
}
`
// Wait for connections from clients.
listen(listenSo cket, 5);

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

clientAddressLe ngth = sizeof(clientAd dress);
connectSocket = accept(listenSo cket,
(struct sockaddr *) &clientAddre ss,
&clientAddressL ength);
if (connectSocket < 0) {
cerr << "cannot accept connection ";
exit(1);
}

// Show the IP address of the client.
cout << " connected to " << inet_ntoa(clien tAddress.sin_ad dr);

char x[123];
exam *o;
while (recv(connectSo cket,x,sizeof(e xam), 0) 0) {

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

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

int main()
{
int socketDescripto r;
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(b uf);
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

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

serverAddress.s in_family = hostInfo->h_addrtype;
memcpy((char *) &serverAddress. sin_addr.s_addr ,
hostInfo->h_addr_list[0], hostInfo->h_length);
serverAddress.s in_port = htons(serverPor t);

if (connect(socket Descriptor,
(struct sockaddr *) &serverAddre ss,
sizeof(serverAd dress)) < 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(socketDes criptor,y,sizeo f(exam), 0) < 0) {
cerr << "cannot send data ";
close(socketDes criptor);
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******** *************** ***********@i29 g2000prf.google grou
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.c omwrote:
iceman <jegan...@gmail .comwrote innews:e5****** *************** *************@i 29g2000prf.goog legrou
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 objektorientier ter Datenverarbeitu ng
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
3019
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, to HIS port 8801. This means that I need to bind my sender socket to (myaddress,8801), use it for the send, then shut it down and close it. Then I want to wait, say 30 seconds, and do it all over again. Naturally, the socket's ReuseAddress...
1
1593
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 = socket.Receive(mesajlar,0,mesajlar.Length,SocketFlags.None);
1
2358
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 = socket.Receive(mesajlar,0,mesajlar.Length,SocketFlags.None);
11
7707
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 the app, even the first time. The application will be required to be sending out repeated emails, about one every second or two. Must this be done asynchronously? Thank you. -Greg I get the generic error messages;
2
13143
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 Socket.Close()). I see the server connection status go from ESTABLISHED to FIN_WAIT2, the client connection go from ESTABLISHED to WAIT_CLOSE. Step3: client.Send() succeeds and return the number of bytes sent (over the closed connection!)
3
4292
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3156
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 System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
1
2732
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 anything. I would love some help, here is the code: import socket import thread import time
3
1739
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):
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.