public void ToStringAAddress() { IrDAEndPoint ep1 = new IrDAEndPoint(new IrDAAddress(new byte[] { 1, 2, 3, 4 }), "SvcName1"); String str = ep1.ToString(); Assert.AreEqual("04030201:SvcName1", str); }
public void ToStringABytes() { IrDAEndPoint ep1 = new IrDAEndPoint(Values.AddressOne, "SvcName1"); String str = ep1.ToString(); Assert.AreEqual("04030201:SvcName1", str); }
public void ToStringAddressNone() { IrDAEndPoint ep1 = new IrDAEndPoint(IrDAAddress.None, "SvcName1"); String str = ep1.ToString(); Assert.AreEqual("00000000:SvcName1", str); }
public void ToStringC() { IrDAEndPoint ep1 = new IrDAEndPoint(new IrDAAddress(new byte[] { 1, 2, 3, 4 }), String.Empty); String str = ep1.ToString(); Assert.AreEqual("04030201:", str); }
/// <summary> /// Gets the name of the peer device using the specified socket. /// </summary> /// <param name="irdaSocket">A connected IrDA <c>Socket</c>.</param> /// <returns>The name of the remote device.</returns> /// - /// <remarks> /// This finds the name of the device to which the socket is connection, /// an exception will occur if the socket is not connected. /// </remarks> /// - /// <exception cref="T:System.ArgumentNullException"> /// <c>s</c> is null (<c>Nothing</c> in Visual Basic). /// </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// The remote device is not present in the list of discovered devices. /// </exception> /// <exception cref="T:System.InvalidOperationException"> /// The socket is not connected. /// </exception> public static string GetRemoteMachineName(Socket irdaSocket) { if (irdaSocket == null) { throw new ArgumentNullException("irdaSocket", "GetRemoteMachineName requires a valid Socket"); } if (!irdaSocket.Connected) { throw new InvalidOperationException("The socket must be connected to a device to get the remote machine name."); } //get remote endpoint IrDAEndPoint ep = (IrDAEndPoint)irdaSocket.RemoteEndPoint; IrDAAddress a = ep.Address; //lookup devices and search for match IrDADeviceInfo[] idia = DiscoverDevices(10, irdaSocket); foreach (IrDADeviceInfo idi in idia) { if (a == idi.DeviceAddress) { return(idi.DeviceName); } } // See unit-test "CheckExceptionThrownByGetRemoteMachineName". throw ExceptionFactory.ArgumentOutOfRangeException(null, "No matching device discovered."); }
// Constructor. public IrDAListener(IrDAEndPoint ep) { if (ep == null) { throw new ArgumentNullException("ep"); } this.localEP = ep; }
// Constructor. public IrDAListener(IrDAEndPoint ep) { if(ep == null) { throw new ArgumentNullException("ep"); } this.localEP = ep; }
private void LoadDevice() { _client.Client.SetSocketOption( IrDASocketOptionLevel.IrLmp, IrDASocketOptionName.NineWireMode, 1); var device = SelectIrDAPeerInfo(_client); _endPoint = new IrDAEndPoint(device.DeviceAddress, ServiceName); }
/// <summary> /// Intenta conectarse a un servidor. /// </summary> public override void Conectar() { extremoIR = new IrDAEndPoint(this.informacionIR[seleccionado].DeviceID, "Dame una Vida"); this.clienteIR.Connect(extremoIR); this.nombreRemoto = clienteIR.RemoteMachineName; if (OnConectadoAAlguien != null) { OnConectadoAAlguien(this, new EventArgs()); } }
public void IrdaCliConnect() { var cli = new IrDAClient(); IrDAEndPoint epNull = null; try { var task = cli.ConnectAsync(epNull, null); Assert.Fail("expected exception"); } catch (ArgumentException) { } }
// Function with send/recieve string endless loop on server socket void ServerFn() { Socket socket = null; try { IrDAEndPoint ep = new IrDAEndPoint( new byte[] { 0, 0, 0, 0 }, "Pop"); socket = new Socket(ep.AddressFamily, SocketType.Stream, 0); socket.Bind(ep); socket.Listen(int.MaxValue); Log("Server started, waiting for client..."); Socket remote = socket.Accept(); NetworkStream stream = new NetworkStream(remote, true); BinaryWriter bw = new BinaryWriter(stream); BinaryReader br = new BinaryReader(stream); Log("Client accepted"); int no = 0; while (true) { Log("Sending bytes"); try { bw.Write("hello no. " + (++no) + " from server"); } catch (Exception ex) { // MSG_NOSIGNAL on irda socket is supported since linux // version 2.6.24 throw new Exception("Error in writing to socket, " + "please check if your linux kernel is >= 2.6.24", ex); } Log("OK"); Log("Reading bytes"); Log(br.ReadString()); Log(""); } } catch (Exception ex) { Log("Error while running server"); Log(ex.ToString()); } finally { if (socket != null) { socket.Close(); } } }
public IAsyncResult BeginConnect(string service, AsyncCallback requestCallback, object state) { IrDADeviceInfo[] devs = this.DiscoverDevices(1); if (devs.Length > 0) { IrDAEndPoint ep = new IrDAEndPoint(devs[0].DeviceAddress, service); return(BeginConnect(ep, requestCallback, state)); } else { throw new InvalidOperationException("No remote device"); } }
/// <summary> /// Forms a connection to the specified service on an arbitrary peer. /// </summary> /// <remarks> /// As noted the connection will be made to an arbitrary peer. This is /// most useful when it is expected that there will be only one device in /// range — as is often the case. If a connection is to be made to /// a particular remote peer, then use /// <see cref="M:InTheHand.Net.Sockets.IrDAClient.Connect(InTheHand.Net.IrDAEndPoint)"/>. /// </remarks> /// <param name="service">The Service Name to connect to eg "<c>OBEX</c>". /// In the very uncommon case where a connection is to be made to a /// specific LSAP-SEL (port number), then use /// the form "<c>LSAP-SELn</c>", where n is an integer.</param> /// - /// <exception cref="T:System.InvalidOperationException"> /// No peer IrDA device was found. The exception has message “No device”. /// </exception> /// <exception cref="T:System.Net.Sockets.SocketException"> /// A connection could not be formed. See the exception message or /// <see cref="P:System.Net.Sockets.SocketException.SocketErrorCode"/> /// (or <see cref="P:System.Net.Sockets.SocketException.ErrorCode"/> on NETCF) /// for what error occurred. /// </exception> public void Connect(string service) { IrDADeviceInfo[] devs = this.DiscoverDevices(1); if (devs.Length > 0) { IrDAEndPoint ep = new IrDAEndPoint(devs[0].DeviceAddress, service); Connect(ep); } else { throw new InvalidOperationException("No device"); } }
public void Connect(IrDAEndPoint remoteEP) { if (cleanedUp) { throw new ObjectDisposedException(base.GetType().FullName); } if (remoteEP == null) { throw new ArgumentNullException("remoteEP"); } clientSocket.Connect(remoteEP); active = true; }
// Function with recieve/send string endless loop on client socket void ClientFn() { Socket socket = null; try { IrDADeviceInfo dev = selectedDevice; if (dev == null) { Log("Device not selected"); return; } IrDAEndPoint ep = new IrDAEndPoint (new byte[] { 0, 0, 0, 0 }, "Pop"); Socket socket = new Socket (AddressFamily.Irda, SocketType.Stream, 0); IrDAEndPoint ep2 = new IrDAEndPoint(dev.DeviceID, ep.ServiceName); socket.Connect(ep2); NetworkStream stream = new NetworkStream(socket, true); BinaryWriter bw = new BinaryWriter(stream); BinaryReader br = new BinaryReader(stream); int no = 0; while (true) { Log("Reading"); Log(br.ReadString()); Log("Writing"); bw.Write("hello no. " + (++no) + " from client"); Log("OK"); } } catch (Exception ex) { Log("Error while running client"); Log(ex.ToString()); } finally { if (socket != null) { socket.Close(); } } }
/// <summary> /// Da comienzo a un servicio de comunicaciones como Servidor. /// </summary> public override void Servir() { try { Cursor.Current = Cursors.WaitCursor; clienteIR = new IrDAClient(); extremoIR = new IrDAEndPoint(informacionIR[seleccionado].DeviceID, "Dame una Vida"); escuchaIR = new IrDAListener(extremoIR); escuchaIR.Start(); clienteIR = escuchaIR.AcceptIrDAClient(); Cursor.Current = Cursors.Default; this.nombreRemoto = clienteIR.RemoteMachineName; if (OnAlguienConectado != null) { OnAlguienConectado(this, new EventArgs()); } } catch { } }
// Should this fail? The ServiceName char[] is _not_ null-terminated!!! public void CreateBNoPadByte() { byte[] buffer = { /* AF */ 26, 0, /* ID */ 1, 2, 3, 4, /* SN */ (byte)'a', (byte)'a',(byte)'a', (byte)'a',(byte)'a', (byte)'a', (byte)'a',(byte)'a', (byte)'a',(byte)'a', (byte)'a', (byte)'a',(byte)'a', (byte)'a',(byte)'a', (byte)'a', (byte)'a',(byte)'a', (byte)'a',(byte)'a', (byte)'a', (byte)'a',(byte)'a', (byte)'a',(byte)'a', /* No padding byte. */ }; System.Net.SocketAddress sa = Factory_SocketAddressForIrDA(buffer); IrDAEndPoint ep = (IrDAEndPoint) new IrDAEndPoint(Values.AddressOne, "x").Create(sa); // Assert.AreEqual(new byte[] { 1, 2, 3, 4 }, ep.Address.ToByteArray()); Assert.AreEqual(new String('a', 24), ep.ServiceName); }
public void CreateA() { byte[] buffer = { /* AF */ 26, 0, /* ID */ 1, 2, 3, 4, /* SN */ (byte)'S', (byte)'v',(byte)'c', (byte)'N',(byte)'a', (byte)'m', (byte)'e',(byte)'1', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* struct alignment pad */ (byte)'!' }; System.Net.SocketAddress sa = Factory_SocketAddressForIrDA(buffer); IrDAEndPoint ep = (IrDAEndPoint) new IrDAEndPoint(Values.AddressOne, "x").Create(sa); // Assert.AreEqual(new byte[] { 1, 2, 3, 4 }, ep.Address.ToByteArray()); Assert.AreEqual("SvcName1", ep.ServiceName); }
public IrDAListener(String service) { this.localEP = new IrDAEndPoint(new byte [4], service); }
public IrDAClient(IrDAEndPoint remoteEP) : this() { Connect(remoteEP); }
/// <summary> /// Initializes a new instance of the <see cref="T:InTheHand.Net.Sockets.IrDAClient"/> /// class and connects to the specified endpoint. /// </summary> /// <remarks> /// <para> /// This is /// equivalent to calling the default constructor followed by /// <see cref="M:InTheHand.Net.Sockets.IrDAClient.Connect(InTheHand.Net.IrDAEndPoint)"/>. /// </para> /// <para> /// The endpoint specifies both the peer device and service name /// to connect to. If only one device is expected to be in range, or /// an arbitrary peer device is suitable, then one can use /// <see cref="M:InTheHand.Net.Sockets.IrDAClient.#ctor(System.String)"/> instead. /// </para> /// </remarks> /// <param name="remoteEP"> /// An <see cref="IrDAEndPoint"/> initialised with the address of the peer /// device and the service name to connect to. /// </param> public IrDAClient(IrDAEndPoint remoteEP) : this() { this.Connect(remoteEP); }
/// <summary> /// Initializes a new instance of the <see cref="IrDAListener"/> class. /// </summary> /// <param name="service">The name of the service to listen for.</param> public IrDAListener(string service) { Initialize(); serverEP = new IrDAEndPoint(IrDAAddress.None, service); }
/// <summary> /// Initializes a new instance of the <see cref="IrDAListener"/> class. /// </summary> /// <param name="ep">The network address to monitor for making a connection.</param> public IrDAListener(IrDAEndPoint ep) { Initialize(); serverEP = ep; }
// Connect to a remote end point. public void Connect(IrDAEndPoint remoteEP) { socket.Connect(remoteEP); }
private ObexStatusCode Connect() { if (!connected) { if(ns == null) { try { if (uri.Host.Length == 0) { System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream != null); System.Diagnostics.Debug.Assert(m_alreadyConnectedObexStream.CanRead && m_alreadyConnectedObexStream.CanWrite); ns = m_alreadyConnectedObexStream; } else { BluetoothAddress ba; IrDAAddress ia; if(BluetoothAddress.TryParse(uri.Host,out ba)) { s = new Socket(AddressFamily32.Bluetooth, SocketType.Stream, BluetoothProtocolType.RFComm); Guid serviceGuid; switch (uri.Scheme) { case "obex-ftp": serviceGuid = BluetoothService.ObexFileTransfer; break; //potential for other obex based profiles to be added case "obex-sync": serviceGuid = BluetoothService.IrMCSyncCommand; break; default: serviceGuid = BluetoothService.ObexObjectPush; break; } BluetoothEndPoint bep = new BluetoothEndPoint(ba, serviceGuid); s.Connect(bep); } else if (IrDAAddress.TryParse(uri.Host, out ia)) { //irda s = new Socket(AddressFamily.Irda, SocketType.Stream, ProtocolType.IP); IrDAEndPoint iep = new IrDAEndPoint(ia, "OBEX"); s.Connect(iep); } else { //assume a tcp host s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipa; try { ipa = IPAddress.Parse(uri.Host); } catch { // Compile-time: warning CS0618: 'System.Net.Dns.Resolve(string)' // is obsolete: 'Resolve is obsoleted for this type, // please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202' // However GetHostEntry isn't supported on NETCFv1, // so just keep it and disable the warning on // the other platforms. #if V1 /* #endif #pragma warning disable 618 #if V1 */ #endif ipa = System.Net.Dns.Resolve(uri.Host).AddressList[0]; #if V1 /* #endif #pragma warning restore 618 #if V1 */ #endif } IPEndPoint ipep = new IPEndPoint(ipa, 650); s.Connect(ipep); } ns = new NetworkStream(s, true); #if V2 && WinXP ns.ReadTimeout = timeout; ns.WriteTimeout = timeout; #endif } //do obex negotiation byte[] connectPacket; if (uri.Scheme == "obex-ftp") { connectPacket = new byte[] { 0x80, 0x00, 26, 0x10, 0x00, 0x20, 0x00, 0x46, 0x00, 19, 0xF9, 0xEC, 0x7B, 0xC4, 0x95, 0x3C, 0x11, 0xD2, 0x98, 0x4E, 0x52, 0x54, 0x00, 0xDC, 0x9E, 0x09 }; } else { connectPacket = new byte[7] { 0x80, 0x00, 0x07, 0x10, 0x00, 0x20, 0x00 }; } ns.Write(connectPacket, 0, connectPacket.Length); byte[] receivePacket = new byte[3]; int bytesReceived = ns.Read(receivePacket, 0, 3); //failure if (bytesReceived == 0) { throw new Exception("Connection Lost"); } while (bytesReceived < 3) { bytesReceived += ns.Read(receivePacket, bytesReceived, 3 - bytesReceived); } if (receivePacket[0] == (byte)(ObexStatusCode.OK | ObexStatusCode.Final)) { //get length short len = (short)(IPAddress.NetworkToHostOrder(BitConverter.ToInt16(receivePacket, 1)) - 3); byte[] receivePacket2 = new byte[3+len]; Buffer.BlockCopy(receivePacket, 0, receivePacket2, 0, 3); int nextReceived = ns.Read(receivePacket2, 3, len); if(nextReceived == 0) { throw new Exception("Connection Lost"); } bytesReceived += nextReceived; while (bytesReceived < len+3) { bytesReceived += ns.Read(receivePacket2, 3+bytesReceived, len - bytesReceived); } ObexParser.ParseHeaders(receivePacket2, ref remoteMaxPacket, null, headers); if (headers["CONNECTIONID"] != null) { connectionId = int.Parse(headers["CONNECTIONID"]); } //ParseHeaders(receivePacket2, headers, null); } return (ObexStatusCode)receivePacket[0]; } finally { if (s != null && !s.Connected) { s = null; } } } } return (ObexStatusCode)0; }
public IAsyncResult BeginConnect(IrDAEndPoint remoteEP, AsyncCallback requestCallback, object state) { return(this.Client.BeginConnect(remoteEP, requestCallback, state)); }
public void SerializeOverlongServiceName() { System.Net.SocketAddress sa = new IrDAEndPoint(Values.AddressOne, new String('a', 50)).Serialize(); }
public System.Threading.Tasks.Task ConnectAsync(IrDAEndPoint remoteEP, object state) { return(System.Threading.Tasks.Task.Factory.FromAsync( BeginConnect, EndConnect, remoteEP, state)); }
public void Serialize27ByteServiceName() { System.Net.SocketAddress sa = new IrDAEndPoint(Values.AddressOne, new String('a', 27)).Serialize(); //AssertAreEqual(ExpectedBuffer24LetterAThusOneNullTerminatorAndWithPadByte, sa); }