import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.Frame;
import java.awt.TextArea;
public class Ace
{
public static void main(String[] arg)
{
new Abc();
}
}
class Abc extends Frame
{
private static final long serialVersionUID = 1L;
public Abc(){
this.setSize(600, 600);
this.setLocation(50, 50);
this.setVisible(true);
TextArea text = new TextArea("default words");
text.setSize(300, 300);
this.add(text);
this.socket();
}
void socket()
{
try{
Socket socket = new Socket("127.0.0.1",8798);
BufferedWriter buffer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
buffer.write("I love you");
buffer.close();
}catch(UnknownHostException e){
e.printStackTrace();
System.out.println(e.getMessage());
}catch(IOException e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Abc.socket(Ace.java:33)
at Abc.<init>(Ace.java:28)
at Ace.main(Ace.java:14)
这是异常信息,求大神赐教 而且我的服务端是OK的 netstat 看到了服务端监听了端口
------解决方案--------------------
很明显就是本机8798端口没打开,你自己试试 www.google.com, 80 这个主机和端口,就不会有这个连接错误了
------解决方案--------------------
java.net.ConnectException: Connection refused: connect 一般来说出现refused拒绝异常的话,基本上是服务没起或者端口没开