简体   繁体   中英

Java.net.SocketException: Permission denied: connect

I have two PCs in one network that I want to connect. One of them should send a notification to the other via TCP. One the one PC I have a "server" (Python script) socket which waits for the "client"(Jar file) to send a specific String and then it gives me a notification. This works perfectly fine when I'm trying it out one one PC. But when I want to do the intended action the "client" PC's .jar gives me an error that the connection is refused. Do I have to open a specific port on the other PC or what else could cause trouble? One PC runs Fedora the other Windows 8

"Server Code"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind(("", 5005)) 
s.listen(1)

try: 
    while True: 
        komm, addr = s.accept() 
        while True: 
            data = komm.recv(1024)
            if data == "$":
                noty()
            if not data: 
                komm.close() 
                break
finally: 
    s.close()

"Client" Code

public static void main(String[] args) throws Exception {
Socket socket = new Socket("192.168.178.25", 5005); 
OutputStream out = socket.getOutputStream();
String dat = "$";
    out.write(dat.getBytes());
socket.close();
}

if its permission denied then something is blocking your connection with the computer. i would try to open a port and see if that works. if you want an example of java sockets you can take a look at my SUPER Tic-Tac-Toe Multiplayer it uses java sockets to send strings to the clients as a way to represent what actions the clients should take.

Your server is probably binding to the wrong interface,

calling

s.bind(("", 5005)) 

Without setting an interface will allow the program to pick what ip address / interface it will connect to.

Since your client is trying to connect to ("192.168.178.25", 5005); you may want to put an IP address into the bind call to prevent the server picking the wrong ip interface.

Example:

s.bind(("192.168.178.25", 5005)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM