# Sending character 0 over network

## Recommended Posts

Hi, I am using an external network library, which is called ENet.I mostly completed the data transmission.I would like to send x,y coordinates over it, I am using 2 chars for this.I get the coordinate with inline function (a*256+b) on the other side.However if a char is 0 then everything collapses after that.It's not a problem like it stops reading, because I get it char by char.However as I said data gets strange after that. I am thinking of increasing a and b by 1 but that will cause error too when coord. is for example 255. What should I do? Is it a problem of ENet(when getting data from the buffer), but thats not wise because it sends the size of packets within every packet.Any help would be appreciated, thanks =) Please don't mark threads 'solved' in this forum. --jpetrie [Edited by - jpetrie on May 4, 2009 8:56:28 PM]

##### Share on other sites
Show the exact code where you pass your data to send to ENet, and the opposite snippet where you read the data from ENet.

##### Share on other sites
They are not the problem I am quite sure, because I checked the string that server is sending, and I only do

string bilgi = event.packet->data

by client and the results are as I said(again I saw it by debugging).
But if you want to check

ENetPacket* Komut(unsigned char komut,unsigned int ID,int type,int takim,int x,int y){	// Player Yarat	        ustring mesaj;            uchar x1,x2,y1,y2;                        x2 = x%256;            x1 = (x-x2)/256;			y2 = y%256;			y1 = (y-y2)/256;			mesaj += (unsigned char)komut;			mesaj += (unsigned char)ID;			mesaj += type;			mesaj += takim;			mesaj += x1;			mesaj += x2;			mesaj += y1;			mesaj += y2;// I checked mesaj, and it is obviously fine ..			ENetPacket * packet = enet_packet_create (mesaj.c_str(),mesaj.length() + 1, ENET_PACKET_FLAG_RELIABLE);			return packet;}

if(mNetwork->GetEvent()){	ENetEvent* event = &mNetwork->event;	ustring bilgi;	switch (event->type)        {        case ENET_EVENT_TYPE_CONNECT:            printf ("A new client connected from %x:%u.\n", event->peer -> address.host,event->peer -> address.port);            break;        case ENET_EVENT_TYPE_RECEIVE:			bilgi = event->packet->data;// I debug here and see //that data doesn't "continue" after the 0 character, that I looked by the //server side.It's just full of 0's with the count of packet size after the //first 0 character.look below*://...... //

That means that ENet is able to determine the size of the package but can't continue to read data after char 0, and it just continues to write 0 till the packet end.I guess that this is a problem about ENet, but I don't know how to solve..

btw ustring is "typedef basic_string<unsigned char> ustring;" because packets should contain unsigned chars according to ENet.But this works great without 0's..

Again Thanks.

##### Share on other sites
What is the data type of event->packet->data? If it's const unsigned char* or something like that, then assigning from there into the ustring will lose the data after the first zero byte, because C++ doesn't know to use the length information from the packet to assign the string.

To fix that, you can do something like

        case ENET_EVENT_TYPE_RECEIVE:			bilgi.assign(event->packet->data, event->packet->length);

##### Share on other sites
Zahlman's got it already.

Whenever you assing a (const) char* to a std::string the string class tries to read all characters up to a 0. If a 0 is part of the chain then the string class doesn't know.

##### Share on other sites
Oh gee, it is working perfectly now ! If I had known how to do this my uniqueID's ,teamnumbers and slotnumbers would start from 0, but now they are starting from 1.
.
Anyway it doesn't matter thank you all for your help =)

exact code should be;

bilgi.assign(event->packet->data,event->packet->dataLength);

##### Share on other sites
You might also consider whether a std::vector<char> might be a better choice of container for received data than a std::string. :)

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978695

• 21
• 14
• 12
• 10
• 12