Where Is It From?

C

Chan

Tring to use Sockets, following examples from online doc. Got his
error for

Async_Send_Receive

The type or namespace name 'Async_Send_Receive' could not be found
(are you missing a using directive or an assembly reference?)

I can not find it anywhere from the library.

Anyone know where is it from?

FYI, here are part of the code:

IPEndPoint ipe = new IPEndPoint(host, 8080);
Socket newSocket = new Socket(ipe.AddressFamily, SocketType.Stream,
ProtocolType.Tcp);
newSocket.BeginConnect(ipe, new
AsyncCallback(Async_Send_Receive.Connect_Callback), newSocket);
 
J

Jon Skeet [C# MVP]

Chan said:
Tring to use Sockets, following examples from online doc. Got his
error for

Async_Send_Receive

The type or namespace name 'Async_Send_Receive' could not be found
(are you missing a using directive or an assembly reference?)

I can not find it anywhere from the library.

Anyone know where is it from?

FYI, here are part of the code:

IPEndPoint ipe = new IPEndPoint(host, 8080);
Socket newSocket = new Socket(ipe.AddressFamily, SocketType.Stream,
ProtocolType.Tcp);
newSocket.BeginConnect(ipe, new
AsyncCallback(Async_Send_Receive.Connect_Callback), newSocket);

It's not a complete sample. The idea is that you'd provide your own
callback. One of the problems (IMO) with the MSDN examples is that
they're *not* complete. I'd like to see a short but complete app for
every example...
 
H

H Chan

You are absolutely right that many of the examples of the online doc are
not complete. Here is the C# from "Socket Class" of .NET Framework lib.
----------------------------
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

public class mySocket
{
private static Socket connectSocket(string server, int port)
{
Socket s = null;
IPHostEntry iphe = null;


try
{
// Get host related information.
iphe = Dns.Resolve(server);


// Loop through the AddressList to obtain the supported
AddressFamily. This is to avoid
// an exception to be thrown if the host IP Address is not
compatible with the address family
// (typical in the IPv6 case).
foreach(IPAddress ipad in iphe.AddressList)
{
IPEndPoint ipe = new IPEndPoint(ipad, port);

Socket tmpS =
new Socket(ipe.AddressFamily, SocketType.Stream,
ProtocolType.Tcp);

tmpS.Connect(ipe);

if(tmpS.Connected)
{
s = tmpS;
break;
}
else
continue;
}
}

catch(SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
return s;

}


// This method requests the home page content for the passed server.
// It displays the number of bytes received and the page content.
private static string socketSendReceive(string server, int port)
{
//Set up variables and String to write to the server.
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;

// Create a socket connection with the specified server and port.
Socket s = connectSocket(server, port);

if (s == null)
return ("Connection failed");

// Send request to the server.
s.Send(ByteGet, ByteGet.Length, 0);


// Receive the server home page content.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);



// Read the first 256 bytes.
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);


while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}

return strRetPage;
}

public static void Main(string[] args)
{
string host;
int port = 80;

if (args.Length == 0)
// If no server name is passed as argument to this program,
use the current
// host name as default.
host = Dns.GetHostName();
else
host = args[0];


string result = socketSendReceive(host, port);

Console.WriteLine(result);
}

}
--------------------------

Notice, its VB.NET code has an extra:

Imports Microsoft.VisualBasic

So, Async_Send_Receive might the intrinsic VB stuff, I haven't have time
to explorer into it yet.

Thanks.
 
H

H Chan

Jon,

Please forget about the previous code example, it should be from
"Socket.BeginSend Method"

----------------------------
allDone.Set();
Socket s = (Socket) ar.AsyncState;
s.EndConnect(ar);
StateObject so2 = new StateObject();
so2.workSocket = s;
byte[] buff = Encoding.ASCII.GetBytes("This is a test");
s.BeginSend(buff, 0, buff.Length,0,
new
AsyncCallback(Async_Send_Receive.Send_Callback), so2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top