How can I call this?

T

trint

How can I call this?
This doesn't work:
SendReceiveTest4();

public static int SendReceiveTest4(Socket server)
{
byte[] msg = Encoding.UTF8.GetBytes("This is a test");
byte[] bytes = new byte[256];
try
{
// Blocks until send returns.
int i = server.Send(msg, 0, msg.Length, SocketFlags.None);
Console.WriteLine("Sent {0} bytes.", i);

// Get reply from the server.
server.Receive(bytes, 0, server.Available, SocketFlags.None);
Console.WriteLine(Encoding.UTF8.GetString(bytes));
}
catch (SocketException e)
{
Console.WriteLine("{0} Error code: {1}.", e.Message,
e.ErrorCode);
return (e.ErrorCode);
}
return 0;
}

Thanks,
Trint
 
P

Peter Jausovec

Hi,

You have to pass the Socet -
Socket mySocket = new Socket (...);
int retValue = SendReceiveTest4(mySocket);
 
T

Trint Smith

Peter,
Socket mySocket = new Socket (System.IntPtr.Zero);<<here
What do I put here (...)?
I get some type of error code no matter what I put:
"CS0122: 'System.Net.Sockets.Socket.Socket(System.IntPtr)' is
inaccessible due to its protection level."

Here is my code before that:
private void button12_Click(object sender, System.EventArgs e)
{
IPHostEntry ipHostInfo = Dns.Resolve("NPIFA21AE");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipAddress,515);
TcpClient s = new TcpClient();
//IPAddress Send = ipHostInfo.AddressList[0];

try
{
s.Connect(ipe);
}
catch(ArgumentNullException ae)
{
Console.WriteLine("ArgumentNullException : {0}", ae.ToString());
// listBox1.Items.Add("ArgumentNullException : {0}", ae.ToString());
}
catch(SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
// listBox1.Items.Add("SocketException : {0}", se.ToString());
}
catch(Exception se)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
// listBox1.Items.Add("Unexpected exception : {0}", e.ToString());
}
//
// byte[] msg = System.Text.Encoding.ASCII.GetBytes("This is a test");
// int bytesSent = s.Send(msg);
//
// byte[] bytes = new byte[1024];
// int bytesRec = s.Receive(bytes);
// Console.WriteLine("Echoed text = {0}",
// System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRec));

Socket mySocket = new Socket (System.IntPtr.Zero);
int retValue = SendReceiveTest4(mySocket);
}
Thanks for any help,
Trint


.Net programmer
(e-mail address removed)
 

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