Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-29-2011, 09:13 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Exclamation Arranging Byte Array

Hi All,

I have a question that I have been unable to solve for a good week now. I am new to Xcode and cocoa so please excuse my lack of knowledge. Well, I have been trying to send a TCP/IP command to an sqlserver and oracle server at work and I have been successful in sending a receiving data byte array in NSlog. The problem is that when I send the command @"1GA~N" I get an enormous amount of data in one string. The data is organized in a fashion that if for a char int of 14 that is a column of the sqlserver, 15 is a row, and 20 is a table. Now I have no idea how to search the byte array before it is converted to string for characters 14,15, and 20. If anyone could please help I would deeply appreciate it.

Thanks,
Arjun

Last edited by arjunb; 07-29-2011 at 09:58 AM. Reason: re-wording
arjunb is offline   Reply With Quote
Old 07-29-2011, 11:24 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

We can probably help, I'm just not sure what you're asking.

I get your sending a command, maybe something like:

NSData *data = [self SendCommand:@"1GA~N"];

and the data is coming back, but what data is the format in?
QuantumDoja is offline   Reply With Quote
Old 07-29-2011, 02:10 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Post Data

Thank you so much for replying. My program takes the string 1GA~N converts it into bytes and sends it. The server then receives the bytes and sends me data from the SQL database in bytes. My program successfully converts the received bytes back into string and displays it, but it's just one giant string with the names of the tables, columns, and the data within them. The designer of the server has it so if you look for char 14 it is a row separator for example, 15 is a column, etc. I do not know how to read the bytes before it is converted into standard string and look for these special characters and have them actually divide up the string neatly.
arjunb is offline   Reply With Quote
Old 07-29-2011, 02:19 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

Hi, Can you post a snippet of the data you received with at least 2 rows and columns in it?
QuantumDoja is offline   Reply With Quote
Old 07-29-2011, 03:07 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Default Snippet

Quote:
Originally Posted by QuantumDoja View Post
Hi, Can you post a snippet of the data you received with at least 2 rows and columns in it?
Here is the snippet of string.

11RUNNING BEHIND SCHEDULE'07/29/2011''07/29/2011'U21REFUELING'07/29/2011''07/29/2011'U31MECHANICAL BREAKDOWN'07/29/2011''07/29/2011'U41NO FARE'07/29/2011''07/29/2011'U51LOST'07/29/2011''07/29/2011'U61EMERGENCY - NEED HELP'07/29/2011''07/29/2011'U71AFFIRMATIVE'07/29/2011''07/29/2011'U81NEGATIVE'07/29/2011''07/29/2011'U90SUPERVISOR NEEDED'07/29/2011'

I get this and more from the server, but I cannot view characters 14,15, or 20. I basically need to read integers 14, 15, and 20 in bytes and tell the program to divide up the string based on the integer found.
arjunb is offline   Reply With Quote
Old 07-29-2011, 03:15 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Default

Quote:
Originally Posted by arjunb View Post
Here is the snippet of string.

11RUNNING BEHIND SCHEDULE'07/29/2011''07/29/2011'U21REFUELING'07/29/2011''07/29/2011'U31MECHANICAL BREAKDOWN'07/29/2011''07/29/2011'U41NO FARE'07/29/2011''07/29/2011'U51LOST'07/29/2011''07/29/2011'U61EMERGENCY - NEED HELP'07/29/2011''07/29/2011'U71AFFIRMATIVE'07/29/2011''07/29/2011'U81NEGATIVE'07/29/2011''07/29/2011'U90SUPERVISOR NEEDED'07/29/2011'

I get this and more from the server, but I cannot view characters 14,15, or 20. I basically need to read integers 14, 15, and 20 in bytes and tell the program to divide up the string based on the integer found.
If you would like I could post the whole project on media fire. If you find it is a good idea.
arjunb is offline   Reply With Quote
Old 07-29-2011, 04:09 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

Right, lets have a look at this data:

Code:
11RUNNING BEHIND SCHEDULE'07/29/2011''07/29/2011'
U21REFUELING'07/29/2011''07/29/2011'
U31MECHANICAL BREAKDOWN'07/29/2011''07/29/2011'
U41NO FARE'07/29/2011''07/29/2011'
U51LOST'07/29/2011''07/29/2011'
U61EMERGENCY – NEED HELP'07/29/2011''07/29/2011'
U71AFFIRMATIVE'07/29/2011''07/29/2011'
U81NEGATIVE'07/29/2011''07/29/2011'
U90SUPERVISOR NEEDED'07/29/2011'
It looks like, but you need to confirm this:

Unique ID | Status_Text | CreateDate | LastEditDate

Is your data always going to be in this format?
QuantumDoja is offline   Reply With Quote
Old 07-29-2011, 04:15 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Default

Quote:
Originally Posted by QuantumDoja View Post
Right, lets have a look at this data:

Code:
11RUNNING BEHIND SCHEDULE'07/29/2011''07/29/2011'
U21REFUELING'07/29/2011''07/29/2011'
U31MECHANICAL BREAKDOWN'07/29/2011''07/29/2011'
U41NO FARE'07/29/2011''07/29/2011'
U51LOST'07/29/2011''07/29/2011'
U61EMERGENCY – NEED HELP'07/29/2011''07/29/2011'
U71AFFIRMATIVE'07/29/2011''07/29/2011'
U81NEGATIVE'07/29/2011''07/29/2011'
U90SUPERVISOR NEEDED'07/29/2011'
It looks like, but you need to confirm this:

Unique ID | Status_Text | CreateDate | LastEditDate

Is your data always going to be in this format?
It is all in one format and that is the correct format.
arjunb is offline   Reply With Quote
Old 07-29-2011, 04:32 PM   #9 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Default

Quote:
Originally Posted by arjunb View Post
It is all in one format and that is the correct format.
This is the code used for android:

private final String colSep = "" + (char) 14;
private final String rowSep = "" + (char) 15;

I have no idea how to port this to iPhone.

Last edited by arjunb; 07-29-2011 at 04:34 PM. Reason: grammar mistake
arjunb is offline   Reply With Quote
Old 07-30-2011, 09:00 AM   #11 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Default Android

Quote:
Originally Posted by QuantumDoja View Post
Could you post the android code? It might be easier just to port that.
Ok, I will post it later today. I sent an email request for the whole android code.

Thank you.
arjunb is offline   Reply With Quote
Old 07-30-2011, 09:21 AM   #12 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 10
arjunb is on a distinguished road
Default

Quote:
Originally Posted by arjunb View Post
Ok, I will post it later today. I sent an email request for the whole android code.

Thank you.

Here is the code

Code:
private int initPdaItmsAvlData_WaitTime = 15 * 1000;

	 private int exitSyncWaitTime = 2;
 
	private int pingNetworkServer = 4;




	 private boolean justBeforeExit_NowExit = false;
 
	private String justBeforeExit_TaskTypeMsg = "";

	 private boolean manifestLoaded = false;
 
	private boolean waitingForDriverLogin = false;

	 private boolean loadingFirstManifest = true;
 
	private String startupType = "";

	 private String autoShutdown = "N";
 
	private int[] dbCount;

	 // private static String serverIP = "";

	 // private static String serverIP_Alt = "";
 
	// private static int serverPort = 18057;

	 // private static int tcpReceiveTimeOut = 30000; //30 seconds
 
	// private static int tcpSendTimeOut = 30000; //30 Seconds

	 // private static int sendRecvBufferSize = 32768;
 



	private int totalBytesSent = 0;

	 private int totalBytesRecv = 0;
 



	private String travelDate = "";

	 private String travelDateSqlServer = "";
 
	private String segmentId = "";

	 private String driverId = "";
 
	private String vehicleId = "";

	 private String cmdLine = "";
 
	private String oraSyncVer = "";

	 private String oraSyncFileName = "";
 
	// private static String oraSyncLogFileName = "C:\ITMS\LogFiles\OraSync_";

	 // private static String modemWatcherFileName = "C:\Program Files\Sierra
 
	// Wireless Inc\3G Watcher\Watcher.exe";

	 private String onExitKillModemFileName = "N";
 
	private int modemWatcherPID = 9999;




	 private String tracingOption = "on";
 
	private String initPdaDb = "N";

	 private String loadClients = "N";
 
	private String showNetworkErrMsgBallon = "N";

	 private int archivePurgeDays = 15;
 



	private boolean checkforDoneFlag = false;

	 private boolean itms_Synchronize_Trip_Act_DoneFlag = false;
 
	private boolean itms_Synch_Rem_Dispatch_DoneFlag = false;

	 private boolean itms_Synchronize_Trip_Xfr_DoneFlag = false;
 
	private boolean itms_Synch_Driver_Comm_DoneFlag = false;

	 private boolean itms_Synchronize_Trips_DoneFlag = false;
 
	private boolean itms_Synch_Rem_Avl_Data_DoneFlag = false;

	 private boolean dumpOldPdaDataToRemote_DoneFlag = false;
 



	private final String colSep = "" + (char) 14;

	 private final String rowSep = "" + (char) 15;
 



	private final int tcpReceiveTimeOut = 120000;

	 private final int sendRecvBufferSize = 64000;
 
	private final String serverIP = "173.162.209.121";

	 private final String serverIP_Alt = "173.162.209.121";
 
	private final int serverPort = 8765;

	private byte[] GetSendServerData(String theCmd, String theData) {
 
		Socket aTcpClient = null;
 
		final int aByteBuffSize = 8192;

		 InputStream in;

		 OutputStream out;

		 String aServerIPUsed = serverIP;

		 boolean aConnectedToServer = false;

		 String aErrConnStr = "";







		 byte[] aBytesRecv = null;







		 for (int i = 0; i < 2; i++) {

			 if (i >= 0 && i < 1)

				 aServerIPUsed = serverIP;

			 else

				 aServerIPUsed = serverIP_Alt;

			 aErrConnStr = "Remote Server [" + aServerIPUsed + ":" + serverPort

					 + "] Not Accessible!!!";




			 try {




 				boolean loadingFirstManifest = false;

				 if (loadingFirstManifest && theCmd == "4G~") {
 
					aServerIPUsed = serverIP_Alt;




				 }




				 // TCP Client uses 19~20 seconds for Connection timeout

				 aTcpClient = new Socket(serverIP, serverPort);

				 aTcpClient.setTcpNoDelay(true);
 
				aTcpClient.setSoTimeout(tcpReceiveTimeOut);

				 aTcpClient.setReceiveBufferSize(sendRecvBufferSize);
 
				aTcpClient.setSendBufferSize(sendRecvBufferSize);

				 if (aTcpClient.isConnected()) {
 
					aConnectedToServer = true;

					 break;

				 }
 
			} catch (Exception e) {

				 aConnectedToServer = false;

				 System.out.print(e.toString());

			 }

		 }

		 System.out.println("Hey this is initTCP1");

		 try {

			 if (aConnectedToServer) {

				 in = aTcpClient.getInputStream();

				 out = aTcpClient.getOutputStream();

				 // aBufferedStream = new BufferedStream(aNetworkStream,
 
				// aByteBuffSize);

				 out.flush();

				 byte[] aSendBytes;
 
				if (theData.length() > 0 && theData != null)

					 aSendBytes = (theCmd + theData).getBytes();
 
				else

					 aSendBytes = theCmd.getBytes();




				 out.write(aSendBytes, 0, aSendBytes.length);

				 out.flush();

				 aBytesRecv = ReadClientRequestDataFully(in, 0);



 









				in.close();

				 out.close();

 				aTcpClient.close();

			 }

		 } catch (Exception e) {
 
			System.out.println("Exception in GetSendServerData:    " + e.toString());

		 }
 



		return aBytesRecv;

	 }




	 private byte[] ReadClientRequestDataFully(InputStream in, int theInitLength) {

		 // If we've been passed an unhelpful initial length, just

		 // use 32K.

 		if (theInitLength < 1) {

			 theInitLength = 8192;

		 }
 



		byte[] aDataBuffer = new byte[theInitLength];

		 int aBytesRead = 0;
 



		int aChunk;




		 try {

			 // while ((aChunk = theStream.Read(aDataBuffer, aBytesRead,

			 // aDataBuffer.Length - aBytesRead)) > 0)

			 while ((aChunk = in.read(aDataBuffer, aBytesRead,
 
					aDataBuffer.length - aBytesRead)) > 0) {

				 aBytesRead += aChunk;


 

				if (aBytesRead == aDataBuffer.length) {

					 int aNextByte = in.available();
 



					// End of stream? If so, we're done

					 if (aNextByte == 0) {
 
						return aDataBuffer;

					 }




					 // Nope. Resize the buffer, put in the byte we've just

					 // read, and continue

					 byte[] aNewBuffer = new byte[aDataBuffer.length * 2];
 
					for (int i = 0; i < aDataBuffer.length; i++) {

						 aNewBuffer[i] = aDataBuffer[i];
 
					}

					 // aNewBuffer[aBytesRead] = (byte) aNextByte;

					 aDataBuffer = aNewBuffer;

					 // aBytesRead++;

				 }

			 }
 
		} catch (Exception e) {

			 System.out.println(e.toString());

			 return null;

		 }




		 // Buffer is now too big. Shrink it.

		 byte[] aRetBuff = new byte[aBytesRead];

		 for (int i = 0; i < aRetBuff.length; i++) {

			 aRetBuff[i] = aDataBuffer[i];
 
		}

		 return aRetBuff;

	 }
 



	private String ByteArrayToString(byte[] aByte) {

		 String temp = "";
 
		for (int i = 0; i < aByte.length; i++) {

			 temp += (char) aByte[i];

		 }

		 return temp;

	 }
arjunb is offline   Reply With Quote
Reply

Bookmarks

Tags
arranging, array, byte, command, tcp/ip

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 393
14 members and 379 guests
7twenty7, blasterbr, buggen, chiataytuday, dre, fiftysixty, HemiMG, jimmyon122, jonathandeknudt, LEARN2MAKE, nyoe, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,902
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:02 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0