Windows Phone P2P TCP/IP Connection - sockets

I'm working on a Windows Phone 8 app (game). In the app, I need to be able to start a TCP/IP connection with another phone (lobby match-up), and then send messages to and from, without a centralized server. I am currently following this tutorial, however, I need connect to another Windows Phone, not a server. Is this possible without a centralized server? If so, how might I go about doing it? If not, would I be better off using PeerFinder or some other P2P type connection finder?
Alternatively, could I find another phone using PeerFinder and set up a socket connection there?
I guess in essence, my question is: what are the alternatives/what are my options if I want to connect to another Windows Phone, but without a central server?

First of all sorry about my English.
Most probably you can use wifi for the work.its not good enough to make a program that is always listening to a port. Because it makes a security hall. So it is better to open the TCP port only when you need it also make it to manual as much as possible.
To connect two phones together you can follow the following procedure.
First of all you need to specify a conmen TCP port for the all users
INITIAL HANDSHAKING
Make sure that relevant application is running on both mobile phones.
Make the application to auto on wifi when it's initiating it self.
Make a button call "Search for users".
when user click it open a specific TCP port which is unique for the application.
then search for the open wifi connections.
send a TCP packet for the selected port which you set for the application and wait for the acknowledgement.
List all the connections who sent the acknowledgement (Available application users).
SELECT A USER
Enable user to select one of the user from available list
Then send a TCP request to selected user.
Receiver can accept or reject it
by accepting it user make his/her TCP end point to IP that sends data.
Then you can simply transfer the data via opened port.
its better if you can allocate list of TCP ports to use. then you can write an algorithm to select another when one is allocated by another program.
I think this will be useful for you.

Related

Why do outgoing sockets need port numbers?

I understand why a server would need sockets for incoming data, but I do not understand why it is necessary that a socket connecting to another computer needs a source port.
While others have mentioned the exact reason why, let me illustrate the point by giving you an example:
Say you want to ssh to your server. OK, you ssh in and do some stuff. Then you tail a log file. So now you don't have access to the console anymore. No problem you think, I'll ssh again...
With one port number, if you ssh again that second connection will be a mirror of the first since the server won't know that there are two connections (no source port number to tell the difference) so you're out of luck.
With two port numbers you can ssh a second time to get a second console.
Say you browse a website, say Stackoverflow. You're reading a question but you think you've seen it before. You open a new tab in your browser to stackoverflow to do a search.
With only one port number the server have no way of knowing which packet belongs to which socket on the client so opening a second page will not be possible (or worse, both pages receive mixed data from each other).
With two port numbers the server will see two different connections from the client and send the correct data to the correct tab.
So you need two port numbers for client to tell what data is coming from what server and for the server to tell what data is coming from which socket from the client.
A TCP connection is defined in terms of the source and destination IP addresses and port numbers.
Otherwise for example you could never distinguish between two connections to the same server from the same client host.
Check out this link:
http://compnetworking.about.com/od/basiccomputerarchitecture/g/computer-ports.htm
Ultimately, they allow different applications and services to share the same networking resources. For example, your browser probably uses port 80, but your email application may use port 25.
TCP communication is two-way. A segment being sent from the server, even if it is in response to a segment from the client, is an incoming segment as seen from the client. If a client opens multiple connections to the same port on the server (such as when you load multiple StackOverflow pages at once), both the server and the client need to be able to tell the TCP segments from the different connections apart; this is done by looking at the combination of source port and destination port.

Confusion over Sockets and Ports

I am trying to write a programme that will 'listen' to application that is running on a port over TCP/IP.
When I point my browser to localhost:30003 , I get the output stream from the application printed to the screen. It would appear that the browser successfully 'listens' to the port.
What is happening here? Is my browser polling the application or is the application pushing tcp data which the browser picks up?
I am not sure whether to get this data I need to create a client or server instance.
One of the best ways to find out what is actually happening is to fire up Wireshark and follow the tcp stream.
http://www.wireshark.org/
Alternately, you can use something like TCP mon if you only care about the text, and none of the networking details.
http://ws.apache.org/commons/tcpmon/download.cgi
Based on the limited information in your question, the most likely thing is that the browser makes the tcp connection, and you send back a malformed response. The brower assumes you are a broken site, and does it's best to adjust. If you aren't sending the correct http header, it dosn't know what else to do so it probably just puts the text on the screen.
Best way to know the details is with wireshark or tcpmon
Pointing the browser to localhost:30003 will cause it the open the connection to port 30003 on the localhost and sent the string "GET /" to request a web page from what is thinks is a web host. Whatever text is sent by your app upon receiving a connection is simply displayed by the web browser as if it had received the contents of a text file on a web server.
when you write "localhost:30003" in your browser a connection is established to some program that listens to the port 30003 on your computer. The prefix in the URL, (default HTTP) determines the protocol used by server and client, in this case the browser is the client connecting to your PC, the server.
If you want to do the same with your program you can set up a socket connection to your localhost using the same port 30003. Your program then becomes the client. Depending on the program (which you don't mention anything about) you may have more protocol options and would need to handle the protocol in your program.
An alternative is to use telnet to connect to your program but it depends on available protocols.

Lan chat design

I'm in the process of trying to write a chat application and I have a few issues
that I trying to work out. The application is basically a chat application that works on a Lan. One client acts as the
host and other clients can connect to the host and publicly chat among themselves. I want also the option of a client starting
a private chat with an already connected client. So what is the best way for this to happen. For example should the request message (which
contains the ip address of client) route through the host and then if the requested client wants to connect , then they initiate the connection
using ip of the requesting client. Should this also be on a separate port number. Does it matter if your application uses a number of ports.
Or, when ever a client connects to a host, the host should send them a list of users with there ip addresses, and then the client can
attempt a connection with the other client for a private chat.
Hope this all makes sense. Any help would be appreciated
Thanks
If you are just interested in a quick-and-dirty chat facility that only needs to work over a LAN, I'd suggest having all clients send and receive broadcast UDP packets on a single well-known port number. Then no server is necessary at all, and thus no discovery is necessary either, and things are a lot simpler.
If you really want to go the client-server route, though, you should have your server (aka host) machine accept TCP connections on a single well-known port, and then have it use select() or poll() to multiplex the incoming TCP connections and forward any data that comes in from each incoming TCP socket to all of the others sockets. Clients can connect via TCP to the server at this well-known port, but the clients will have to have some way of knowing what IP address to connect to... either from having the user type in the IP address of the server, or by some discovery mechanism (broadcast UDP packets could be used to implement that). This way is a lot more work though.
I'm all for creating my own but depending on time constraints sometimes I look for alternatives like this I used it in a company I worked at before. It's really good. But if you decide to make your own you first have to map out a logic, structure, Database and so on before you even think about code..

How do I design a peer-to-peer app that avoids using listening sockets?

I've noticed that if you want to write an application that utilizes listening sockets, you need to create port forwarding rules on your router. If I want to connect two computers without either one of the the computers messing about with router settings, is there a way that I can get the two clients to connect to each other without either of them using listening sockets? There would need to be another server somewhere else telling them to connect but is it possible?
Some clarifications, and an answer:
Routers don't care about, or handle ports, that is the role of a firewall, which do port forwarding. The router/firewall combined device most of us have at home adds to the common misunderstanding.
Can you connect two computers without ServerSocket? No. You can use UDP (a stateless, connectionless communication protocol), but the role of a ServerSocket is to "listen" for incoming connection requests, and generate a Socket from those requests, which creates a communications channel between two endpoints. A Socket has both an InputStream and an OutputStream, so it can both read at write at either end. At that point (once the connection is made), the distinction between client/server is arbitrary, since a Socket is a two-way connection object, which allows both sides to send/receive.
What about proxying? Doesn't that allow connections between two computers without a ServerSocket? Well, no, because the server that's doing the proxying still has to be using a ServerSocket. Depending on what application you're trying to implement, this might be the way to go, or or might just add overhead. Even if there were "another server somewhere else telling them to connect", somebody has to listen for a connection request, which is the job of the ServerSocket.
If connections are happening over already open ports (most publicly accessible servers have ports <1024 not blocked by firewalls, but exceptions exist), then you shouldn't need to change firewall settings to get the connection to work.
So, to reiterate, the ONLY role of a ServerSocket (as far as your question is concerned) is to listen for incoming connection requests, and from those requests, create a Socket, which is a two-way communications channel between the two end points.
To answer the question, "How do I design a peer-to-peer app that avoids using listening sockets?", you don't. In the case of something like Vuze, the software acts as both client and server simultaneously, hence the term "peer", vs. "client" or "server" alone. In Vuze every client is a server, and every server (except for the tracker) is a client.
If you need a TCP connection between the 2 computers and both of them are behind routers (and you don't want to set up port forwarding) I think the only other possibility you have is having a third server somewhere that isn't behind a firewall running a ServerSocket and accepting connections between your 2 other computers and proxying communications between the 2. You can't establish a TCP Connection between the 2 without one listening to a socket and the other connecting to it.
Q: If I want to connect two computers without either one of the the
computers messing about with router settings, is there a way that I
can get the two clients to connect to each other
Yes: have the server listen on an open port :)

C++ Winsock API how to get connecting client IP before accepting the connection?

I am using the Winsock API (not CAsyncSocket) to make a socket that listens for incoming connections.
When somebody tries to connect, how can I get their IP address BEFORE accepting the connection? I am trying to make it only accept connections from certain IP addresses.
Thanks
SO_CONDITIONAL_ACCEPT socket option. Here
Also, pretty sure it's available in XP and Server 2003, not just Vista.
Two reasons why I do not want to accept the connection in order to check the remote IP address:
1). The client would see that there is a listening socket on this port. If i decide to reject the client connection, I would not want them to know that there is a socket listening on this port.
2). This technique is not as efficient and requires more CPU, RAM, and network usage; so it is not good in case of a Denial Of Service attack.
When using ATM, the CONNECT ACK packet will come from the most recent switch, not the end client. So, you would have to call accept() on the socket, then look at the address (based on the passed addr_family), and at that point just close the socket. By the time it reaches the requester, it will probably just get a failure.
And I'm not sure how many resources you think this will take up, but accepting a connection is at a very low level, and will not really be an issue. It's pretty easy to drop them.
If you come under a DoS attack, your code CAN quit listening for a preset amount of time, so the attacker just gets failures, if you are so worried about it.
Does it really matter if the client knows there is a socket listening? Try using telnet to connect to your localhost on port 137 and see how fast the file sharing in windows drops the connection... (If you even have it enabled, and if I remembered the correct port number.. heh..)
But, at the SOCKET level, you are not going to be able to do what you want. You are talking about getting down to the TCP level, and looking at the incoming connection requests, and deal with them there.
This can be done, but you are talking about a Kernel driver to do it. I'm not sure you can do this in user-mode at all.
If you want Kernel help with this, let me know. I may be able to give you some examples, or guidance.
Just my own two cents, and IMVHO...
accept the connection, look at the IP, if it is not allowed, close the connection
Edit:
I'm assuming you're talking about TCP connection. When you listen to the port and a connection comes from a client, the API will perform the TCP 3-way handshake, and the client will know that this port is being listened to.
I am not sure if there is a way to prevent sending any packets (i.e. accepting the connection) so that you can look at the IP address first and then decide.
The only way I can think of is to do packet filtering based on the source IP at the network layer (using firewall, for example).