Runtime: Can't locate TcpClient and TcpListener from Socket.Net.Sockets

Created on 6 Feb 2016  Â·  27Comments  Â·  Source: dotnet/runtime

Hi .NET Core,

I like the direction of this project. It's fun to watch and see everything evolve..

Trying to upgrade an existing 4.5.1 project to .NET core on windows 10 and VS2015. It uses lots of TcpClient and TcpListener. From searching github repo, it appears like it's in the System.Web.Sockets package. However, these classes aren't visible to me on public nuget.

Including the package:

image

Checking object browser:

image

What am I doing wrong? Where can I find TcpClient and TcpListener?

Many thanks

area-System.Net

Most helpful comment

@vishal1912 Use client.Dispose();.

All 27 comments

You need to select the 4.1.* packages of System.Net.Sockets. The 4.0.* packages have a smaller API surface area. Also, the NuGet org feed doesn't have the 4.1.* packages yet as stable but there are pre-release versions.

Example:
http://www.nuget.org/packages/System.Net.Sockets/4.1.0-beta-23516

This NuGet package has the 4.1 System.Net.Sockets API surface which includes classes like TcpClient.

Thanks @davidsh . That's good to know. I tried install-package with -pre flag, and I get the same thing. Also, looks like I had 23516 installed already, unless project.json needs to show -pre in some way?

image

Cheers

@CVertex you need to target dotnet5.5 if you want to use TcpClient and friends.
dotnet5.5 not supported on UWP now, but with next release of tools UWP will support dotnet5.6 which will be compatible with dotnet5.5.

That is not correct. System.Net.Sockets 4.1 contract is supported now for UWP apps:

See src\System.Net.Sockets\ref\System.Net.Sockets.csproj:

c# <PropertyGroup> <AssemblyVersion>4.1.0.0</AssemblyVersion> <OutputType>Library</OutputType> <PackageTargetFramework>dotnet5.4</PackageTargetFramework> <NuGetTargetMoniker>.NETPlatform,Version=v5.4</NuGetTargetMoniker> </PropertyGroup>

cc: @CIPop @ericstj @stephentoub

@davidsh just create project using Class library (Universal Windows) template, install System.Net.Sockets 4.1 and try to use TcpClient.

just create project using Class library (Universal Windows) template, install System.Net.Sockets 4.1 and try to use TcpClient.

What happens if you try to create a UWP itself (not a class library)?

cc; @SidharthNabar

Also, what version of Visual Studio are you using? 2015? And did you install Update 1 or something else?

The intent is that System.Net.Sockets 4.1 contract should work on both dnxcore50 targets (ASP.NET Core 1.0 previously known as ASP.NET v5) and netcore50 targets, i.e. UWP Store Apps for Windows 10.

It's possible that creating a "Class library (Universal Windows)" is actually creating a portable library where the intersection of dependencies is different and thus only 4.0 contract will work. But I'm sure that was intended to behave like that.

Also, what version of Visual Studio are you using? 2015? And did you install Update 1 or something else?

I'm using VS 2015 Update 1.

What happens if you try to create a UWP itself (not a class library)?

Same thing - TcpClient not available.

@davidsh is right - we had intended System.Net.Sockets v4.1 to work on UWP in its current version. @ericstj - do you know what the underlying issue could be that is preventing Sockets 4.1 from working in UWP?

Thanks for looking into this.. I'm keen to get my project migrated. Any ETA?

This is not working for me in a class library against dotnet5.4, with Visual Studio 2015 Update 2. I have 4.1.0-beta-23516 referenced. How can I debug this further?

You will need to wait until the RC2 packages are released to NuGet sometime next week. When that happens, this scenario should work.

OK. Is there something I can do in the meantime to test this locally?

You can use the most recent dev packages from the CoreFx MYGET dev feed:
https://dotnet.myget.org/gallery/dotnet-core

You'll need NuGet version 3.4 but since you installed Visual Studio 2015 Update 2, you probably already have that. Just make sure to update all your Visual Studio extensions.

I could do with some help here. my project.json looks like this ...

{
  "supports": {
    "net46.app": {},
    "uwp.10.0.app": {},
    "dnxcore50.app": {}
  },
  "dependencies": {
    "Microsoft.NETCore": "5.0.0",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.0",
    "System.Net.Sockets": "4.1.0-beta-23516"
  },
  "frameworks": {
    "dotnet": {  }
  }
}

For some reason I get the exception "System.Net.Sockets 4.1.0-beta-23516 is not compatible with .NETPlatform,Version=v5.0."

Could someone tell me what I did wrong?
This seems to suggest it should be compatible ...
https://dotnet.myget.org/feed/dotnet-core/package/nuget/System.Net.Sockets
Unless I missed something?

I recommend you wait until RC2 goes live later this month. There have been a lot of changes in the packages and tooling (dnxcore50 isn't valid anymore for example).

Could someone tell me what I did wrong?

Short summary: It doesn't work properly with the 'beta' packages. You didn't do anything wrong.

Ah cool, I think I have it working, i changed the ref to the sockets assembly to ...
"System.Net.Sockets": "4.0.0"
That appears to have fixed it.

I'm just refactoring the code from what is basically the current documented version of an async socket client.
Sorta liking it so far but im only in to the receive loop :)

using the prerelease after seeing this, but the usual TcpClient client = new TcpClient.Client("192.0.0.0",PORT); is not working? no overloads available? What's going on there?

using the prerelease after seeing this, but the usual TcpClient client = new TcpClient.Client("192.0.0.0",PORT); is not working? no overloads available? What's going on there?

TcpClient class is in the 4.1.* System.Net.Sockets package.

Please attach a full repro of your issue including project.json. Thx.

Hey - sorry struggling with the port of a project with all these changes.
project.json is below.


{
"dependencies": {
"DBreeze": "1.74.0",
"M2Mqtt": "4.3.0",
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0",
"Newtonsoft.Json": "8.0.3",
"protobuf-net": "2.0.0.668",
"System.Net.Sockets": "4.1.0-beta-23516",
"System.Runtime.Serialization.Json": "4.0.1",
"Microsoft.NETCore.Portable.Compatibility":"1.0.0"
},
"frameworks": {
"uap10.0": {}
},
"runtimes": {
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
}


Error CS0426 the type name 'Client' does not exist in the type 'TcpClient'

Note: Have managed to get around this by using the Addressfamily approach, apologies - I assumed that the same connection approach could be used in the latest system.net.sockets nuget but after examining the metadata file, you can only use the Addressfamily;

public TcpClient();
public TcpClient(AddressFamily family);

not the previous

public TcpClient();
public TcpClient(IPEndPoint localEP);
public TcpClient(AddressFamily family);
public TcpClient(String hostname, int port);

"System.Net.Sockets": "4.1.0-beta-23516",

This is the beta package. You need to try to out either the RC3 (current dev feed) packages on the MYGET feed. The beta package had problems with UWP target.

https://dotnet.myget.org/gallery/dotnet-core

or wait until later next week when RC2 publishes to NuGet.Org

But I will say that the property 'Client' on the TcpClient object is not part of the 4.1 contract. While it is on .NET Framework Desktop, we made the decision to remove that property (which is of type Socket) due to problems with exposing the raw socket from TcpClient.

See this PR:
https://github.com/dotnet/corefx/pull/5953

This is current 4.1 System.Net.Sockets TcpClient class:

c# public partial class TcpClient : System.IDisposable { public TcpClient() { } public TcpClient(System.Net.Sockets.AddressFamily family) { } protected bool Active { get { return default(bool); } set { } } public int Available { get { return default(int); } } public bool Connected { get { return default(bool); } } public bool ExclusiveAddressUse { get { return default(bool); } set { } } public System.Net.Sockets.LingerOption LingerState { get { return default(System.Net.Sockets.LingerOption); } set { } } public bool NoDelay { get { return default(bool); } set { } } public int ReceiveBufferSize { get { return default(int); } set { } } public int ReceiveTimeout { get { return default(int); } set { } } public int SendBufferSize { get { return default(int); } set { } } public int SendTimeout { get { return default(int); } set { } } public System.Threading.Tasks.Task ConnectAsync(System.Net.IPAddress address, int port) { return default(System.Threading.Tasks.Task); } public System.Threading.Tasks.Task ConnectAsync(System.Net.IPAddress[] addresses, int port) { return default(System.Threading.Tasks.Task); } public System.Threading.Tasks.Task ConnectAsync(string host, int port) { return default(System.Threading.Tasks.Task); } public void Dispose() { } protected virtual void Dispose(bool disposing) { } ~TcpClient() { } public System.Net.Sockets.NetworkStream GetStream() { return default(System.Net.Sockets.NetworkStream); } }

@AoifeB

This: TcpClient client = new TcpClient.Client("192.0.0.0",PORT); can't possibly work, on .Net Core or .Net Framework. I think you meant: TcpClient client = new TcpClient("192.0.0.0",PORT);.

Yep my mistake, that was just a typo in the original query - the code itself has the correct call.
cheers,
----- Original Message -----
From: Petr Onderka [email protected]
To: dotnet/corefx [email protected]
Cc: AoifeB aoife.[email protected], Mention [email protected]
Sent: Fri, 06 May 2016 22:23:05 +0800 (WST)
Subject: Re: [dotnet/corefx] Can't locate TcpClient and TcpListener from Socket.Net.Sockets (#5939)

@AoifeBThis: TcpClient client = new TcpClient.Client("192.0.0.0",PORT); can't possibly work, on .Net Core or .Net Framework. I think you meant: TcpClient client = new TcpClient("192.0.0.0",PORT);.—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

Aoife Breathnach Senior Software Developer

Imdex Limited | 216 Balcatta Road BALCATTA WA 6021
T +61 (0) 8 9445 4000 | F +61 (0) 8 9445 4042
Web: www.imdexlimited.com

This message (including any attachments) contains confidential information and is intended only for the use of the addressee named above. If you are not the intended recipient of this message you must not disseminate, copy or take any action in reliance on it. If you have received this message in error, please notify us immediately by return email. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Imdex Limited. You should scan any attachments for viruses before opening or using them. Imdex Limited does not accept liability for any loss or damage which may be caused to your computer system from opening or using these attachments.

I am closing this issue since it has drifted from the original issue of being unable to use the System.Net.Sockets 4.1 contract, i.e. classes like TcpClient that are only in the 4.1 contract. That issue is fixed for RTM (but not for RC2).

I followed @Maxwe11 suggestion and created a brand new Class Library (Universal Windows):

@davidsh just create project using Class library (Universal Windows) template, install System.Net.Sockets 4.1 and try to use TcpClient.

I needed to modify the project.json file to use the 5.2 version of Microsoft.NETCore.UniversalWindowsPlatform. This is only available right now on the MYGET feed (CoreFx Dev Feed):
https://dotnet.myget.org/gallery/dotnet-core

project.json:

``` c#
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.0-rc3-24116-00"
},
"frameworks": {
"uap10.0": {}
},
"runtimes": {
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
}

I created a class in my Universal Class library and was able to use the TcpClient class:

``` c#
using System.Net.Sockets;

namespace ClassLibraryUniversal
{
    public class Class1
    {
        TcpClient _client;

        public Class1()
        {
            _client = new TcpClient();
        }
    }
}

Please try the RC3 packages including specifying/updating your project.json to select the latest RC3 versions of the UniversalWindowsPlatform package. If there are problems, please open up a new issue. Thx.

cc: @SidharthNabar @himadrisarkar @stephentoub @CIPop

System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(AddressFamily.InterNetwork);
client.ConnectAsync(IPAddress.Parse("127.0.0.1"), port);

able to connect to a Tcp Server coded in .Net Core.

But unable to Find client.Close(); API anywhere.

How to disconnect the client.

@vishal1912 Use client.Dispose();.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ericstj picture ericstj  Â·  152Comments

terrajobst picture terrajobst  Â·  158Comments

fiigii picture fiigii  Â·  181Comments

jamesqo picture jamesqo  Â·  182Comments

hqueue picture hqueue  Â·  155Comments