rdavisau / sockets-for-pcl

Cross-platform socket API for Xamarin iOS/Android/Forms, Xamarin.Mac/MonoMac, Windows Phone 8/8.1, Windows Store and Windows Desktop.
MIT License
223 stars 76 forks source link

Using Sockets PCL in another PCL #112

Open jmc420 opened 7 years ago

jmc420 commented 7 years ago

Hi

I am trying to using Sockets PCL in an Xamarin Forms app but when I try to instantiate a TcpSocketListener I get this error:

System.NotImplementedException: The empty PCL implementation for Sockets was loaded. Ensure you have added the Sockets nuget package to each of your platform projects. at Sockets.Plugin.TcpSocketListener..ctor () [0x00006] in <763bc5368b9b425994cb494a5a2e7457>

I have added Sockets PCL to the main project, to Droid project and the iOS project. Here is the packages.config for the ios project:

What am I doing wrong?

James

rdavisau commented 7 years ago

Hi - sounds like you're doing the right thing. Do you get this error on all platforms?

jmc420 commented 7 years ago

Hi

thanks for the reply. Yes I get in both ios and android. This is the jni error I get when running it on the android emulator:

System.NotSupportedException: Could not activate JNI Handle 0xb4900025 (key_handle 0x5285ab84) of Java type 'md562498a7269813d6300fb90f21f91988d/MainActivity' as managed type 'example.Droid.MainActivity'. ---> System.NotImplementedException: The empty PCL implementation for Sockets was loaded. Ensure you have added the Sockets nuget package to each of your platform projects. at Sockets.Plugin.TcpSocketListener..ctor () [0x00006] in <763bc5368b9b425994cb494a5a2e7457>:0

In the packages.config of the Droid directory I include Sockets PCL:

When I look at the packages directory I see all the DLLs for Socket.PCL.

A couple of thoughts:

I guess socket handling is only partially implemented in the emulators but this would not cause this error?

The other thing I have noticed is that when I do update nuget packages, I get an error caused by a missing package which I no longer use:

Package 'EventBus 1.1.2' does not exist in folder '/Users/jamescowan/xamarin/example/packages'

Could not install package 'EventBus 1.1.2'. Maybe this is causing the problem but I do not know how to fix this - I cannot find any references to it packages.config in the various project. Are the nuget references held somewhere else in Xamarin Studio?

James

rdavisau commented 7 years ago

I think you are attaching or trying to include images in the issue but I can't see them - are you able to share links?

There's no problem with using the emulators for socket work - the only thing you need to watch out for is firewalls and traversing networks if you want to work between physical and simulator devices. But we have a ways to go before we get to that here!

Strange error, is the EventBus folder in your packages directory? If So, delete. I suppose you have tried a full clean and build also?

Can you try a new clean solution where you install sockets for pcl into that and check whether you can instantiate any classes?

jmc420 commented 7 years ago

I think one of the issues was that I was opening the socket before the Xamarin initialisation had finished.

I have created a very simple ExampleSocket here: https://github.com/jmc420/xamarin-examples

All it does is open a TcpSocketListener and bind a port and then display to the screen the socket has been opened.

The iOS project works but the Android project still crashes in the file ExampleTcpServer.cs on line 24: this.server = new TcpSocketListener();

James

jmc420 commented 7 years ago

During editing source code, when I go to the declaration of TcpSocketListener within Xamarin Studio, I go to PCL version:

public TcpSocketListener () { throw new NotImplementedException ("The empty PCL implementation for Sockets was loaded. Ensure you have added the Sockets nuget package to each of your platform projects."); }

Should I be able to go into the specific implementation?

James

jmc420 commented 7 years ago

if I add package id="Sharpcaster.SocketsForPCL" version="2.0.4", the ExampleSocket works.

What is the relationship between Sharpcaster.SocketsForPCL and rda.SocketsForPCL?

James

Tapanila commented 7 years ago

Hey,

SharpCaster.SocketsForPCL is a fork from this project. You can found it from here https://github.com/tapanila/sockets-for-pcl