TAGS :Viewed: 3 - Published at: a few seconds ago

[ Connecting Python and Java via sockets ]

I was trying to implement a simple server program in python and client program in java.

Python server runs well. And Java client compiles good. But cannot connect to Python server.

This is my server.py

import sys
import socket

HOST = ""
PORT = 8888

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket created")

    s.bind((HOST, PORT))
except Socket.error as msg:
    print("Bind failed.")
    print("Error code: "+str(msg[0]))
    print("Message: "+str(msg[1]))
print("Socket bind complete")

print("Socket now listening")

while 1:
    conn, addr = s.accept()
    print("Connected with: "+addr[0])


And this is my Client.java file:

import java.net.Socket;
import java.io.IOException;

public class Client {

    public static void main(String[] args) {

        try {

            Socket socket = new Socket("localhost", 8888);
            System.out.println("Socket closed");
        } catch(IOException exception) {


Python server listens to Port 8888. But Java client cannot connect.

This is the output when I run Client:

java Client  

And the output:

    java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at Client.connect(Client.java:84)
    at Client.<init>(Client.java:65)
    at Client.main(Client.java:301)
Error connecting to localhost at 5555   

Why can't it connect? When I write an equivalent Java server program, it works fine.

This is my Java server: Server.java

import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.IOException;

public class ChatServer {

    public static void main(String[] args) {

        try {

            ServerSocket server = new ServerSocket(8888);
            Socket socket = server.accept();
            InetAddress ip = socket.getInetAddress();

            System.out.println("Client connected: "+ip.getHostAddress());
        } catch(IOException exception) {


So my question is short:
Why is Client.java connected to Server.java but not Server.py?

Any help would be appreciated. Thankyou.

Answer 1

Seems like you never cleanly close the socket in your Python code, so it could still be stuck from a previous run attempt? Also, your python program currently doesn't do anything with the connection once received, just keeps trying to accept.

Try something like this:

    conn, addr = s.accept()
    print("Connected with: "+addr[0])
    #Waits for any incoming data and echoes it back
        data = conn.recv(1024)
        #break if not found to get to close statement which was never reached in old code
        if not data: break 
        print("Received data:", data.decode())

Answer 2

I use the exact Python code and Java code you post, it seems work fine, may be something wrong with your environment? you can diagnosis the problem with following steps(assume you run your program in a linux box like me):

1.just run the python server script


2.use netcat to see if the server is good for connect

nc -zv localhost 8888

if things goes well, you may see connect established as expected. if not working, use netcat to build a tcp server listen on port 8888

nc -l -p 8888

and connect to it with your java program.

you can also use netstat to distinguish if server.py works fine

sudo netstat -ntlp | grep :8888

Answer 3

I think I can solve your problem, but I do not know why. You can have a try by adding an item on firewall config with

-A INPUT -i lo -j ACCEPT

then, restart your iptables service.