Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string concatenation problem. 1

Status
Not open for further replies.

luciddream

Programmer
Nov 8, 2000
712
US
i've been having a problem that doesn't make any sense to me. i've been trying to put a variable in a string, but, the string always cuts off at the end of the variable..

i hope someone has had this problem before and can help me out. the variable i'm trying to put in the string is a substring of data that is read from a socket. i've tried everything i can think of to try to get around this problem.. (renaming the variables, trimming it, making a char array out of it then joining it together.. the problem remains.)

this is what my code originally looked like:

Code:
private string uid;

private void doLogin(string com)
{
	uid = com.Substring(1);
	sqlCmd.CommandText = "insert into print_users (cc_index,ip,uid) Values("+index.ToString()+",'"+mySocket.RemoteEndPoint.ToString()+"','"+uid+"')";
	try 
	{
		sqlCmd.ExecuteNonQuery();
	}
	catch(Exception e)
	{
		rtb.Text += e.Message+" from "+mySocket.RemoteEndPoint.ToString()+"\n";
	}
}

the problem is with the sql query.. after uid, it won't concatenate the "')" and i get a sql error.

the code that calls this is :

Code:
public void HandleThread() 
{
	Thread currentThread = Thread.CurrentThread;
        mySocket = myTcpListener.AcceptSocket();
	rtb.Text += "Connection from "+mySocket.RemoteEndPoint.ToString()+"\n";
	while(boo)
	{
		if(mySocket.Available>0)
		{
			byte[] buffer = new byte[1024];
			string command = string.Empty;
			int avail = mySocket.Available;
			int read = mySocket.Receive(buffer,0,avail,SocketFlags.None);
			command = Encoding.ASCII.GetString(buffer);
			rtb.Text += command+"\n";
			switch(command.Trim().Substring(0,1))
			{
				case "K":
					boo = false;
					break;
				case "L":
					doLogin(command);
					break;
				case "A":
					keepAlive();
					break;
				default:
					break;
			}
		}
		else
		{
			Thread.Sleep(1000);
		}
	}
	doLogout();
	mySocket.Shutdown(SocketShutdown.Both);
	mySocket.Close();
	rtb.Text += "Connection Closed\n";
	currentThread.Abort();
}

if anyone can help me, i'd greatly appreciate it.

adam@croem.net
 
Hi,

I don't see anything wrong with the string concatenation line. I think it is more likely your error is coming from reading data from the socket.

There is no guarantee that the socket will contain all the data you are waiting for at the time of the read. You may need to do two receives to get all the data for each request. One way to do it is to have an end of command sequence like the double carriage return for indicating the end of HTTP headers.

For example,

using System.Text.RegularExpressions;

string command = string.Empty;

while(!Regex.Match(command, "\r\n\r\n").Success)
{
byte[] buffer = new byte[1024];
int read = mySocket.Receive(buffer, 0, 1024, SocketFlags.None);

if(read == 0)
break; // Something closed the socket.

command += Encoding.ASCII.GetString(buffer, 0, read);
}

Regards,

Aaron

 
when i'm debugging, all the data i'm expecting is there. it's just the when i send something like "LADAM", the string will end up like this:

insert into print_users (cc_index,ip,uid) Values(1,'127.0.0.1:8456','ADAM

this has been driving me insane.. i've been trying to get it working for 3 days now.. :(

adam@croem.net
 
Try using a StringBuilder to do the concatenation.

Also, like Azarc said, you need to loop around your socket receive, and only concatenate the amount of data that you received. While your network doesn't break your packets up, the customer's network surely will.

One other reason to only concatenate the amount of data is so you don't get any null characters (hexadecimal 0), which might be what is causing your problem.

Chip H.
 
Did you change the line which extracts the string from the buffer?

command += Encoding.ASCII.GetString(buffer, 0, read);

The way you were originally doing it would only work if you read at least 1024 bytes!

Aaron
 
thanks.. that was the problem. i forgot that it was converting the whole array to a string..



adam@croem.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top