I’m trying to use two ints coming from a socket to draw a circle.
vector<string> c = ofSplitString(str, ":");
string x = c[0];
string y = c[1];
int a = ofToInt(x);
int b = ofToInt(y);
ofSetColor(0,0,200);
ofCircle(a, b, 40);
I keep getting an error from the ofToInt function when I run this program. I assume the error is in how I’m converting from the array to the int, but I can’t figure out what I’m doing wrong. Does anyone have an idea? Thanks!
Which is referring to the definition of the ofToInt() function. It’s not giving a more specific error than that. Is there something obvious that I’m missing in the way that I’m assigning the variables perhaps? Any help is greatly appreciated! Thanks.
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
//we run at 60 fps!
ofSetVerticalSync(true);
//setup the server to listen on 11999
TCP.setup(5000);
//optionally set the delimiter to something else. The delimter in the client and the server have to be the same, default being [/TCP]
TCP.setMessageDelimiter("\n");
}
//--------------------------------------------------------------
void ofApp::update(){
ofBackground(20, 20, 20);
//for each client lets send them a message letting them know what port they are connected on
for(int i = 0; i < TCP.getLastID(); i++){
if( !TCP.isClientConnected(i) )continue;
//TCP.send(i, "hello client - you are connected on port - "+ofToString(TCP.getClientPort(i)) );
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetHexColor(0xDDDDDD);
ofDrawBitmapString("TCP SERVER Example \n\nconnect on port: "+ofToString(TCP.getPort()), 10, 20);
ofSetHexColor(0x000000);
ofRect(10, 60, ofGetWidth()-24, ofGetHeight() - 65 - 15);
ofSetHexColor(0xDDDDDD);
//for each connected client lets get the data being sent and lets print it to the screen
for(unsigned int i = 0; i < (unsigned int)TCP.getLastID(); i++){
if( !TCP.isClientConnected(i) )continue;
//give each client its own color
ofSetColor(255 - i*30, 255 - i * 20, 100 + i*40);
//calculate where to draw the text
int xPos = 15;
int yPos = 80 + (12 * i * 4);
//get the ip and port of the client
string port = ofToString( TCP.getClientPort(i) );
string ip = TCP.getClientIP(i);
string info = "client "+ofToString(i)+" -connected from "+ip+" on port: "+port;
//if we don't have a string allocated yet
//lets create one
if(i >= storeText.size() ){
storeText.push_back( string() );
}
//we only want to update the text we have recieved there is data
string str = TCP.receive(i);
if(str.length() > 0){
storeText[i] = str;
//str = json.asString();
}
//draw the info text and the received text bellow it
ofDrawBitmapString(info, xPos, yPos);
ofDrawBitmapString(storeText[i], 25, yPos + 20);
vector<string> c = ofSplitString(str, ":");
string x = c[0];
string y = c[1];
int a = ofToInt(x);
int b = ofToInt(y);
ofSetColor(0,0,200);
ofCircle(a, b, 40);
//ofSleepMillis(500);
}
}
This is the whole program. Everything runs fine across the socket. I’m getting the string, separated by “:”. It only fails when the string tries to convert to an int.
Yeah, you need to validate the data coming from the socket before you try to split strings, etc. I suspect that what is coming back as str is some sort of invalid data – or you’re asking for data when it isn’t available.
vector<string> c = ofSplitString(str, ":");
string x = c[0];
//string y = c[1];
int a = ofToInt(x);
//int b = ofToInt(y);
ofSetColor(0,0,200);
ofCircle(a, a, 40);
When I only access the first member of the array in this way, everything works. When I try to access both members of the array, with the original code, I get this:
string newStr = str;
string x, y;
vector<string> c = ofSplitString(newStr, ":");
if(newStr.size() > 1){
x = c[0];
y = c[1];
} else {
x = "0";
y = "0";
}
int a = ofToInt(x);
int b = ofToInt(y);
ofSetColor(0,0,200);
ofCircle(a, b, 40);
The issue is that the array wasn’t being filled every time for some reason. If I go through the “if” check, to make sure the array has enough values in it, then everything seems to work fine. The EXEC_BAD_ACCESS error was from trying to access a value that hadn’t been added yet. Anyway, cheers, and thanks for the help!