I made chat server in C# using
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.TCP);
s.Bind(IPAddress.Parse(theIP),thePort);
But then I am getting a
"SocketException was unhandled, The requested address is not valid in
its context"
How do I look for the correct IP to use? cmd ipconfig IPv4Address? Because that IP (I believe) is for the internal IP. I want the server IP to be an external IP address that is accessible from outside my network
You need to create a static path (sometimes called port forwarding) in your router binding one of your external IP/ports to an internal address and the port needed for your chat server. You will then reference your internal IP address, on your network, in your code.
Related
I've written a server and client in C# .net 4.6 that should accept both ipv6 and ipv4 connections. The relevant code for the server listener is as follows:
listener = new Socket(AddressFamily.InterNetworkV6,
SocketType.Stream, ProtocolType.Tcp);
listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
listener.DualMode = true;
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(new IPEndPoint(IPAddress.IPv6Any, 3425));
listener.Listen(100);
And the client connects like this:
socket = new Socket(AddressFamily.Unspecified, SocketType.Stream, ProtocolType.Tcp);
try
{
IPAddress[] ip = Dns.GetHostAddresses(ipAddress);
StatusBoxHandler.statusText = "Connecting...";
IPEndPoint remoteEP = new IPEndPoint(ip[0], port);
Debug.Log(ip[0]);
socket.Connect(remoteEP);
I've had no problems so far with 3 different people trying to connect to this server with the given client. The fourth person, who is running Windows 8.1 however is having a bit of difficulty.
He is unable to ping the server with the hostname as it says destination unreachable, and he is unable to ping the servers ipv6 address, giving the error:
PING: transmit failed, general failure.
He is however able to ping the ipv4 address successfully. The weird bit with this is that he has ipV6 enabled and I have verified this.
When attempting to connect with the client and forcing it to use the ipv4 address he obtains the error:
System.Net.Sockets.SocketException: The system detected an invalid pointer address in attempting to use a pointer argument in a call.
I've also verified that in the log it's connecting to the exact same ipv4 address that mine would use to connect, so it's not a matter of it obtaining the wrong ip address. Additionally, I've connected from the client to the server successfully with ipv6, ipv4 and the dns hostname addresses and they all work fine for my machine and two others.
Is there some sort of problem in my code that I'm missing or is there just some sort of strange unrelated issue that is only affecting his machine?
The way IP addess is being retrieved is not correct. Instead use below way
var ipHostInfo = Dns.GetHostEntry("localhost");
var ipAddress = ipHostInfo.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);
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));
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.
I am using C# sockets to have a connection between a device (client) and a computer (server).
All is working good except for the fact that I am trying to avoid the user to enter the IP address and port number wherein to connect to on the device. Instead I want a listbox or drop down list of all IP addresses with listening sockets. Just wondering if there's a way to get all the IP addresses and port numbers of hosts with listening (socket)?
Thanks for any help! :)
What you're asking to do is called a port scan. It basically involves testing each IP address in a range and each port and reporting the success of that attempt. It's slow and it will cause a lot of alarms if there is any kind of threat monitoring on the network because port scanning is one of the ways attackers try to find network vulnerabilities.
So in short, this is a bad idea and probably won't be responsive enough for you to use for this purpose. Instead what you might consider is using a central server as a "directory" that each server would register with.
Or you can send out a broadcast on your subnet and wait for servers to respond. This is how some of the peer networking works in Windows for example. Note that this assumes you are the developer of the server as well and you can add in the logic necessary for the server to listen for broadcasts.
By using UDP broadcast, you can send out the data to the Server which are listening at the fixed port. The following is the working example.
foreach (IPAddress ip in allLocalNetworkAddresses.AddressList)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//Allow sending broadcast messages
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
//Create endpoint, broadcast.
IPEndPoint AllEndPoint = new IPEndPoint(IPAddress.Broadcast, Port);
byte[] sendData = Encoding.ASCII.GetBytes("1");
//Send message to everyone on this network
client.SendTo(sendData, AllEndPoint);
Console.Write("Client send '1' to " + AllEndPoint.ToString() +
Environment.NewLine);
I'm working on a simple hello world TCP/IP client server app in C# and am unable to get my client to connect. Can anyone offer any additional troubleshooting steps? I'm starting to run out of ideas...
Here are the relevant sections of code:
server:
Console.Out.WriteLine("About to bind address");
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
Console.Out.WriteLine("Choose a port to bind...");
String port = Console.In.ReadLine();
int iPort = Int32.Parse(port);
TcpListener myList = new TcpListener(ipAd, iPort);
myList.Start();
Console.WriteLine("The server is running at: "+myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
client:
Console.Out.WriteLine("enter address: ");
string address = Console.In.ReadLine();
Console.Out.WriteLine("enter port: ");
int port = Convert.ToInt32(Console.In.ReadLine());
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
Console.Out.WriteLine("Address: " + address + ":" + port);
tcpclnt.Connect(address, port);
I am able to ping the server from the client machine, however I am unable to telnet to the server using the bound port. I've tried a variety of ports (a few in the low 8000s and a few up around 40000). I have disable windows firewall on both systems. The systems are connected to a router which is not on the internet. I've tried with and without port forwarding set to forward incoming requests on the given port to the server machine with no effect.
The only exception that I've been able to trap is thrown by the client:
No connection could be made because
the target machine actively refuses
it.
I checked for an InnerException but it seems that there are none - that is the base exception. Could that be right?
Not sure what else I should be looking at - any additional troubleshooting steps would be helpful.
Thanks!
I've run into this before. The trick is to bind to 0.0.0.0 rather than 127.0.0.1. When you bind to 127.0.0.1 the server will only accept connections from localhost. Binding to 0.0.0.0 it will accept all requests.
You also may want to nmap the host machine from the client and see what ports it sees as being open.
EDIT: If you hard code the IP address of the machine in it the listener will only listen on that network interface. If you use 0.0.0.0 the listener will listen on all available network interfaces. This includes interfaces between your computer and a USB attached hand held device, a second network card or a VPN link.
The code above is listening to request coming from the loopback address. This will effectively only listen to connection on that network, and that network only includes your machine.
Have you tried listening to the address bound to the network from which the connection should be coming? On a local network it should be something like 192.168.x.x or 10.x.x.x
try netstat -al on your machine (the exact command line varies between Windows and unix) and see if the server is listening on the port
Why don't you use .NET remoting? It is better than doing a TCP/IP client server. You can pass messages between objects.
Have you tried running the client server on the same machine to make sure the connection is made first? Beyond that try using the router assigned or static IP of the machine running the server vs binding to loopback.