I try to send a code(TLRequestAuthSendCode) but it doesn't come. In what there can be a problem?
Part of code:
TLContext tlContext = new TLApiContext();
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(connections[0].getAddress(), (int)connections[0].getPort());
TLRequestAuthSendCode tlRequestAuthSendCode = new TLRequestAuthSendCode("PHONE_NUMBER",
0, 19114, "TOKEN", "en");
tlRequestAuthSendCode.serializeBody(new StreamWriter(tcpClient.GetStream()));
can you debug your code and tell if
tcpClient.Connect(connections[0].getAddress(),
Connections[0] has a value?
I'd suggest you change your code to the following:
public void run() {
connections = new ConnectionInfo[]{
new ConnectionInfo(1, 0, "149.154.167.40", 443)
};
apiState = new ApiState(connections);
doReqCode(connections);
private void doReqCode(connections){
var args = new SocketAsyncEventArgs();
I don't see where the code could break, maybe a more detailed description of your problem would be helpful
TcpClient tcpClient = new TcpClient();
As i tried the code, it mostly seemd to me as if the port was either blocked or already in use. Maybe u have a permission problem there. At least i didnt manage to get a Socket to work with the code like that.
Also i didnt dive to deep in your code, but you are using port 443. This is a reserved port with limited access.
You can get started with this, but I have written it in vb.net, not C#.
It will walk you through getting started building your own Telegram-API from scratch.
Also try and get familiar with the online documentation, it's hard but the step by step explanation I gave in that link above on generating your AuthKey, should get you started.
Good Luck
Related
I'm trying to use SocketLite.PCL with my iOS/Android solution in Xamarin,
but I get the message Allow Multiple Bind To Same Port only allowed on Windows when running it.
What does it mean and how do I fix it?
EDIT:
Example code I'm using can be found here: https://github.com/1iveowl/SocketLite.PCL
I put the following code inside rotected async override void OnStart(){} of the app:
var udpReceived = new UdpSocketReceiver();
await udpReceived.StartListeningAsync(4992, allowMultipleBindToSamePort: true);
var udpMessageSubscriber = udpReceived.ObservableMessages.Subscribe(
msg =>
{
System.Console.WriteLine($"Remote adrres: {msg.RemoteAddress}");
System.Console.WriteLine($"Remote port: {msg.RemotePort}");
var str = System.Text.Encoding.UTF8.GetString(msg.ByteData);
System.Console.WriteLine($"Messsage: {str}");
},
ex =>
{
// Exceptions received here
}
);
EDIT 2:
Ok, so setting allowMultipleBindToSamePort to false stopped that error.
Now I get the error Address already in use.
However I am still curious as to what allowMultipleBindToSamePort is used for.
As you can see in the new documentation:
IMPORTANT: Please notice that the parameter allowMultipleBindToSamePort will only work on Windows. On other platforms it should be set to false
About However I am still curious as to what allowMultipleBindToSamePort is used for.
There is a good and complete explanation on this post, you can read more in the following stackoverflow post
I want to connect to a cisco switch via telnet in c#.I want to send commands in cmd with c# and when it asks for password I want to enter it with my program. But my problem is that I can’t send password when it is connected to telnet.And when I used StreamWriter it throws an exception.
Here is my code:
class Program
{
static void Main(string[] args)
{
string data = "";
StreamReader reader = new StreamReader(#"C:\Windows\System32");
StreamWriter writer = new StreamWriter(#"C:\Windows\System32");
IPAddress address = IPAddress.Parse("172.16.0.110");
IPEndPoint ipe = new IPEndPoint(address, 23);
Socket telnetSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
telnetSocket.Connect(ipe);
NetworkStream NsStream = new NetworkStream(telnetSocket, true);
if (telnetSocket.Connected)
{
NsStream = new NetworkStream(telnetSocket, true);
reader = new StreamReader(NsStream);
}
while (!reader.EndOfStream)
{
data = reader.ReadLine();
if (data.Contains("Password:"))
{
//I want to enter password in cmd here
}
}
reader.Close();
if (NsStream == null)
NsStream.Close();
}
}
You're going to need to talk Telnet. Telnet rfcs will give you an idead what you're dealing with. https://www.rfc-editor.org/rfc/rfc854
If you want to hand-roll this, a few suggestions that might help. Or else show why it might be a good idea to use a telnet library.
TcpClient will reduce your work a little. At least it will create the NetworkStream for you.
You can probably ignore much of the protocol details, but not the initial option negotiation. You probably want to deal with the raw NetworkStream initially, because telnet sessions start with 'option' negotiation. A quick search for telnet option negotiation turns this up: How to deal with the telnet negotiation
There are two problems using the reader/writer above:
They will be using UTF8 encoding by default, so the telnet option negotiation data (which isn't 7 bit ASCII) will likely get mutated.
Readline() will probably hang anyway, as it will read the telnet options, which are the first thing sent by the server, but then keeping trying to read until the first end of line ... which will never arrive because the server is waiting for a response to the options it sent. i.e. you need to finish the telnet option negotiation.
If you use a lib like SSH.NET you get all these problems solved for you and no need to reinvent the wheel again!
I'm using my TOR browser to connect to .onion websites and download data using c#. What I wanted to do, was add a button that allowed the user to get a new identity, but nothing I tried have worked so far.
I've tried using telnet, sending a webrequest to the port 9151, running a vbs that was supposed to do this, but nothing worked.
I have tried using TorSharp, but while that worked that only worked Async and I couldN't use that properly. I'm currently using com.LandonKey.SocksWebProxy.
How could I do this?
I'll add relevant code when I know what is needed, just ask.
EDIT:
#Ralph Wiggum
Sadly I can't remember every way I've tried creating a new Identity, as I've said, I tried running a VBS using Diagnostic.Process.Start(), but i doN'T have that script any more.
I also tried using WebRequest but I'm not even sure how that should be done.
This is how that looked as i can remember:
com.LandonKey.SocksWebProxy.Proxy.ProxyConfig pc = new com.LandonKey.SocksWebProxy.Proxy.ProxyConfig();
pc.SocksAddress = IPAddress.Parse(tb_Location.Text);
pc.SocksPort = 9151;
SocksWebProxy sw = new SocksWebProxy(pc);
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create("http://127.0.0.1");
wreq.Headers.Add("SIGNAL", "AUTHENTICATE \"\"");
wreq.Headers.Add("SIGNAL", "NEWNYM");
using (var wres = wreq.GetResponse())
{
using (var s = new StreamReader(wres.GetResponseStream()))
{
MessageBox.Show(s.ReadToEnd());
}
}
I also tried using telnet (using PrimS.Telnet), and that didn't work either. That looked something like this:
CancellationToken ct = new CancellationToken();
PrimS.Telnet.Client c = new PrimS.Telnet.Client("127.0.0.1", 9151, ct);
c.WriteLine("AUTHENTICATE \"\"\n");
c.WriteLine("SIGNAL NEWNYM");
#drew010
As I said, I did use TorSharp but I stopped. It was incredibly easy to create a new identity there, but it ran entirely Async, and I couldn't manage to fix it to use it with the rest of my code.
To get a new identity using through code, you need to open a connection to the control port (usually 9051 and disabled by default [edit your torrc to add ControlPort 9051]) and issue a NEWNYM signal to establish a new circuit.
To do it you can use the TorControlClient class in TorSharp.
using Knapcode.TorSharp.Tools.Tor;
TorControlClient tc = new TorControlClient();
tc.ConnectAsync("localhost", 9051);
tc.AuthenticateAsync(null); // you should password protect your control connection
tc.SendCommandAsync("SIGNAL NEWNYM");
You can also use this batch file to request a new identity but C# is probably better for your application. Reference that code to see the sequence on the control connection for getting a new identity.
See ControlPort and HashedControlPassword configuration options.
Hope that helps.
I am using C# (Unity3D) and a TcpClient to connect to my socketserver. The URL I connect with is ws://secure.mydomain.com, but I need to specifically connect to the URL ws://secure.mydomain.com/scoreTrack.
This is how I initialise the connection:
string url = "secure.mydomain.com";
mySocket = new TcpClient();
mySocket.Client.Connect(url, port);
theStream = mySocket.GetStream();
theWriter = new StreamWriter(theStream);
theReader = new StreamReader(theStream);
If I set the url to secure.mydomain.com/scoreTrack I get a host not found error. However, I can't find a place to add a path to my URL so I write to that specific path.
Any thoughts?
thanks for your comments! I am not very familiar with these protocols, but I have a working version now thanks to the free Unity plugin Socket.IO.
https://www.assetstore.unity3d.com/en/#!/content/21721
With this plugin I could easily set it up, using the full url "ws://secure.mydomain.com:80/scoreTrack".
Thanks again for your help.
The client connects to the server using GenuineChannels (we are considering switching to DotNetRemoting). What I mean by find is obtain the IP and port number of a server to connect to.
It seems like a brute-force approach would be try every IP on the network try the active ports (not even sure if that's possible) but there must be a better way.
Consider broadcasting a specific UDP packet. When the server or servers see the broadcasted UDP packet they send a reply. The client can collect the replies from all the servers and start connecting to them or based on an election algorithm.
See example for client (untested code):
using System.Net;
using System.Net.Sockets;
[STAThread]
static void Main(string[] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
socket.Bind(new IPEndPoint(IPAddress.Any, 8002));
socket.Connect(new IPEndPoint(IPAddress.Broadcast, 8001));
socket.Send(System.Text.ASCIIEncoding.ASCII.GetBytes("hello"));
int availableBytes = socket.Available;
if (availableBytes > 0)
{
byte[] buffer = new byte[availableBytes];
socket.Receive(buffer, 0, availableBytes, SocketFlags.None);
// buffer has the information on how to connect to the server
}
}
I'd say the best way is to use Bonjour/Zeroconf/mDNS for C#; a lot of thought went into making it play nice with the network; IE it pings less frequently over time if possible, etc. There's Mono.Zeroconf, and I read there's an older .NET project in the Apple SDK but I haven't found it.
So the easiest would be to install Bonjour for Windows, then get the Windows Binaries for Mono.Zeroconf try the example MZClient.exe drop the Mono.Zeroconf.dll and/or Mono.Zeroconf.Providers.Bonjour.dll into your project references and go.
Something like this:
var service = new Mono.Zeroconf.RegisterService {
Name = "Use Me for Stuff",
RegType = "_daap._tcp",
ReplyDomain = "local.",
Port = 0024200,
TxtRecord = new Mono.Zeroconf.TxtRecord {
{"I have no idea what's going on", "true"}}
};
service.Register();
var browser = new Mono.Zeroconf.ServiceBrowser();
browser.ServiceAdded +=
delegate(object o, Mono.Zeroconf.ServiceBrowseEventArgs args) {
Console.WriteLine("Found Service: {0}", args.Service.Name);
args.Service.Resolved +=
delegate(object o, Mono.Zeroconf.ServiceBrowseEventArgs args) {
var s = args.Service;
Console.WriteLine(
"Resolved Service: {0} - {1}:{2} ({3} TXT record entries)",
s.FullName, s.HostEntry.AddressList[0], s.Port, s.TxtRecord.Count);
};
args.Service.Resolve();
};
browser.Browse("_daap._tcp", "local");
Just wanted to point out an alternative Zeroconf NuGet package: Zeroconf. It does not have any native dependencies, so you don't need to install Bonjour for Windows or anything else.
It has support for .NET 4.5, WP8 and Win8.
WS-Discovery is a protocol intended for this purpose. It has a few different variations, different flavors of broadcasting and proxies. http://en.wikipedia.org/wiki/WS-Discovery
.NET WCF4 implements this.
Have the server listen for broadcast on a specific port on the network (must use UDP), When client starts have it broadcast some "ping" request on that port. when the server sees a "ping" it send back a message with the TCP address and port required for the client to connect to it.