Basic python socket server application doesnt result in expected output

2024/10/14 10:42:17

Im trying to write a basic server / client application in python, where the clients sends the numbers 1-15 to the server, and the server prints it on the server side console.

Code for client:

import socket
clientsocket.connect(('localhost', 8303))
def updateX():x = 0while (x < 15):xclientsocket.send(format(x))print xx = x+1updateX()

server:

import socketHOST = 'localhost'
PORT = 8303
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5) # become a server socket, maximum 5 connections
connection, address = s.accept()while True:connection, address = s.accept()buf = connection.recv(64)print buf

The output of running the client while the server is live results in either no output, prints only 1, or prints only 12. Ideas?

Answer

Before entering the main loop on the server side, you accept a connection:

connection, address = s.accept()

But then in the loop itself you begin by accepting a connection:

while True:connection, address = s.accept()buf = connection.recv(64)print buf

As a result, you never read from the first connection. That's why you don't see any output.

Note also that it's wrong (for what you're trying to do) to accept a new connection on every iteration. Even if you keep making new client connections, the server will accept a connection on each iteration and read from the socket once, but then continue the next iteration and wait for a new connection, never reading more data sent by a client. You should be making multiple recv calls on the same connection object instead.

You might find this tutorial helpful.

https://en.xdnf.cn/q/117968.html

Related Q&A

creating dictionaries to list order of ranking

I have a list of people and who controls who but I need to combine them all and form several sentences to compute which person control a list of people.The employee order comes from a txt file:

Python: How to use MFdataset in netCDF4

I am trying to read multiple NetCDF files and my code returns the error:ValueError: MFNetCDF4 only works with NETCDF3_* and NETCDF4_CLASSIC formatted files, not NETCDF4. I looked up the documentation a…

Pyspark: Concat function generated columns into new dataframe

I have a pyspark dataframe (df) with n cols, I would like to generate another df of n cols, where each column records the percentage difference b/w consecutive rows in the corresponding, original df co…

Mysql.connector to access remote database in local network Python 3

I used mysql.connector python library to make changes to my local SQL server databases using: from __future__ import print_function import mysql.connector as kkcnx = kk.connect(user=root, password=pass…

concurrent.futures not parallelizing write

I have a list dataframe_chunk which contains chunks of a very large pandas dataframe.I would like to write every single chunk into a different csv, and to do so in parallel. However, I see the files be…

Querying SQLite database file in Google Colab

print (Files in Drive:)!ls drive/AIFiles in Drive:database.sqlite Reviews.csv Untitled0.ipynb fine_food_reviews.ipynb Titanic.csvWhen I run the above code in Google Colab, clearly my sqlite file is pre…

AttributeError: function object has no attribute self

I have a gui file and I designed it with qtdesigner, and there are another py file. I tried to changing button name or tried to add item in listwidget but I didnt make that things. I got an error messa…

Find file with largest number in filename in each sub-directory with python?

I am trying to find the file with the largest number in the filename in each subdirectory. This is so I can acomplish opening the most recent file in each subdirectory. Each file will follow the namin…

Selenium Python - selecting from a list on the web with no stored/embedded options

Im very new to Python so forgive me if this isnt completely comprehensible. Im trying to select from a combobox in a webpage. All the examples Ive seen online are choosing from a list where the options…

How to use a method in a class from another class that inherits from yet another class python

I have 3 classes :class Scene(object):def enter(self):passclass CentralCorridor(Scene):def enter(self):passclass Map(object):def __init__(self, start_game): passAnd the class map is initiated like this…