I'm developing an application in ASP.NET using Visual Basic, that have to connect to a Server in my private network.
The application must working only into my network (in future it can work on internet, too),
now I have a problem with TcpClient on ASP.NET: If I connect to the Server using an instance of IPAddress
Client = New TcpClient
Client.Connect(New IPAddress("192.168.1.12"), 6001)
the Socket try to connect to 176.64.116.11 (that's not my public IP Address...), else, if I connect to the server with a string that contains the local IP Address
Client = New TcpClient
Client.Connect("192.168.1.12", 6001)
the Socket connects succesfully but nothing responds to my command (with NetworkStream.Write and Read)
I try all of these in a Windows Application and all work succesfully.
Thanks to all
(I made any mistake in English? Ahaha, sorry :D)
PS. If you post me some code in C# don't worry, I can translate it
TcpClient has various overloads, you can give it a string containing the ip address or an IPAddress object.
Also, use
IPAddress ipAddress = IPAddress.Parse("192.168.1.12");
IPAddress does not contain a constructor that takes a string.
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.connect.aspx
As for your transmission issue; disable firewalls. Try localhost first.
Related
Using the following code, my client fails to connect my server:
private static TcpClient client = new TcpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0));
private static IPEndPoint destinationEp = new IPEndPoint(IPAddress.Parse("192.168.0.100"), 1234);
//...
client.Connect(destinationEp);
Using TcpClient client = new TcpClient() instead will work.
In the original case, my understanding is that I am setting the local IP to the local machine, and using any available port as the local port to facilitate communication. My suspicion is that the server is trying to connect to the client using the IP "127.0.0.1", which wouldn't work, but I don't know this for sure.
Why do I have to use new TcpClient() instead of new TcpClient(myEndpoint) to successfully establish a server connection?
See the docs:
Initializes a new instance of the TcpClient class and binds it to the specified local endpoint.
Emphasis mine. You use that constructor only if you want to control the local part of the socket. See also the remainder of the docs:
You do not need to specify a local IP address and port number before connecting and communicating. If you create a TcpClient using any other constructor, the underlying service provider will assign the most appropriate local IP address and port number.
So your suspicion is correct. You're basically telling the network stack that you want your end of the socket to be bound to 127.0.0.1:0, which won't work for outbound connections.
I have a server and a client app, the server throws no exception and seems to listen properly but the clients aren't able to connect for some reasons, I tried with both local network and public IP.
-With local network's IP I can connect to it only when the client app is runned on the same computer than the server.
-With public IP nothing can connect to the server.
However in both cases when the connection fails, it behaves always the same : waits for aproximately 5-10 seconds and then throws System.Net.Socket.SocketException. I tried with port 1507 first then tried with port 80 but it didn't change anything
Server
IPAddress adress = IPAddress.Parse(MyIp);
TcpListener listener = new TcpListener(adress, 80);
listener.Start();
TcpClient Client = await listener.AcceptTcpClientAsync();
Client
TcpClient client = new TcpClient();
client.ConnectAsync(IPAddress.Parse(MyIp), 80).GetAwaiter().GetResult();
I don't understand where the problem comes from, is it coming from my code or not? If not the what could it be and how could I fix it?
Have you tried Telnet from outside network on specific port?
If you can't connect probably is router port redirection or access is blocked by firewall.
I built tcp client/server application for my organisation. The server opens and listens to a specific port, and each client establishes Tcp Connection to the server port. Nothing special. The application works beautifully. But today, one client wanted the Tcp client app to work over WiFi network with firewall. The WiFi firewall is configured to block all ports by default. If I want my application to work, I have to give their network administrator a list of ports to open for my application. The server listening port is configurable so it is easy. Once I configure the port, I can give them is this specific port for the server. However the client app is unable to connect to the server because each time a TcpClient establishes a connection, it creates a random local Tcp port that will be blocked by their firewall.
Their network admin will not open all ports for the machine because they said it created security risks for their organisation. Therefore, I am looking for a way to force the client to open a specific local port when it establishes a Tcp connection. I've both looked into MSDN docs and been Googling but I haven't found an adequate answer. Would you be able to suggest a workaround or a third party library that can do that? Thank heaps.
I'm not aware of any way to have this level of control with TcpClient. However, if you manually create the Socket object, you can bind to a local port of your choosing:
var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Specify the local port to use
var endpoint = new IPEndPoint(IPAddress.Any, 9999);
sock.Bind(endpoint);
// And connect to the remote end point
sock.Connect("example.com", 80);
Of course, by doing this you limit yourself to one connection on the machine.
how about use bind() like this(if you are using c++)
struct sockaddr_in cli;
memset(&cli, 0x00, sizeof(cli));
cli.sin_family = AF_INET;
cli.sin_port = htons(YOUR PORT);
cli.sin_addr.s_addr = inet_addr(YOUR IP ADDRESS);
int on = 1;
if (setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0){
perror("Set Error");
}
bind(hSocket, (struct sockaddr *)&cli, sizeof(struct sockaddr));
int nRet = connect(hSocket,(sockaddr*)&svr, sizeof(svr));
My system is accessing internet via a proxy server
(IE proxy server address : myserver.mydomain.com , port: 80).
Im trying to send some data to a TCP server using class "System.Net.Sockets.TcpClient". But i am unable to connect. If I try using static IP internet, i am able to connect.
Is there any way to provide proxy address ? I am pretty sure the problem is the proxy, because i have tried from two systems which do not use proxy and it worked fine.
My application is a console application.
My Code is something like :
TcpClient tcpClient = new TcpClient("tcpserver.com", 3101);
string message = "message";
byte[] auditMessageStream;
auditMessageStream = Encoding.UTF8.GetBytes(message);
int i = tcpClient.Client.Send(auditMessageStream);
You can use this opensource lib to create a socket conection through a proxy.
Watch this too
You need HTTP tunneling unless your proxy is a SOCKS proxy.
I made a program that connects to a server (which I also wrote) via TCP\IP.
When testing on the same computer - it works fine (connect to 127.0.0.1), but it doesn't work on different computers.
I have an NOIP address also - how can I use it?
I did try using my address itself (80.whatever) on both the client and server and it didn't connect at all - it couldn't reach the host.
What should I do?
Try disabling your windows Firewall.
Make sure your server is binding to all of your IP addresses, not just localhost (127.0.0.1).
Oh - basically I found out that I don't need to supply my IP address - I just did:
_client = new TcpListener(1234);