-1

I have an assignment where I have to write TCP client server python programs to implement a basic voting application. There are only two candidates: JohnD and JaneD. I was testing the communication between the client and server. So when I run the client side, nothing happens. Could someone tell me why.

At first I was using "localhost" for serverName but I kept getting this error: clientSocket.connect(("localhost", serverPort)) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

So I set the serverName to my local IP. The client runs but nothing happens.

#TCP CLIENT#

from socket import*

#serverName = "localhost"

serverName = "131.100.39.41"

serverPort = 9001

clientSocket = socket(AF_INET, SOCK_STREAM)

clientSocket.connect(("131.100.39.41", serverPort))

option = input("Enter the voter name: ")

clientSocket.send(bytes(option, "utf-8"))

print("Name sent to server.", option)

clientSocket.close()

This is the server side.

#TCP SERVER#

from socket import*

serverPort = 9001

serverSocket = socket(AF_INET, SOCK_STREAM)

serverSocket.bind(("", serverPort))

serverSocket.listen(1)

print("Server is up and running.")

while(1):

    connectionSocket, addr = serverSocket.accept()

    option = connectionSocket.recv(1024)

    print("received from client", option)


serverSocket.close()

  • Do you close the client socket too early? Perhaps the server should send a response and the client wait for it. – quamrana Nov 06 '19 at 22:14
  • I doubt that's the case. The Client side isn't printing anything after clientSocket.connect(("131.100.39.41", serverPort)). Everything before it works – Apoorv Joshi Nov 06 '19 at 22:17
  • 'Connection refused' means the server wasn't running when you started your client. You forgot to close the accepted socket in the server. – user207421 Nov 06 '19 at 22:22
  • This is the real code copied and pasted from IDLE. Everything on the Client side runs until: clientSocket.connect(("131.100.39.41", serverPort)). i don't get any errors when I run the Server and Client. – Apoorv Joshi Nov 06 '19 at 22:31
  • But you need to run them *in that order*, and you also need to be sure that the client is connecting to the server's IP address. NB 'Client does nothing' is not the same thing as 'client got connection refused'. – user207421 Nov 06 '19 at 23:03

1 Answers1

-1

This code looks good. It is working.

You get connectionRefused error in client , because the PORT is not opened , or there is 2 channels opened for the port. The port is opened for listening only when the server is started.

check with 'netstat -anp | findstr ":9001" If there is port already opened , then choose another port or kill the port.

Remember:

  1. Change your socket back to localhost or 127.0.0.1
  2. First Start the server and then start the client

below is output i recieved , when i ran your code

Server

    Server is up and running.
    received from client b'test'

client:

Enter the voter name: test
Name sent to server. test
roshan ok
  • 383
  • 1
  • 6
  • 'Connection refused' means nothing was listening at that IP:port. It does not mean the port wasn't open, and '2 channels opened for the port' is meaningless. – user207421 Nov 06 '19 at 22:23
  • Thanks @roshan ok. I changed my socket to 127.0.0.1 and it's working now. What difference did that make though? – Apoorv Joshi Nov 06 '19 at 22:41
  • Please refer : https://stackoverflow.com/questions/7382602/what-is-the-difference-between-127-0-0-1-and-localhost – roshan ok Nov 06 '19 at 22:43
  • But , there was no issue in your code. I believe it was only the sequence of your execution.[ Server first and then client ]. – roshan ok Nov 06 '19 at 22:45
  • from your comment , it looked like you had ConnectionRefusedError . In my experience i had recieved this when there was 2 channels listening under same port. This normally appears when you use REUSE ADRESS and previous connection is not closed properly – roshan ok Nov 06 '19 at 22:46
  • '2 channels listening under same port' is still meaningless. – user207421 Nov 06 '19 at 23:04
  • @user207421 i appreciate your feedback. have attached a screenshot in the answer. Please let me know if that helps . same port 8081 , listening in different channels – roshan ok Nov 06 '19 at 23:25
  • @ApoorvJoshi please close the ticket if your query has been answered – roshan ok Nov 07 '19 at 02:34