为什么创建本地Socket连接,创建不了,老是报错?
代码:packagejavapackage.chapter;importjava.net.*;importjava.io.*;publicclassSocketAppCl...
代码:
package javapackage.chapter;
import java.net.*;
import java.io.*;
public class SocketAppClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Socket socket=new Socket("127.0.0.1",1234);
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.writeUTF("XW");
DataInputStream dis=new DataInputStream(socket.getInputStream());
System.out.println(dis.readUTF());
socket.close();
dos.close();
dis.close();
}
}
异常显示:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at javapackage.chapter.SocketAppClient.main(SocketAppClient.java:13) 展开
package javapackage.chapter;
import java.net.*;
import java.io.*;
public class SocketAppClient {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Socket socket=new Socket("127.0.0.1",1234);
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.writeUTF("XW");
DataInputStream dis=new DataInputStream(socket.getInputStream());
System.out.println(dis.readUTF());
socket.close();
dos.close();
dis.close();
}
}
异常显示:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at javapackage.chapter.SocketAppClient.main(SocketAppClient.java:13) 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询