How to let client application outside your netowrk connect to your server - sockets

I'm new to socket programming. I have developed fundamental/simple client and server application where client successfully communicates with server. Currently, both the server is on my system (local host) and client is also my system.
Now I want to somehow allow clients outside the network ( network where my system belongs to) to communicate with the server but i have no idea what to do and how to proceed. Any help would be appreciated.
Here's a smaple code taken from here
server.py
enter code here
import socket
# next create a socket object
s = socket.socket()
print "Socket successfully created"
port = 12345
s.bind(('', port))
print "socket binded to %s" %(port)
s.listen(5)
print "socket is listening"
while True:
# Establish connection with client.
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
client.py
import socket
s = socket.socket()
port = 12345
s.connect(('127.0.0.1', port))
print s.recv(1024)
s.close()

You don’t really need to change anything in your code, except for the IP that the client connects to. It needs to be the server PC’s public Internet IP instead of 127.0.0.1.
If the server PC is connected directly to the Internet modem, then you are done.
Otherwise, if the server PC is behind a router or proxy, then you need to configure port forwarding on that router/proxy to forward traffic from its public WAN IP/port to the server PC’s LAN IP/port. Consult your router/proxy’s documentation for how to do that configuration.
If the router/proxy has uPNP enabled, your server code can dynamically forward the WAN IP/port to itself at runtime. See Python: Open a Listening Port Behind a Router (upnp?)

Related

Client and Server connection with different port numbers?

I have set up a client and a server using sockets in python where my client sends data to the server, servers performs an operation, then returns some data to the client. Originally both the client and server were to have the same port number (9999). My issue currently is that I have to change the port of the server to 19999, and when I try to run, it does not work. The client is able to send data if its port is also changed to 19999, but it does not work if client is 9999 and server is 19999, which is what I need. New to networking systems so would appreciate any useful links to information or advice.
pic of client (left) and server (right)
Normally, clients will use an ephemeral port for its local port, and connect to the server port. Your client code is attempting to connect to port 9999, which is not the server port, which explains why it is not working. You need to connect to port 19999, since that is the port the server is listening on.
Using an unbound socket causes the client to choose an ephemeral port for its local address when making a connection. If you want the client to bind to a specific port, use bind before you call connect.
# bind locally to 9999
local_addr = (host, 9999)
s.bind(local_addr)
# connect to 19999
remote_addr = (host, 19999)
s.connect(remote_addr)

Socket Connection Without Knowing Port Number - Python

I would like to create a connection between a server and a client via socket on 2 different computers (but the question is relevant also to a connection on the same computer). I know the IP of the server, but I would like to connect without knowing the port number of the server - is there a way to do so?
for example:
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.bind((127.0.0.1, " "))
s.listen(1)
conn, addr = s.accept()
Is there a way to do so?
Can it also be done without knowing IP?
You can't open a socket without knowing the port number. However, you can use the predefined port numbers in the protocol. For example, if port is not specified, HTTP uses port 80 and HTTPS uses port 443 by default.

Socket ports are not matching listening ports

I am setting up a simple server using sys.net.Socket (cpp, linux).
The server is bound like this:
hostSocket.bind(new Host("0.0.0.0"), 20301);
And I connect to the server like this:
clientSocket.connect(new Host("localhost"), 20301);
If the ports do not match, the connection won't work, so that works as expected.
However, when I "accept" the connection on the server side, and want to print information about the client, I get a random port whenever a new connection is incoming, just never 20301:
var connectedClient : Socket = hostSocket.accept();
trace("Incoming connection from " + connectedClient.peer().host.toString()
+ " on port " + connectedClient.peer().port);
Now I get results like this:
Incoming connection from 127.0.0.1 on port 50977
Incoming connection from 127.0.0.1 on port 50978
Incoming connection from 127.0.0.1 on port 50979
What is going on here? Why is the displayed port not 20301?
Both server and client sockets need to bind to some local address (ip,port) for a connection to happen.
The client simply binds to a local free port, and will very likely change a lot, depending on all other connections happening on your machine.
Finally, Haxe sockets are (sometimes indirectly) wrappers over POSIX sockets; the spec for connect() says:
If the socket has not already been bound to a local address, connect() shall bind it to an address which, ..., is an unused local address.

TCP/IP Socket Programming Static Web IP between Dynamic IP

I am confused about TCP/IP Socket Programming. I know the internet protocols but in theory...
I am explaining my problem (what i need )
I have a server working on X.X.X.X IP Adress. And it always listens to Y PORT.
When i want to connect that server on MY PC, i have no problem because i wrote the ip adress (X.X.X.X) and the port (Y) and my PC connects..
When i connect to server server keeps my ip adress and my local ip adress.. After that connection is end.
Here is my problem starting...
As i sad my server knows my local PC informations. How can i connect to my local PC on server my web server and sent TCP or UDP packets ? I did port forwarding on my modem but i dont want it. When i did port forwarding there is no problem but i dont want it ...
Thanks for replies and sorry for my engislih if i have mistakes .
If a router/NAT sits between your server and an outside client, you MUST use port forwarding on the router. The outside client will NOT be connecting to your server's private LAN IP/Port directly, it will be connecting to the router's public WAN IP/Port instead. So the router needs to know to forward inbound packets to that IP/Port to the server's private LAN IP/Port.
A client running on your local PC is able to connect to the server because they are both on the same LAN side of the router, so the connection is direct and does not go through the router's WAN. That is not the case for clients that are on the WAN side of the router.

tcp socket server get name of server client used

Let's say I have a server, which has multiple domain names which resolve to its IP address. For example my server is 10.0.0.33 and can be accessed by serverA.mysite.com, serverB.mysite.com, and serverC.mysite.com. If I have a process running with code similar to the following:
#!/usr/bin/env python
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.listen(5)
while True:
client, client_addr = server.accept()
#server_name = <some function>(client)
#Do Something with the client knowing the value of servername...
server.close()
Is there a way to determine if the tcp connection made by the client was aimed at serverA.mysite.com or serverB.mysite.com...?
My Example is in python but I don't need a python specific answer.
No, TCP/IP connections work at the IP address level, so you cannot determine how the client obtained the IP address on which your server was listening.
HTTP works around this by requiring (since 1.0) that the client send the original host name as part of the request data.
I believe - you are listening on INADDR_ANY ("0.0.0.0") and would like to know exactly which one the client connected to, if you were listening on multiple ports? That's fairly simple you should use s.getsockname after the accept. So your code would look something like this
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 10000))
s.listen(5)
while True:
s2 = s.accept()
print s2.getsockname()
s2.close()