System.Net.Internals.SocketExceptionFactory.ExtendedSocketException

Joshua Feng 21 Reputation points
2022-06-16T22:55:35.313+00:00

I was writing the C# program below for both read and write using bluetooth between two PCs. However, the code always reports error on line "client.Connect(ep);". The error message is below. This doesn't happen on the other computer. Does anybody know why? Is there any problem with the PC setting side?
212157-image.png212200-image.png

using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading;  
using System.Threading.Tasks;  
using InTheHand.Net;  
using InTheHand.Net.Bluetooth;  
using InTheHand.Net.Sockets;  
  
  
namespace BluetoothStudy  
{  
    class Program  
    {  
  
        static void Main(string[] args)  
        {  
            BluetoothClient client = new BluetoothClient();       
            BluetoothRadio radio = BluetoothRadio.PrimaryRadio;   
            radio.Mode = RadioMode.Connectable;       
            BluetoothAddress blueAddress = new BluetoothAddress(new byte[] { 0xa3, 0xdb, 0xb0, 0x28, 0xfc, 0x9c });   
            BluetoothDeviceInfo[] devices = client.DiscoverDevices();     
  
  
  
            Console.WriteLine("Enumerating Devices until the desired one");  
            foreach (var item in devices)  
            {  
                if (item.DeviceName.Contains("DESKTOP-EJGOC3D"))  
                {  
                    Console.WriteLine(item.DeviceAddress);  
                    Console.WriteLine(item.DeviceName);  
                    blueAddress = item.DeviceAddress;            
                    break;  
                }  
                Console.WriteLine(item.DeviceAddress);  
                Console.WriteLine(item.DeviceName);  
            }  
  
            BluetoothEndPoint ep = new BluetoothEndPoint(blueAddress, BluetoothService.SerialPort);  
  
            Console.WriteLine("Connecting!");  
            client.Connect(ep);     
            if (client.Connected)  
            {  
                Console.WriteLine("Connection Succeeded!");  
                Stream peerStream = client.GetStream();        
  
                //write data  
                string str = "Messege";  
                peerStream.Write(System.Text.Encoding.Unicode.GetBytes(str), 0, str.Length);   
                Console.WriteLine("Message Sent!");  
  
                  
                //read data  
                Thread listenThread;  
                listenThread = new Thread(() => Program.ReceiveData2(peerStream));  
                listenThread.Start();  
                  
            }  
  
  
        }  
}  
Windows for business | Windows Client for IT Pros | Networking | Network connectivity and file sharing
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,921 Reputation points
    2022-06-23T08:48:35.29+00:00

    Hi JoshuaFeng-6335,

    Your code is moreover correct you'll need to add Exception Handling part to avoid this error and you'll need to enable :

    Application. appxmanifest / Capabilities / Private Networks (Client & Server) For exception handling implementation, refer to following documentation :- https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.socketexception?view=net-6.0

    --------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.