Python Forum
tcp server/client port connection issue
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tcp server/client port connection issue
#1
Hi everyone,
I have quite a simple problem -
please look at my code snippet, this is my simple server:
import socket
def Main():
    
    host = 'localhost'
    port = 2222
    
    s = socket.socket()
    print(host)
    s.bind((host,port))

    s.listen(1)
    c,addr = s.accept()
    print("Connection from: " + str(addr) + "\n" + str(c))

    s.close()

if __name__ == '__main__':
    Main()
when it runs, it outputs just 'localhost'.
Next, this is the client on the same machine:
import socket

def Main():
    
    host = 'localhost'
    port = 2222
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host,port))
    
    s.close()
    
     
if __name__ == '__main__':
    Main()
            
When it starts after the server I get the same error:
Error:
============== RESTART: E:/Progs/Python 3.6.3/tcpServerTest.py ============== localhost ============== RESTART: E:/Progs/Python 3.6.3/tcpClientTest.py ============== Traceback (most recent call last): File "E:/Progs/Python 3.6.3/tcpClientTest.py", line 15, in <module> Main() File "E:/Progs/Python 3.6.3/tcpClientTest.py", line 9, in Main s.connect((host,port)) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it >>>
Well then, while I run TcpView from sysinternals I see four rows and somehow port 2222 is redirected(?) to another port:

-----------------------------------------------------local port-------------remote port------------------------
pythonw.exe--5620----TCP----127.0.0.1-----10137----0.0.0.0.0---------------------LISTENING
pythonw.exe--2960----TCP----127.0.0.1-----2222-----0.0.0.0.0---------------------LISTENING
pythonw.exe--5980----TCP----127.0.0.1-----10128----127.0.0.1------10125-------ESTABLISHED
pythonw.exe--5620----TCP----127.0.0.1-----10125----127.0.0.1-------10128------ESTABLISHED

If I start firefox on localhost:2222 it connects to the server and the server outputs:
Output:
============== RESTART: E:/Progs/Python 3.6.3/tcpServerTest.py ============== localhost Connection from: ('127.0.0.1', 10152) <socket.socket fd=444, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 2222), raddr=('127.0.0.1', 10152)> >>>

Notice that the remote port raddr is not 2222. Why?
What is redirecting port from 2222 to another?
How make my client connect to the server at the same port 2222?
I have windows 8, the same machine, the firewall is off, the router is off, no internet, no wifi while I carry out this experiment.
Reply
#2
Does replacing 'localhost' with '127.0.0.1' change anything?
Reply
#3
No, it doesn't.
It looks like the windows has its own shadow proxy server
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,277 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,422 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,361 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,754 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,757 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,192 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,317 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,336 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,066 Jun-02-2020, 01:08 PM
Last Post: nuffink

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020