|
|
This post looks like continuation of your post yesterday in the Windows forms forum. It is still top most over there.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
probably there are small numbers of users
ok and can you answer me?
C# Developer
|
|
|
|
|
How to plug/unplug USB serial port problematically?
I am facing some strange issue with my USB serial port.
I want to unplug and plug the USB Serial port Programitically, as i need to refresh the connection between system and hardware connected.Kindly help.......
Thank in advance for the great programmers
|
|
|
|
|
...
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
I agree that i cross post, but the reason behind is, i am not particular about the language used for my problem.I am only looking for the solution and it will be great if you have anything let me know.
|
|
|
|
|
Still not a reason to spam the forums as you have
only two letters away from being an asset
|
|
|
|
|
try this using devcon.exe . Its get from
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272
Run this exe using process class ... have lot of commanding is there.. i thing its useful for u
Thank u
|
|
|
|
|
If i used crystal report in my project login failed error is has been generated.The error asking for username and password but i am not given any username and password to sql server.It is not giving me problem when i am doing my insert update queries. It is only shows error when i use crystal report . Is their any external connection for crystal report.Please give me solution urgently.
|
|
|
|
|
Hello,
First of all thanks for reading this. I am aiming to create a proxy server to overcome some authentication problems. The idea is to have the server handling the requests to a certain port, appending the right credentials, sending the authenticated request to the desirable address, and giving back the response (bytes) to the client.
For that I created two sockets: a listening socket and a client socket for which I show here, the class definition.
As I have a large volume of information on the requests, I am trying to use asynchronous sockets, so that I wont block the system waiting for an answer.
The RequestState class contains the definitions for the state of the request, including a buffer.
The process results in a serious of callbacks and the order of events is more or less this: the BeginGetResponse calls the RespCallback, where the webrequest to the desirable page is thrown, and it activates the BeginRead, that calls a ReadCallback. The readCallback fills the buffer and then I can either call a synchronous blocking send request (which does not work on IE, for e.g.) or call BeginSend with a callback function. The BeginSend also does not work, since the callback (finish) terminates without actualy sending all the information.
Any tips about what I'm doing wrong?
Thanks in advance,
Jo
using System;
using System.Net;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Threading;
using System.Text;
namespace TEST
{
public class RequestState
{
public int BufferSize = 1024;
public byte[] RequestData;
public byte[] BufferRead;
public WebRequest Request;
public Stream ResponseStream;
public WebResponse Response;
public Socket aSocket;
public RequestState()
{
BufferRead = new byte[BufferSize];
RequestData = new byte[0];
Request = null;
aSocket = null;
ResponseStream = null;
}
}
public delegate void DestroyDelegate_(ProxyClient client);
public class ProxyClient : IDisposable
{
const string myUri="http://DEV309.Dev.cadcorp.net:4326/KmlService/Kml.serv?REQUEST=GetCapabilities";
public ProxyClient(Socket ClientSocket, DestroyDelegate_ Destroyer, IPEndPoint MapTo, CredentialCache credCache)
{
this.ClientSocket = ClientSocket;
this.Destroyer = Destroyer;
this.MapTo = MapTo;
this.m_credCache = credCache;
this.m_sendDone = new ManualResetEvent(true);
}
private IPEndPoint MapTo
{
get
{
return m_MapTo;
}
set
{
if (value == null)
throw new ArgumentNullException();
m_MapTo = value;
}
}
internal Socket ClientSocket
{
get
{
return m_ClientSocket;
}
set
{
if (m_ClientSocket != null)
m_ClientSocket.Close();
m_ClientSocket = value;
}
}
public void Dispose()
{
try
{
ClientSocket.Shutdown(SocketShutdown.Both);
}
catch { }
if (ClientSocket != null)
ClientSocket.Close();
ClientSocket = null;
if (Destroyer != null)
Destroyer(this);
}
private void ReadCallBack(IAsyncResult asyncResult)
{
try
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Stream responseStream = myRequestState.ResponseStream;
int read = responseStream.EndRead(asyncResult);
if (read > 0)
{
byte[] newBuffer = new byte[myRequestState.RequestData.Length + read];
Buffer.BlockCopy(myRequestState.RequestData, 0, newBuffer, 0, myRequestState.RequestData.Length);
Buffer.BlockCopy(myRequestState.BufferRead, 0, newBuffer, myRequestState.RequestData.Length, read);
myRequestState.RequestData = newBuffer;
IAsyncResult asynchronousResult =
responseStream.BeginRead(myRequestState.BufferRead, 0, myRequestState.BufferSize,
new AsyncCallback(ReadCallBack), myRequestState);
}
else
{
if (myRequestState.RequestData.Length > 0)
{
myRequestState.aSocket.BeginSend(myRequestState.RequestData, 0, myRequestState.RequestData.Length,SocketFlags.None, new AsyncCallback(Finish), myRequestState);
}
}
}
catch (SocketException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
Dispose();
}
}
private void Finish(IAsyncResult asyncResult)
{
try
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;
Socket mySocket = myRequestState.aSocket;
int Ret = mySocket.EndSend(asyncResult);
if (Ret <= 0)
{
Dispose();
myRequestState.ResponseStream.Close();
}
}
catch (SocketException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
Dispose();
}
}
private void RespCallback(IAsyncResult asynchronousResult)
{
try
{
RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
WebRequest myWebRequest1 = myRequestState.Request;
myRequestState.Response = myWebRequest1.EndGetResponse(asynchronousResult);
Stream responseStream = myRequestState.Response.GetResponseStream();
myRequestState.ResponseStream = responseStream;
IAsyncResult asynchronousResultRead =
responseStream.BeginRead(myRequestState.BufferRead, 0, myRequestState.BufferSize,
new AsyncCallback(ReadCallBack), myRequestState);
}
catch (SocketException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
Dispose();
}
}
public void StartHandshake()
{
try
{
WebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
request.Credentials = m_credCache;
RequestState myRequestState = new RequestState();
myRequestState.Request = request;
myRequestState.aSocket = ClientSocket;
IAsyncResult r = (IAsyncResult)request.BeginGetResponse(
new AsyncCallback(RespCallback), myRequestState);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
Dispose();
}
}
private CredentialCache m_credCache;
private DestroyDelegate_ Destroyer;
private Socket m_ClientSocket;
private IPEndPoint m_MapTo;
private ManualResetEvent m_sendDone;
}
|
|
|
|
|
I didn't debug the code, but I see you used a lot of async calls. Do you really need that much of async calls?
I will explain: If I am not wrong, every async call will use a ThreadPool thread. So, on the first callback you will be in a thread-pool thread (instead of the main one). Generating a new async callback will only use a new Thread to do the parallel work, for something that is already in parallel.
So, maybe you can simplify everything simple doing everything "sinchronously" inside a ThreadPool thread, by using a ThreadPool.QueueUserWorkItem. I think this way the code will get more readable, you will make it work and also will be faster, as it will use only one thread instead of 3 or 4 threads to finish the work.
Also, for the read (not the write) note that you may ask to read 100 bytes and the read can read 3 bytes and return 3. This way, if you REALLY need that 100 bytes, you will need to call read again, as many times as needed or even throw an exception if the connection is closed before it finishes.
|
|
|
|
|
Thanks for the advice! That was a really good suggestion.
I decided that I didnt need such a complicated solution; I didnt need one asynchronous listening socket and an asynchronous client socket;
In fact, all I need is a listening socket that is asynchronous (otherwise it will block the main thread) and I can stream the request in the same class (multithreading is not really an issue, since I wont have multiple requests to the server); I also found it much more convenient to use the HttpListener class;
Here is the code I ended up with:
using System;
using System.Text;
using System.Net;
using System.IO;
namespace TEST
{
public delegate void delReceiveWebRequest(HttpListenerContext Context);
public class ProxyServer
{
private int m_port = 47809;
private static string localhost = "127.0.0.1";
const int BUFFER_SIZE = 1024;
public string m_server;
protected HttpListener m_listener;
private CredentialCache m_credCache;
public event delReceiveWebRequest ReceiveWebRequest;
public ProxyServer(string server, CredentialCache credCache)
{
this.m_server = server;
this.m_credCache = credCache;
}
public string Address()
{
return "http://" + localhost + ":" + m_port.ToString() + "/";
}
public bool Start()
{
try
{
this.m_listener = new HttpListener();
this.m_listener.Prefixes.Add(Address());
this.m_listener.Start();
IAsyncResult result = this.m_listener.BeginGetContext(new AsyncCallback(WebRequestCallback), this.m_listener);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
System.Diagnostics.Debug.WriteLine("Could not connect on port " + m_port.ToString() + "!");
m_port++;
return false;
}
System.Diagnostics.Debug.WriteLine("Listening socket on port " + m_port);
return true;
}
public void Stop()
{
if (m_listener != null)
{
this.m_listener.Close();
this.m_listener = null;
System.Diagnostics.Debug.WriteLine("Proxy stopped on port " + m_port.ToString() + "!");
}
}
protected void WebRequestCallback(IAsyncResult result)
{
if (this.m_listener == null)
return;
HttpListenerContext context = this.m_listener.EndGetContext(result);
this.m_listener.BeginGetContext(new AsyncCallback(WebRequestCallback), this.m_listener);
if (this.ReceiveWebRequest != null)
this.ReceiveWebRequest(context);
this.ProcessRequest(context);
}
protected virtual void ProcessRequest(HttpListenerContext Context)
{
string uri=Context.Request.RawUrl;
this.m_server = this.m_server.Remove(this.m_server.Length - 1);
string address = this.m_server + uri;
WebRequest request = (HttpWebRequest)WebRequest.Create(address);
request.Credentials = m_credCache;
StringBuilder RequestData = new StringBuilder(String.Empty);
WebResponse myWebResponse = request.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[BUFFER_SIZE];
int count = readStream.Read(read, 0, BUFFER_SIZE);
HttpListenerResponse response = Context.Response;
System.IO.Stream output = response.OutputStream;
while (count > 0)
{
String str = new String(read, 0, count);
output.Write(encode.GetBytes(str), 0, encode.GetBytes(str).Length);
count = readStream.Read(read, 0, BUFFER_SIZE);
}
output.Close();
}
}
}
|
|
|
|
|
How much data are you receiving from the web request and sending over the socket? I've had problems doing the same thing before where the web service returned too much data to send at once.
joana.simoes wrote: the callback (finish) terminates without actualy sending all the information
What indicates this? Does mySocket.EndSend(asyncResult) throw an exception, return less than you expected or does something else happen? If it simply returns less than you expected you could try sending the rest of the data again.
|
|
|
|
|
hi
i have an app.config file that should be encrypted. is there any way to encrypt it?
this file will be update freuqently . is there any way that encrypt data with less overhead?
thanks
|
|
|
|
|
This[^] article details how to update the config file. I'd be wary of storing frequently updated values in a config file though - that's not what the config file is fore.
"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
|
|
|
|
|
hi
i want to lock some text file in the specified directory
just two applications can access these files
1) first application for reading and writing
2) the second one just for reading
what should i do?
thanks in advance
|
|
|
|
|
reza assar wrote: i want to lock some text file in the specified directory
just two applications can access these files
Locking a file based on the application that's used for opening it is not supported by the file systems that I know.
reza assar wrote: what should i do?
Review the specifications and research alternative solutions. Here's some to get you started;
* Is it necessary to 'lock' the file, or are you trying to prevent other people from reading the file? If the latter is the case, then try encrypting your text before you send it to the file.
* Could a container be used? You can store multiple documents in a zipfile, and protect them with a password.
* If speed is more important than having a textfile, consider using a dekstop-database like SQL CE.
HTH
I are Troll
|
|
|
|
|
i what to prevent users from "writing" and "reading" !!!! one application grant to write it and my application grant to read it but prevent other users from writing is more more important that reading! it may damage my application information
is there any other way?
|
|
|
|
|
reza assar wrote: is there any other way?
So you're just trying to hide what's in the file from the user? In that case, go for zipfiles, a database or plainly encrypt the data.
No other methods that come to mind right now.
I are Troll
|
|
|
|
|
Hi, I am fetching the recored with a sql procedure, which returns a collection of values with the help of ADO.NET Entity framework. Now if I need to use stored procedure then I have to import that procedure in my .edmx file and map with a entity.
Now the problem is that, I am fetching the records with inner join that's why it can not be map any single Entity. Is it compulsory to map the sotred procedure to an Entity? Can we use stored procedure without mapping with an Entity ?
Pankaj
|
|
|
|
|
You can create an entity in the designer that maps to multiple tables then map the stored proc to it. Otherwise, yes you can use stored procs. Check here[^] for some use extensions and samples.
only two letters away from being an asset
|
|
|
|
|
hi
i want to develop an application that , user could not execute more than one instance at a time
for example my application name is: "APP" . if some one click APP icon it will be run if there is no another instance running at the current machine
what should i do?
thanks in advance!
|
|
|
|
|
Take a look at Mutex[^] class.
Best wishes,
Navaneeth
|
|
|
|
|
You need to be very careful when you are using globally available mutexes running on limited OS accounts.
Maybe finding all processes with the same name like the current will be better and safer solution because doesn't require any special permissions.
Life is a stage and we are all actors!
|
|
|
|
|
Hristo Bojilov wrote: You need to be very careful when you are using globally available mutexes running on limited OS accounts
What problems are you seeing?
Best wishes,
Navaneeth
|
|
|
|