Python Forum
Simple TCP Client and TCP Server Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple TCP Client and TCP Server Problem
#1
OK, I may be being particularly thick with this question but I am new with python and I am trying to follow a PDF book on coding with it, we have reached the point where he has had me create a simple TCP Server (named: tcp_server.py)
import socket
import threading

bind_ip = "0.0.0.0"
bind_port = 9999

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind((bind_ip,bind_port))

server.listen(5)

print ("[*] Listening on %s:%d" % (bind_ip,bind_port))

#this is our client handling thread
def handle_client(client_socket):
    
    #print out what the client sends
    request = client_socket.recv(1024)
    

    print ("[*] Received: %s" % request)
    
    # send back a packet
    client_socket.send("ACK!")
    
    client_socket.close()
    
while True:
    
    client,addr = server.accept()
    
    print ("[*] Accepted connection from: %s:%d" % (addr[0],addr[1]))
    
    #spin up our clirnt thread to handle incoming data
    client_handler = threading.Thread(target=handle_client,args=(client,))
    client_handler.start()
and a simple TCP Client (named: tcp_client.py)
import socket

target_host = "www.google.com"
target_port = 80

# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect the client
client.connect((target_host,target_port))

# send some data
client.send('GET / HTTP/1.1\r\nHost: google.com\r\n\r\n')

# receive some data
response = client.recv(4096)

print (response)
in the book he says if I pass the client to the server as an argument then I should get responses like this

Quote:[*] Listening on 0.0.0.0:9999
[*] Accepted connection from: 127.0.0.1:62512
[*] Received: ABCDEF

I have tried passing the filename to many places in the server code but all I ever end up with is
Quote:[*] Listening on 0.0.0.0:9999

and the command windows locks out and I have to close it down, could someone point me in the right direction, I dont want to stray too far away from the book with the coding as the rest of it may not make sense, but where and how would I pass the client filename into the server file for it to do what he says it should do?

many thanks in advance and stay safe
Reply
#2
If you set target_host = "localhost" and target_port = 9999 it should work.
Otherwise, your server doesn't get the request because your host is not www.google.com.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
P.S. I am using Python 2.7 on Linux

the question I should have reiterated is where in the server file do I place the name of the client file to make it function?
Reply
#4
You should avoid the use of Python 2.7. It's deprecated.
Use the right host. If you're accessing www.google.com it will not connect to your server.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
thanks for the answers so far DeaD_EyE, apart from using a deprecated version of Python, the real question was where do I put the filename of the client into the server code to make it function?

in fact I going to stop here, upgrade to version 3 of python and start off on the right footing, I will have only lost a day and a half to 2.7, so not such a bad thing, thanks for the help though
Reply
#6
You don't put filenames anywhere. TCP is a network protocol and the way you specify where to connect to is to specify the host and port of the server. The host should be either an IP address, or the hostname. Right now, your client is connecting to www.google.com on port 80 (lines 3-10). Your server is running on your machine on port 9999 (lines 5-9) and is accessible on all IP addresses on your machine (line 4). DeaD_Eye has given you the right information - "localhost" is a special hostname meaning your machine (you could also use the IP address 127.0.0.1) and the port is 9999 as in the server code.

Which book are you using? Does the author not cover the basics of what TCP is and how it works?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,274 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Asyncio | Websockets - general problem starting the server dreamer 5 3,185 Oct-26-2022, 11:55 AM
Last Post: dreamer
  Client/Server proper finalizing transfer wolfman5874 1 1,421 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
  Socket server problem H84Gabor 0 1,206 Jun-21-2022, 12:14 AM
Last Post: H84Gabor
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,360 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,751 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,756 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,191 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,311 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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