Problems with Sockets

coldfusion244

Well-known member
Joined
Nov 24, 2004
Messages
266
Location
Philadelphia
Sorry HJB, thought I was done but guess not.

Basically what is happening:

I have a server and multiple clients. Data is sent to the server from an outside source that I choose. Once the server digests the information into actual parts, it sends that to certain clients or in cases, all. I have it set to show me what it sends and recieves. The onyl problem I am having is that I have no clue how to control the recieve buffer. I made a global byte for this but I never know when to clear it. I tried it in several different places but no avail. Either it erases the data or keeps the data. Ill post the code if need be, but there is a good bit of it. I have been looking all over for a *good* example of asynchronous sockets, so if you know of one please link it. Thank you to all.

-Sean
 
HJB417 said:
post the code

This isnt vb.net, its C#.net but the CS tags make the code look horrid.
[VB]
private void CheckPackets(IAsyncResult result)
{
try
{
Socket socket = (Socket) result.AsyncState;
int dsocket = socket.EndReceive(result);
if (dsocket<=0)
{
t1.Text += "\r\nNothing found!";
}
else
{
t1.Text +="\r\nGot: " + System.Text.Encoding.UTF8.GetString(rcvd);
Action(System.Text.Encoding.UTF8.GetString(rcvd),socket);

socket.BeginReceive(rcvd,0,rcvd.Length,System.Net.Sockets.SocketFlags.None, new AsyncCallback(CheckPackets),socket);
//rcvd = new byte[65535];
}

}
catch
{
}
}
[/VB]
 
I was hoping to get reproduceable code, but anyways. I gave you an example in another thread where I have an array of objects being stored in the IAsyncResult.AsyncState. You should probably do the same for your receive buffer problem. Have the array of objects store the receive buffer and the socket.
 
HJB417 said:
I made changes to the server code where you call Begin and End Receive.

Once again HJB, you are unsurpassed in brilliance. The program works perfectly now! Since I will be using the client and a few friends of mine as well, Id like to thank you by at least letting me put your name and url in the credits. You deserve much credit and have my appreciation! :D
 
lol, have time for another question that undoubtably you have an answer for?

After the server sends the client a trouble packet, I want to add the information to my tab control. Unfortunately When I try to add a new tab I get the following error "System.ArgumentException: Controls created on one thread cannot be parented to a control on a different thread." I know the sending and recieving is on a different thread, but I dont understand why I cant use data from the other thread. As always here is a link to the source. Here
 
Microsoft said:
By design, Windows Form or Control methods cannot be called on a thread other than the one that created the form or control. If you attempt to do this, an exception is thrown. Depending on the exception handling implemented in your code, this exception may cause your application to terminate. If no exception handling is implemented, the following error message is displayed:
An unhandled exception of type System.ArgumentException occurred in system.windows.forms.dll

Additional information: Controls created on one thread cannot be parented to a control on a different thread.
The exception is raised because Windows Forms are based on a single-threaded apartment (STA) model. Windows Forms can be created on any thread; after they are created, however, they cannot be switched to a different thread. In addition, the Windows Form methods cannot be accessed on another thread; this means that all method calls must be executed on the thread that created the form or control.

Method calls that originate outside the creation thread must be marshalled (executed) on the creation thread. To do this asynchronously, the form has a BeginInvoke method that forces the method to be executed on the thread that created the form or control. The synchronous method call is done with a call to the Invoke method.

Form1.cs for the client

I think your program assumes that when it calls, EndReceive on a socket, it will receive all the data the client sent. This is not true.
e.x.:
Client sends: "1234567890abcdefgABCFEFG";
you can have a situation where the 1st calll to EndReceive returns
"1234"

a 2nd call to EndRecive returns
"567890ab"

a 3rd call to EndRecieve returns

"cd"

etc.

The only thing the TCP socket guarantees is that it will be sent in the right order, and that everything you want to send, will be sent (it will resubmit data if necessary).
 
I did not realize that in the Asynch thread that I couldnt use my forms methods. So I guess now anytime I want to create controls I will need to add a delegate and use the Invoke command, and always pass it an object.
 
coldfusion244 said:
I did not realize that in the Asynch thread that I couldnt use my forms methods. So I guess now anytime I want to create controls I will need to add a delegate and use the Invoke command, and always pass it an object.

anytime I want to create controls from a different thread I will need to add a delegate and use the Invoke command, and always pass it an object.
 

Similar threads

B
Replies
0
Views
90
bagua8
B
D
Replies
0
Views
255
dianas28
D
D
Replies
0
Views
246
dianas28
D
J
Replies
0
Views
59
Jalil Sear [MCPD SharePoint]
J
Back
Top