How to communicate with a computer in a different Network - c#

So I have already made the GUI and the program and all, and I want the option to enter an ip address on 2 computers in different networks (i enter his and he mine) and that we they communicate logs.
Both computers compute something and need to compare the values they compute, so i want to send arrays back and forth.
All that works already without the actual communication. I do not have a server I could use.

It doesn't matter in which network the PCs are for your program.
You just need to make sure the packages are routed correctly, so that it is physically possible to access one pc from the other (via its ip address).
In your program you can just use a TCP connection with the TcpClient and TcpListener class to send your data back and forth. All information you need is the IP and Port of the PC to connect to.
A third-party server you mentioned is not necessarily needed if you have just two computers communicating in the same way all the time

Related

C# Chat - TCP P2P

I am working on a Peer-to-Peer chat program but have ran across an issue: Running the client and server simultaneously. I do not want a dedicated server to manage connections. I believe the solution may be asynchronous direct connections, but I am not sure.
What I am trying to accomplish is to be able to run the program between two hosts, the program will be started and begin trying to connect to an ip address specified by a text box. At the same time, it will also start listening for incoming connections on the localhost ip address.
***I am using tcp, because on the off chance something is corrupted the message will not be able to be read (it is encrypted)
Issues:
1) It is conceivable a client could be waiting for a period of time before the other program tries to connect. So should some form of a loop must be utilized? If so, how?
2) I assume I need to use multi-threading, with one thread for the server part and one thread for the client part, but an issue is keeping them from hanging. Since both programs are identical there way be a way to listen and simultaneously attempt to connect to the other host.
3) I am also having trouble with making my server listen for connections to it, and do not know how to automatically have it pull the ip address from my computer.
Thanks for any help.
EDIT: This is on a LAN only.
Everything you need to know is in Microsoft's docs.
http://msdn.microsoft.com/en-us/library/w89fhyex.aspx

Remote access to SQL Server 2008 R2 [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Reminder: Not on local area network. Is there a need some configuration of SQL Server? What connection strings will be used? C#.net is the programming language.
I don't know much about c# or ms sql server 2008 however I have set up many servers
every computer connected to the internet has an address... I'm sure you know that
now, if you use a router you will know that the router is connected to the modem, and is distributed through the router either through a wireless connection or ethernet connection (the cord), the way the router organizes things, is each computer is assigned a private ip address (something like 192.168.1.2)... this private ip address cannot be accessed by the outside world at all... BECAUSE the router gets a packet, and doesn't know where to put it, so it immediatlely drops it (forgets it)
this is really safe, because this means the only way a bad person could reach your computer is if they were on the same network... however, sometimes you need to host a server that NEEDS to be accessed by the outside world...
this is explained in a bit, just read on
now also every internet packet (which is anything sent around a network) goes to a specific port
these are referenced with regular numbers: ie (port 80, 160, 55, et cetera)
the great thing about ports is that different computer programs can handle different ports, at the same time!!!
so a computer can be running multiple servers at the same time. because the corresponding packet is sent to the corresponding port and therefore a packets meant for a specific application won't interfere by another accidentally.
so in conclusion what we need to do is tell our router to say...
is the port equal to the port used in the ms sql server?
if so take the packet and send it to (the private ip address of your server)
this is called Port Forwarding
you'll need to first find the private ip address of your server.
on windows you would need to go in the command prompt and type:
ipconfig
in Linux and Mac OS X
ifconfig
once you find the private ip address of the server. make it a static... meaning it cannot change
you'll have to research this as I do not use Windows
remember: Static IP Address
now once you get a static ip address for your server you'll need the port(s) that ms sql server 2008 uses.
once you find that you need to set up port forwarding on your router
this varies from router to router, however, if you have a Netgear router you can go to the web admin page by pointing your browser to http://192.168.1.1/
this will bring you to the netgear router admin page. find port forwarding/port triggering on the sidebar and click add custom service, then add the input.
Now the last step is to find your public ip address, to tell your friend, this is the address he/she will access the server with
you can find this real easily by going to http://www.ipchicken.com/
this should work
HOWEVER
if you only have a modem and get your internet directly through that. you have a different situation
all the packets get sent to the one computer connected to WAN port (meaning everything gets sent to your computer regardless of port)...
if this is the case
you should forget about port forwarding and just give your friend your public ip found using ipchicken or another method....
this should work, unless ms sql has an extra security layer that I don't no about
hope this helped
EXTRA
Windows by default adds a firewall that blocks all connections, except for the exceptions that are established after a bit of use... so first you will need to punch a hole in Windows firewall by adding an exception... this is done by clicking on the current connection you are using and clicking on properties or something, don't know exactly but once you find out how to do that. it's just a matter of telling the firewall that port "whatever port ms sql uses" is safe.
After that is done, your friend should be able to connect by entering your ip address as the host.... I don't know much about C# as I said, but what your friend would do is use the nessecary procedures as he or she would have if they were connecting to any old sql server. except the "host" is the ip address of your server.
I don't quite know what you mean by connection string, could you try to give me more info on that please?
All SQL servers can connect over tcp/ip and MS is no exception. If your machine is behind a NAT router or a firewall you need only setup the appropriate port access. On most consumer grade equipment this is known as a pinhole. You must allow inbound to the SQL Server port of 1433 or whichever one you choose.
Most ISP's these days give you one WAN ( real world ) IP address and that is the wan side of your router. All machines on the LAN side are normally on a 192.168.x.x / 10.x.x.x / 172.16.x.x address scheme which are not routeable as all routers are programmed to ignore those address blocks and not to pass them on.
Please keep in mind unless you asked specifically for a static IP address your WAN side will most assuredly change from time to time and so you will need to let your friend know what it has changed to via something like ipchicken.com
Additionally since LOTS of attackers look on the standard ports for MS-SQL I highly suggest you use some random port above say 10,000 instead of the standard 1433 if your device does not have the ability to limit the source address.

Establish a P2P Connection in C#

I realize this question is similar to some others, but I figured my situation is sufficiently different to warrant its own question (hopefully).
What I'm planning on is deploying a program on another person's computer which will open a connection to my computer after which, I'm assuming, the computers should be able to communicate with each other. Once the program starts, it should be able to pull the address information and port (if they aren't blocked) to create a connection, right?
What's more is the internet configuration of the dorm I'm living in. Every room is assigned a unique port and a static IP address assigned by a DHCP server. How do I factor this into the design of my program?
I'm thinking that this setup does not require a server as an intermediate access point, as my address details will always remain the same and the host computer can simply connect to my computer without further information. Is this correct?
Finally, I am reading a few pages about creating a connection, but am confused with all the possibilities of TcpConnection, WCF, CORBA, etc. Which one would actually be the simplest one for me to start with assuming I only want to send messages to the other machine?
Update:
The address is static in the sense that it doesn't change. My IP address is of the form 130.83.20.xxx and I can either wait for the DHCP server to assign me this address, or I can manually enter it myself using a static IP configuration.
As for the messages itself, simple text messages will suffice for the start. The ports mentioned before are the switch ports and do not come into play during network programming I believe.
I would go with TcpClient and TcpListener. Check out the example code on MSDN, copy and paste it into two C# console projects and build them.
I would use 127.0.0.1 (localhost) for testing purposes on port 5001 (a commonly used test port).
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 5001);
Then you should be able to test a simple client/server that runs on your computer only. Once you get that working, you can take the client to another computer in your dorm and make sure it still works. Once that works, you can go to a local coffee shop and take the client with you. Leave the server running at a known IP address in your dorm. In this case, have the server bind to your actual external IP (not localhost). You can do this by just specifying a port to the TcpListener constructor: `
TcpListener server = new TcpListener(5001);
Once you get all that working by yourself or with a friend, then send it external. Better to get these things working in a demo before sending it to your customer and having him troubleshoot with you. :)
Reasoning behind my answer:
Simple TCP client/server is very straightforward and will allow a simple chat program between two computers. Then you can beef it up to send any data stream you want. You can add code like this to get access to a StreamWriter:
NetworkStream stream = client.GetStream( );
StreamWriter writer = new StreamWriter(stream);

C# and ActiveSync communication / open and read files

I have a device connected to a host computer through cradle usb. Now, I'm just wondering if I could use C# sockets to communicate with the device (ie device sending data, host computer processing it then replying back to the device). How can I accomplish this? by that, what ip address etc etc.. do I have to change so that it would connect cause I have the sockets working on wireless. If not, then is there a way to connect to the device, open and read a file (a text document to be more specific) from the device to my host application.. any ideas?
Thanks! :)
Depending on your target device, when you connect via ActiveSync it likely makes a local RNDIS network connection between teh two devices. You can resolve "ppp_peer" as the partner's network name instead of trying to use a hard-coded IP address (IIRC the IP is different on XP than on Vista).
Be aware that it's not a full connection. TCP packets gets passed through, but things like ICMP do not.
Of course, this just gives you a socket connection, just like if you were to connect between two PCs. It's not going to allow you to do file system operations unless you have an app on the other side listening for commands. If you want that type of thing, Microsoft provides the Remote API (RAPI) interface (wrapped in managed code here)for a lot of basic commands, and it can be extended (with C) to do anything you'd like.

how can i send data over internet to any specific computer in subnet

I want to know how I can send or recieve data over internet to/from a computer in subnet
(this is specially in context to PPP users bcoz getting static IP is not so much in practice).
I actually want to create an application which can transfer file between 2 specific computer in WAN.
so what are things I need to know about to do the same..(ex. PRESENT IP or MAC ADDRESS etc..)
PROGRAMATICAL EXPLANATION ALTHOUGH PREFFERED,BUT IS NOT NECCESARY...
FTP?
There is a vast torrent of useful results in google, I seriously suggest to google before you ask here.
For instance, have a look at the top result: http://www.devarticles.com/c/a/C-Sharp/Network-Programming-in-C-sharp/
Apart from that, FTP, as suggested by Colin, may be what you're looking for. If you're new to using FTP in C# have a look at http://www.google.com/search?q=c%23+ftp
Put the information on a public IP server, so both computers keep polling if there is new data and send / download that data as needed. A single text file can hold necessary flags such as paths and other info you need.
server just need to be a web server, which means IIS or similar should be installed.
I'm thinking part of your question has to do with one of the computers not having a static IP address. If the two computers, A & B, don't know each others IP address, then an alternative is to use a server. Either A can store the information on the server to be picked up by B, or A can register his current IP address on the server for B to lookup and then connect to A, assuming firewalls don't interfere. Another scheme is that A and B can simultaneously connect to the server, and the server can relay data between the two.
Of course, all of this communication would be done using techniques such as those suggested by mafutrct and Colin.
This question sounds to me like the difficulties of NAT Traversal and trying to establish peer-to-peer connectivity over the Internet. I stumbled on this question researching the best way to set up Internet connectivity to a device that is sitting behind a firewall. I am assuming that the device behind the firewall has to initiate the connection, that a Internet server application (on a Public IP address) to at least manage the initiation of the connection is required, and the Internet server application may also have to act as a relay if the difficulties of NAT is not able to be traversed.
http://en.wikipedia.org/wiki/NAT_traversal
http://en.wikipedia.org/wiki/Peer-to-peer
A good example of this is LogMeIn. Where the application is installed on the computer that needs to be accessible over the Internet, that application communicates with the LogMeIn Internet servers, you can establish a connection to that computer either by initiating through the LogMeIn servers and connecting peer-to-peer over port 80/443 using NAT Traversal (this would be establishing a peer-to-peer connection) or by initiating through the LogMeIn server and the LogMeIn server acting as a relay if peer-to-peer is unable to be established.
Please note; you must have an application (something running) on both ends of the connection on the Internet, there is no other way. Just like FTP that was mentioned earlier (FTP Client, FTP Server)... but in this case it is peer-to-peer (basically you are writing your own Client and Server... or if you have to use a relay... Client--Relay--Server
http://en.wikipedia.org/wiki/LogMeIn
"The service connects the remote desktop and the local computer using SSL over TCP or UDP and utilizing NAT traversal techniques to achieve peer-to-peer connectivity when available."
The part I don't know is can the relay go in both directions; and I am figuring the device behind the firewall must have to constantly manage the open connection to the Internet Server that is the relay. Those are the questions I am wrestling with now.
Also, you may want to check out this post.
C# byte streams through TCP
From that time, till this date I have encounter so many problems with these four kinds of NAT that basically without an external server you cant do anything.
This is because, one computer can never find where the other one is located as suggested in the pic.
Here PC 'A' will never come to know about the port that corresponds to PC 'B' on R2B without an external server.Even more, if somehow u come to know about the ports you are still in a situation that the router wont allow you to access it if the request hasn't been made for your IP.

Categories

Resources