|
I have a small problem to send a collection or array via socket as we know we can only send byte.
This is a small example of what I wanna do but it is not work:
List<string> list = new List<string>();
list.Add("item1");
list.Add("item1");
sockt.Send(list);
I try this small piece of code but I do not received what I need
String username = Console.ReadLine();
String password = Console.ReadLine();
byte[] msg1 = Encoding.ASCII.GetBytes(username);
byte[] msg2 = Encoding.ASCII.GetBytes(password);
List<ArraySegment<byte>> sendBuffers = new List<ArraySegment<byte>>(2);
sendBuffers.Add(new ArraySegment<byte>(msg1));
sendBuffers.Add(new ArraySegment<byte>(msg2));
client.Send(sendBuffers);
List<ArraySegment<byte>> recvBuffers = new List<ArraySegment<byte>>(2);
byte[] username = new byte[10];
byte[] password = new byte[10];
recvBuffers.Add(new ArraySegment<byte>(username));
recvBuffers.Add(new ArraySegment<byte>(password));
try
{
int bytesReceived = sock.Receive(recvBuffers);
}
catch
{
}
sockt.Received(recvBuffers);
Now if in Client enter
toto as username and 123456 as password
in server I received toto123456 no totoand 123456 seperatly
What can I proceed.
|
|
|
|
|
You need to convert your messages into continuous byte streams, rather than sending multiple messages. txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
Shouldn't the client wait for a prompt before sending the data? And perhaps include a carriage return?
|
|
|
|
|
Im trying to search for the tag within a text file,when i found it then i want to delete it, can you please help me to delete the tag.
public partial class Display : System.Web.UI.Page
{
StreamReader myread;
string pic;
string readtxt;
string[] heading = new string[1000];
int count;
protected void Page_Load(object sender, EventArgs e)
{
pic = "<img[^<>]+>";
count = pic.Length;
try
{
string mine = Server.UrlDecode(Request.QueryString["mine"]);
readtxt = Server.MapPath(mine);
using (myread = File.OpenText(readtxt))
{
for (int i = 0; i < 500; i++)
{
do
{
if (!myread.EndOfStream)
{
heading[i] = myread.ReadLine();
lbldisplay.Text = String.Join(" ", heading, 0, 500);
}
}
while(heading[i].Contains(pic));
{
lblTry.Text = count.ToString();
heading[i].Remove(200,count);
im having a broblem here
}
}
}
}
catch(Exception ex)
{
lblerror.Text= ex.Message;
}
}
}
}
|
|
|
|
|
You are having problems all over the place there, my friend!
Firstly, never use "Magic numbers" such as
for (int i = 0; i < 500; i++) I see what you are doing, but don't! Instead, loop around on the EndOfStream or the ReadLine.
In actual fact, unless there is a good reason why you want to process the first 500 lines only, consider using File.ReadAllText instead.
Secondly, it looks like you are trying to use a regular expression, but failing.
Thirdly, unless every single line contains the img tag, you loop terminates on the first non match.
What you want to do is (pseudo code):
Allocate a string to hold the file content, and use File.ReadAllText to read the whole thing. This allows for img tags which span lines.
Set up a Regex to hold your img tag seeker.
Use Regex.Replace to replace all instances of the img tag in the file.
Use String.Split to break your file into the various lines, if you need it afterwards.
No loop required!
If you aren't sure how to use any of these, google for File.ReadAllText etc. and you will get instructions!You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
thanx, i managed to follow your instruction,im still having a problem with removing the . my intention is to display the text that remain after removing the image, so im having a proble with that. here is my code.
public partial class Display : System.Web.UI.Page
{
StreamReader myread;
string pic;
string readtxt;
string contents;
string mycontents;
char mychar;
protected void Page_Load(object sender, EventArgs e)
{
mycontents = "<img+>";
Regex myreg = new Regex(mycontents);
try
{
string mine = Server.UrlDecode(Request.QueryString["mine"]);
readtxt = Server.MapPath(mine);
using (myread = File.OpenText(readtxt))
{
if (!myread.EndOfStream)
{
contents = myread.ReadToEnd();
if (myreg.IsMatch(contents))
{
pic = myreg.Replace(mycontents, " ");
//lbldisplay.Text Need to display the contents without the picture
}
}
}
}
catch (Exception ex)
{
lblerror.Text = ex.Message;
}
}
}
}
|
|
|
|
|
First off, when you post code fragments here, use the "code block" widget - it preserves formatting and make it easier to read.
Next, I think you need to read up a bit on Regular expressions! Get a copy of expresso - it helps you to understand what they are doing, and to create them.
Congratulations BTW on putting exception handling in! Well done - it is so easily forgotten.
protected void Page_Load(object sender, EventArgs e)
{
Regex removeImgTag = new Regex(@"\<img.*?(/>|</img>)", RegexOptions.Singleline);
try
{
string mine = Server.UrlDecode(Request.QueryString["mine"]);
string contents = File.ReadAllText(Server.MapPath(mine));
string myContents = removeImgTag.Replace(contents, " ");
lbldisplay.Text = myContents;
}
catch (Exception ex)
{
lblerror.Text = ex.Message;
}
}
Several changes from yours:
1) Don't make variables class scope unless you need them for the whole class - if they are only needed in a method, they should be local to the method.
2) Use sensible names for variables - it helps later when you will be dealing with lots of them!
3) The RegexOptions.SingleLine specifies that matches can cross line boundaries.
4) You don't need to open a file if you just want to read the whole thing - just use the File.ReadAllText method and it will do all the work.
5) You don't need to check if any matches exist - the Replace method will do that for you.
And voila! The HTML no longer has img tags in it.You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
I forgot to say - you can get Expresso here[^] - it's free and very helpful. You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
|
Hi,
I'm new to C# and have been asked to help out on an existing project, however, the existing code is throwing errors and i have no idea how to fix it.
Its on the following line:
Lock.AcquireReaderLock(Constants.MaxLockWaitInMilliseconds);
Constants.MaxLockWaitInMilliseconds has a value of 5000
The error being thrown is:
Value was either too large or too small for a UInt16.
It runs through this a few times successfully but then suddenly breaks, if i put a try...catch around it, it just keeps looping endlessly.
Sandy-Lee
|
|
|
|
|
Are you sure the error actually occurs on this line? The max value for a UInt16 is 65535 so the value 5000 is well within it. Also the AcquireReaderLock() method takes an Int32 as its input value.txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
hi friends
I have some perl scripts which are on unix machine.
I need to connect to telnet and execute these perl scripts using C#.
please help me for this...
thanksmodified on Wednesday, February 24, 2010 3:59 AM
|
|
|
|
|
Firstly what has this to do with C#?
Secondly what is your problem in using telnet?txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
firstly read the thread,
i need to connect to telnet and execute the perl scripts which are on unix machine using C#
i hope u will understand this
|
|
|
|
|
He's asking what you've done so far. What code have you got? What's not working?"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
// code is as follow, it executes without any exception but nothing happens at unix and i get the response as ??% all time
string message = "./mahesh/touch test_connection1";
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Blocking = true;
socket.Connect("10.111.16.199", 23);
TelnetStream stream = new TelnetStream(socket);
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
socket.Close();
|
|
|
|
|
What happens if you telnet to this normally? Does it prompt you for credentials?"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
thanks for reply....
it doesn't prompt for login..
|
|
|
|
|
l.laxmikant wrote: it doesn't prompt for login.
Then something is wrong with your UNIX setup, telnet should always prompt for a login.txtspeak is the realm of 9 year old children, not developers. Christian Graus
|
|
|
|
|
l.laxmikant wrote: TelnetStream
What namespace is it in?
|
|
|
|
|
I've had to telnet into Unix systems and run some automated stuff before. (Five years ago, with C# 1.0 and .net 1.1)
I found a telnet client class somewhere, I'm not sure where, but probably not on CP. From that, I wrote my own and added the ability for it to execute command scripts -- with my own little scripting language.
It worked just great with only a few minor problems.
Unfortunately, my classes didn't get to a point where I felt I could write an article and publish them. I was actually working on that when I got laid off from that job.
Anyway, you need a telnet client class; just a socket won't do it. Does the current version of .net have a telnet client class? I haven't bothered to look.
P.S. My Telnet client class wraps a TcpClient object.modified on Wednesday, February 24, 2010 4:58 PM
|
|
|
|
|
Have you gotten it working? I got back to looking at my code yesterday and found that it isn't broken so I may be publishing it soon. If you found another class that does it could you let me know so I can check it out too?
|
|
|
|
|
Here[^] is my article on my TelnetSocket. Have a look and let me know what you think.
|
|
|
|
|
Hi,
I am the maintainer for Little Registry Optimizer. My program is getting error code 17 (The system cannot move the file to a different disk drive) whenever it tries to call RegReplaceKey to compact the registry hives. The weird thing about this error is that the old and new hives are both assigned using the same API and they are in the same directory and drive. I have been able to reproduce this error on my friends computer but not on mine. I have attached the error log below and the location of the registry hives on my friends computer. Please let me know if you have any ideas!
This is the code that is causing the error:
int ret = PInvoke.RegReplaceKeyA(this.hKey, null, this.strNewHivePath, this.strOldHivePath);
if (ret != 0)
throw new Win32Exception(ret);
This is the code that assigns the old and new hive paths
string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
if (File.Exists(tempPath))
return GetTempHivePath();
return tempPath;
Error log: http://tinypaste.com/51dde[^]
Registry Hive Paths: http://tinypic.com/r/10pya37/6[^]
Thanks!
|
|
|
|
|
Does this happen all the time on your friends computer?
Do you run as admin on your? On his?
Can you copy a random file to those paths his computer?Opacity, the new Transparency.
|
|
|
|