gpt4 book ai didi

java.net.SocketException : permission denied is arising

转载 作者:行者123 更新时间:2023-11-30 03:21:15 25 4
gpt4 key购买 nike

我尝试运行代码来使用 java swing 库创建 GUI,以将消息发送到本地主机服务器。GUI 包含一个用于键入消息的文本字段和一个用于将其发送到服务器的按钮。包含服务器代码在另一个类(class)。

当我尝试运行代码时,套接字异常在控制台中显示为:

java.net.SocketException: Permission denied: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)

请帮助我连接到服务器。

我在这里附上了 GUI 和服务器的代码。

用于创建 GUI 并建立与服务器的连接的类:

    package org.myorg;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.*io.IOException;
import java.io.PrintWriter;
import java.net.*;
import org.myorg.MessageServer;

public class SendMessage {
PrintWriter writer;
Socket sock;
JTextField text;

/*Creates GUI and adds SendButtonListener object to the button.
Calls setUpNetworking() method*/

public void go(){
JFrame frame=new JFrame();
JPanel panel=new JPanel();
text=new JTextField("Message",15);
JButton button=new JButton("Send");
button.setSize(5,5);
button.addActionListener(new SendButtonListener());
panel.add(text);
panel.add(button);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,200);
frame.setVisible(true);
setUpNetworking();
}

/*Listens to action event of clicking the 'send' button.
The message from the text field is written to writer object for sending it
to server*/

class SendButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
writer.println(text.getText());
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
text.setText("message");
text.requestFocusInWindow();
}
}

/*Establishes socket connection to the server. Object of PrintWriter is
created*/

private void setUpNetworking(){
try {
sock=new Socket("127.0.0.1",50000);
writer=new PrintWriter(sock.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/*Main method*/

public static void main(String[] args) {
SendMessage sendMessage=new SendMessage();
MessageServer server=new MessageServer();
sendMessage.go();
server.go();
}
}

服务器类别

    package org.myorg;

import java.io.*;
import java.net.*;

public class MessageServer {
Socket sock;

/*Accepts the socket connection and calls the read() method*/

public void go(){
try{
@SuppressWarnings("resource")
ServerSocket socket=new ServerSocket(50000);
sock=socket.accept();
read(sock);
}catch(IOException e){
e.printStackTrace();
}
}

/*Reads the message using InputStreamReader*/


public void read(Socket sock){
InputStreamReader stream;
try {
stream = new InputStreamReader(sock.getInputStream());
BufferedReader reader=new BufferedReader(stream);
System.out.println(reader.readLine());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}

最佳答案

首先,你必须运行服务器,

  public class MessageServer {
Socket sock;
/*Accepts the socket connection and calls the read() method*/
public void go() {
try {
@SuppressWarnings("resource")
ServerSocket socket = new ServerSocket(50000);
sock = socket.accept();
read(sock);
} catch (IOException e) {
e.printStackTrace();
}
}
/*Reads the message using InputStreamReader*/
public void read(Socket sock) {
InputStreamReader stream;
try {
stream = new InputStreamReader(sock.getInputStream());
BufferedReader reader = new BufferedReader(stream);
System.out.println(reader.readLine());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

public static void main(String[] args) {
MessageServer server = new MessageServer();

server.go();
}}

然后运行客户端“SendMessage.java”

 public static void main(String[] args) throws IOException {
SendMessage sendMessage=new SendMessage();
// MessageServer server=new MessageServer();
sendMessage.go();
// server.go();
}

关于java.net.SocketException : permission denied is arising,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com