JMK
JMK

Reputation: 28059

C# - NetworkStream Error - "The operation is not allowed on non-stream oriented sockets."

I am trying to connect to a socket and then Read a NetworkStream.

My code is below:

NetworkStream myNetworkStream;
Socket socket;

socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IPv4);

socket.Connect(IPAddress.Parse("8.8.8.8"), 8888);

myNetworkStream = new NetworkStream(socket);

byte[] buffer = new byte[1024];
int offset = 0;
int count = 1024;

myNetworkStream.Read(buffer, offset, count);

When I try to debug the above I get the following error:

The operation is not allowed on non-stream oriented sockets.

What am I doing wrong?

Thanks

Upvotes: 1

Views: 3788

Answers (2)

Muhammad Hamayoon
Muhammad Hamayoon

Reputation: 61

use this it will work "Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Unspecified)"

Upvotes: 0

harlam357
harlam357

Reputation: 1491

You need to use SocketType.Stream in lieu of SocketType.Raw.

Upvotes: 2

Related Questions