I've tried a ton of tutorials but none work. Or rather, sooner or later ALL digress and leave the crucial part of transfering a file that isn't pure text.
I'm remodeling an app I've previously used to transfer text and project specific objects over a LAN. I'm just having trouble to receive the file and save it again after the transfer (I assume the sending of the file is successful, but of course have no way to actually verify this).
Here is the code for my client (sending) app:
privatevoid Send(string arg)
TcpClient client = new TcpClient(TB_Host.Text, (int)UD_Port.Value);
FileStream fstream = File.Open(OFD_File.FileName, FileMode.Open);
NetworkStream nstream = client.GetStream();
int data = 0;
while (data > 0)
data = fstream.ReadByte();
And this is the code for the server (receiving) app:
int port = 21112;
byte result = new byte;
IPAddress address = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(address, port);
ipEnd = new IPEndPoint(address, port);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
int f = 0;
object state = new object();
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
f = socket.Receive(result);
Invoke(new UpdateDisplayDelegate(UpdateDisplay), result);
The current problem is that the code never reaches the invoke call (the Listen method is on a 2nd thread), which confuses me as there never had been any problem when I transferred text or objects.
I tried a SoapFormatter, but while it works great for text and objects, I can't get it to work for files.
I also tried using no sockets but a NetworkStream to write into the buffer:
result = ns.Write(result, 0, int.MaxValue);
but I always get an ArgumentOutOfRange exception for the size (I used int.MaxValue) parameter.
Can you tell me how to get it to work? Or is this approach wrong in the first place?
Now why would you handle one byte at a time? it will take forever on medium-sized files, and even longer on large ones.
You are trying to receive 1024 bytes, why not do something similar on the sender side?
Use a byte array, Stream.Read(), and make sure to watch the actual number of bytes transferred.
the code never reaches the invoke call
I tried a SoapFormatter
I have no idea why you would use a formatter. Sockets don't care about the data they carry.
I used int.MaxValue
If the method needs to allocate a buffer, it will fail to get int.MaxValue bytes of memory, no matter how much RAM your system has.
I suggest you try something like this as a sender (not tested!):
byte bytes=new byte[DIM];
int count=fstream.Read(bytes, 0, DIM);
if (count==0) break;
nstream.Write(bytes, 0, count);
Look, if you aren't up to working out what he said, that's your problem. The answer he have was helpful:
Your code couldn't possibly run, was inefficient and poorly designed. He covered each of those points. He even gave you a sample of code that fixes those problems.
I suggest you read the other comments and figure out why you are wrong, rather than attempting flip answers.
All you did was provide code that couldn't possibly work. Luc provided reasons why, and a practical suggestion on how to take your code forwards. Luc's comments were valid, pertinent and usable - if you think his code does the same as yours, I suggest that you need to step through the code line by line. BTW - had you actually bothered to debug your application, you'd have spotted that you could never step into the loop.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
I have a question here: Do you want somebody to do your job or do you want to learn?
If you want somebody to do your job, then look for a professional, ask him to do the job and pay him for it.
If you want to learn then read again Luc's answer and try to apply his advices. Additionally, you might say "Thank you for your time" or nothing at all, but this kind of reply is a great invitation to blacklist you and do never answer any other of your questions.
You have two choices :
- use a basic text editor
- use an IDE
More seriously, as you're new to these forums, there are several rules to follow when you post some questions :
- don't post your question at several places ; rather find the right forum and post it in that place
- post precise questions, show what you have searched/done so far, with pieces of code and error messages relevant to your problem. No one is gonna make google searches for you.
I suggest you first read the forum guidelines before posting.
I find that breaking the problem down into ever smaller problems, and solving those problems all the way down the chain helps. I tend not to start with the code; I'd rather thrash out the requirements first and then break those requirements down into smaller and smaller requirements until I have a good grasp what problem I'm solving. That's how I'd write the program, but then again I'm just a picky professional developer. How would you go about writing it?
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
At work I'd do what Pete suggests: break the problem down into smaller tasks, and break those down further. At some point I'd have a strategy, so I'd start coding.
At home I'd just start coding, get about 6 hours into the problem, then wish I'd done what I would have done, had I been at work.
At university I'd have done what Pete suggests, but I'd make sure it satisfies the lecturer's [programming] peccadilloes, and made it properly documented .
Do you have a Neural Network Yet?
You surely have little experience in programming based on the fact you have posted a question such as this.
If this is an assignment of some sort then you should complain that you have not been taught enough to complete this assignment (unless you skipped those lessons?), or maybe its the first task of your new programming job (which you shouldn't have) - so then I recommend you cough up about your inexperience sooner rather than later
First you have to learn the technique, independently of any programming language. These are two radically different ways of approximating functions, and if it's possible to simulate the series solutions with a neural net, SOMEONE has described this technique SOMEWHERE. Google is your friend (as least for finding information).
I have a datagridview in which there is parent child relationship in rows. I want that If a parent row is checked in the datagrid and any child node corresponding to it is checked then if the user tries to uncheck the Parent Row then the Parent row shouldn't gets uncheck and remains checked.
I have tried to write the code for the same on CELLCONTENTCLICK event and on the CELLVALUECHANGEDEVENT but all in vain
I have a datagridview in which there is parent child relationship in rows. If a parent row is checked in the datagrid and any child node corresponding to it is checked then if the user tries to uncheck the Parent Row then that row doesn't gets uncheck and
it again gets checked on itself.
I've got a bit of code that works nicely, but it's really ugly and it offends my sense of style. To wit,
public partial class Form1 : Form
privatestatic string Header = @"^(D|""D|T|S|I|E|""Z)";
privatestatic string Blank = @"^$";
privatestatic string Total = @"^""To";
privatestatic string Sum = @"^""\d\d/\d\d/\d\d\d\d"","""",";
privatestatic string NewData = @"^""\d\d/\d\d/\d\d\d\d"",";
privatestatic string Data = @"^"""",""\w";
private string SourceFiles = new string;
public string WkgDirectory;
public DataSet MeterReads = new DataSet();
public Regex RgxHeader = new Regex(Header);
public Regex RgxData = new Regex(Data);
public Regex RgxNewData = new Regex(NewData);
public Regex RgxTotal = new Regex(Total);
public Regex RgxBlank = new Regex(Blank);
public Regex RgxSum = new Regex(Sum);
public Queue<string> OutQueue = new Queue<string>();
privateint Classify(string s)
if (m.Success) return0; //Type 0 is a header line
if (m.Success) return1; //Type 1 is a blank line
if (m.Success) return2; //Type 2 is a totals line
if (m.Success) return3; //Type 3 is a summation line
if (m.Success) return4; //Type 4 is a data line to save
if (m.Success) return5; //Type 5 is a data line to save
return99; //Bad data
Mind you, I was fairly drunk when I wrote it, but it works perfectly and I hesitate to change it. But it's still ugly.
I don't like having variables stuck in the Form when they're only used in one method. I'd slide all the declarations down into the Classify() method, but I'm afraid that this would cause them to be created and destroyed every time the method was called. That could really bog things down. My understanding of static methods is a bit fuzzy, but I suspect that if I put all the declarations in the method, and make the method static, this might eliminate the problem. Is that true, or should I just hide all the ugly bits in a region and get along with finishing the app?
No. If there's any way that you can delay the trip a day, do so. Tonight and tomorrow they're supposed to get the worst of it, but by Thursday they roads should be drained, at least. Where are you heading? By what route? The Grapevine (I-5) will be closed by morning, I'm sure, if the temperature drops. And I-15 at Cajon Pass (North of San Bernardino) always closes when the snow starts to fall below 5,000 feet.
it would be good to spend the night in Laughlin...
Hehehe... That might be risky, too. They're calling for tremendous rains here, as well. But the fact is, it's 8:00 and we still haven't seen anything but a light, sporadic drizzle. I did get new wipers, though, just in case.
There is a static Regex.IsMatch(input,pattern) method which could fit the bill, and avoid all class members. I am not sure how it would compare performance wise, I don't expect a significant difference but could be wrong.
Well, for a start, if you want to continue using this, make Classify static. There's no real reason for it to be an instance method. Then, I would consider creating a Dictionary of the patterns. You could rewrite this as:
So the object is being created and destroyed at each call, which is expected I suppose. Whether this is heavy enough to have impact on the performance significantly is another matter. A suck-it-and-see approach is best in these circumstances (assuming performance is an issue in the first place, otherwise I'd just go for the cleanest code).
You can use encryption to protect the data that travels between the terminal server and the terminal services client. If you fear unauthorized interception of the data as it travels between the two, you should enable encryption. RSA RC4 encryption is used; encryption can be set to one of the following three levels:
* High: encrypts both the data sent from client to server and the data sent from server to client using a 128 bit key.
* Medium: encrypts both the data sent from client to server and the data sent from server to client using a 56 bit key if the client is a Windows 2000 or above client, or a 40 bit key if the client is an earlier version.
* Low: encrypts only the data sent from client to server, using either a 56 or 40 bit key, depending on the client version. Useful to protect usernames and passwords sent from client to server.
To change the encryption level, you must be an administrator. In Programs | Administrative Tools, select Terminal Services Configuration and perform these steps:
1. In the left console pane, select Connections.
2. In the right details pane, right click RDP-TCP and select Properties.
3. Click the General tab.
4. Under Encryption level, select the desired level in the drop down box and click OK.