Read data from IP port - c#

I am having an static IP (amazon ec2 instance) and a defined port which is connected to a device. This device send data on this particular port usually every minute over the GSM network.
For eg.172.20.1.170 is the IP and 4001 is port.
I am using C# socket to read the data but it is giving error "No connection could be made because the target machine actively refused it 172.20.1.170:4001"
Please suggest the sample code to read the data from the IP and port

Try to hit the ip and port embedded in ur device from the web browser (chrome , firefox).. make sure our PC and device are in same network... if you get the data on browser you are good to go for coding..
you can also try hyper terminal ..

Related

Serial Port Over Remote Desktop

need your help.
I have this scenario:
I have to comunicate to a Printer connected to Serial Port, over remote Desktop, in bidirectional communication, it means, i want to send comands to the Serial Port, and get the response of the remote printer, all of this in Remote Desktop.
I successfull can connect locally, i can send commands to the printer in the remote Desktop with a Spooler, but locally i can get the response that the Printer sends me when all finish (close Fiscal Document).
I have tried with c#, but this only works when i the Printer is connected in my local machine.
I am sharing ports, printers in my remote desktop communication, but i cannot get any response from the remote Printer.
How can i do that?
Any help, will be appreciated.
Normally I will send some code, but, i just want some help of you telling me if that is possible, or maybe if you have some examples or guides.
Regards
Firewall is blocking communications.

C#, Socket.Bind when using the 0 as port

In MSDN, there is a sentence,
"If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000"
My confusion is:
When the Client want to connect to server,
Does the client still need to know which port the server is listening?
Or the Client just connect to the 0 port.
Thanks.
The client still needs to know which port the server was actually bound to, and connect to that.
An example of this in action is in the FTP protocol. Normally when using active mode, the server uses one (known) port for commands, and binds an extra dynamic port for data transfer. When a data transfer starts, the server sends the actual port in a message to the client over the command channel, and the client then connects to the (psudo-random) data port it just got informed about to get the actual file data.
when you connect to server ,you must know ip+port decide your application to link which process on the remoting server.
socket connect like that
your :IP server:Ip
< =============================================================>
your : port server:port
so in client. you do not care which local port. when you connect to the server ,server had know the port .

client/server chat program in C#/winforms IP address issue

I am trying to make a winforms client/server chat application. I already have a client and a server, both are working fine, i can log in, send messages, etc .. The problem is that it only works if i connect to my local IP. When i try to run the program with my external IP, i get the following error:
"The requested address is not valid in its context".
In other words, i can run the server on my own computer, start up (for example) 3 clients and let them connect to the running server. This basically means that i'm talking to myself. What im trying to do is to run the chat server on my computer, and let other people run the client-program and connect to the server through the internet.
this is the part where i get an error when i enter my external IP:
public void StartListening()
{
// Get the IP of the first network device, however this can prove unreliable on certain configurations
IPAddress ipaLocal = ipAddress;
// Create the TCP listener object using the IP of the server and the specified port
tlsClient = new TcpListener(ipaLocal, 50702);
// Start the TCP listener and listen for connections
tlsClient.Start(); <--- THINGS GO SOUTH HERE
// The while loop will check for true in this before checking for connections
ServRunning = true;
// Start the new tread that hosts the listener
thrListener = new Thread(KeepListening);
thrListener.Start();
}
im not sure if what i'm trying to do is possible? i can't imagine it is, but i kinda dont know how to proceed with this. I'm new to network programming so any help will be appreciated.
kind regards,
Jane
Jane,
I think your issue is with your IP address setup. This is a network connection problem. You need external IP address so that outside clients can contact your PC. You need more advanced networking. The IP address provided from you ISP is used for domestic uses. You need specialized public IP address so that clients can find you outside of the firewall. This is networking/ISP/external IP issue.
Your server application needs to listen to incoming connections on a specified port, on a specified locally installed NIC. That is why TcpListener always needs to be created using a local IP address: because it only cares which NIC (if you have multiple installed) it should use.
The MSDN page for TcpListener also states it explicitly:
TcpListener Constructor (IPAddress, Int32) Initializes a new instance of the TcpListener class that listens for incoming connection attempts on the specified local IP address and port number.
External IP address is completely irrelevant to a TCP/IP server. You can have numerous routers and network devices along the way, which can then forward incoming connections to your machine.
Just do make sure that your firewall and router are configured properly to allow incoming connections on a specified port. To do that, start your TCP/IP server to open the port, and then use a service like CanYouSeeMe to see if server can be reached from outside.
Regarding your comment (this can prove unreliable on certain configurations), it's obviously "unreliable" when you think about it: a laptop can easily have an Ethernet network controller with a completely different IP address than a Wifi network adapter. Your server app should allow the user to select which IP address to use, instead of picking the first address it gets.

Unable to connect to local machine IP address using internet IP address

I've spent the last month writing a multiplayer game. I have only been testing it on one machine, using 127.0.01:9051 as the IPEndPoint.
I changed the IP address to my WAN IP, configured port forwarding on my router, configured my software firewall etc... But, it doesn't connect.
I have checked if the port is open using this site. Result: the port is really open.
Also, when I check the port from that website, my server receives packets just fine; however, when I connect from my own machine... it doesn't receive anything.
I've broken everything down to the basics to make sure it wasn't a problem with my code.
This basic code does not work:
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("XXX.XXX.XXX.XXX"), 9051);
TcpClient client = new TcpClient();
client.Connect(iep);
Any ideas are much appreciated.
If you are attempting to connect to the IP that is Forwarded (outsideIP) to the same machine (insideIP), it won't work. There are very few enterprise firewalls and no consumer devices (I know of) that will route a packet from the inside out and translate it back in.
[Internet] -- outsideIP[Router/Firewall]insideIP1 -- insideIP2[Computer]
In this case, packets from insideIP(X) will not be able to connect to outsideIP.
Try to see if you can telnet to your wan IP on port 9051. Routers don't really like it when you connect to their wan IP from within the network itself.
You could use Dyndns or some other service for the outside world to connect to your server and put the same name into your host file that resolves to 127.0.0.1 so that you can connect internally.
Is your client and your server (both the same machine in your failing test) using the same endpoint ip/port? You can only bind a port once at a time. Try using two ports one for server in one for server out (clinet in). That way they will not clash. Use Telnet to check you can connect to the IP/Ports you are using and Netstat to check they are not in use already.

GetExtendedUdpTable and Remote Ip Address

I've got some c# code that is mapping processes to ip addresses, I'm basically trying to write some software that will look at a process and give me the ip address so that I can write a monitor that will allow friends to find be in games.
When running my application though it detects that a game process has a UDP connection but I can't seem to get the ip address.
I've run a packetsniffer and 78.111.229.123:32000 shows up on the router/gateway but it's not showing in my application.
Any ideas to get me up and running?
How are you setting up the socket in the game application? Can you post an example of your socket setup code?
Are you using the wildcard address and port when creating the socket? I'm guessing that if your game application doesn't bind to a specific IP address and port explicitly that may be the source of your problem.
Another question: are you trying to detect the local IP address, remote IP address or both?
If you're trying to detect the remote IP address this may be impossible unless the game application is using what's known as connected UDP sockets. This is where the client application calls connect on the UDP socket to create an association between the local and remote sockets in the underlying OS. This has certain advantages and drawbacks as listed in the above link.
Edit
Take a look at this SO post: PID from socket number on Windows?
This pretty much confirms what I thought - unless the game is using connected UDP sockets then the only way you'll be able to get the remote address is via packet sniffing.
Start up the game and run netstat -ap UDP - if you see that there aren't any remote addresses listed then that would confirm the above suspicion.

Categories

Resources