使用本地主机上的套接字发送消息
Sending messages using sockets on local host
我正在本地计算机上通过套接字进行消息传递。我正在尝试向自己发送消息,这是在模拟我通过互联网发送消息。无论如何,我早些时候遇到了一个绑定异常,但似乎我已经过去了。现在我得到了 :
Exception in thread "main" 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 class2.main(class2.java:10)
我如何让这个连接正常工作并传递我的消息。这是我程序中的两个类:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class IPMessenger {
public static void main(String[] args) throws IOException{
ServerSocket SC = new ServerSocket(4800);
Socket socket = SC.accept();
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
System.out.println(in.readDouble());
System.out.println("hi");
// out.writeChars("hello");
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class class2 {
public static void main(String[] args) throws UnknownHostException, IOException{
Socket sock = new Socket("localhost",4800);
DataInputStream in = new DataInputStream(sock.getInputStream());
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeDouble(5);
//System.out.println(in.readChar());
}
}
java.net.ConnectException: Connection refused: connect
当您 运行 客户端时,您的服务器未 运行ning。
我正在本地计算机上通过套接字进行消息传递。我正在尝试向自己发送消息,这是在模拟我通过互联网发送消息。无论如何,我早些时候遇到了一个绑定异常,但似乎我已经过去了。现在我得到了 :
Exception in thread "main" 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 class2.main(class2.java:10)
我如何让这个连接正常工作并传递我的消息。这是我程序中的两个类:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class IPMessenger {
public static void main(String[] args) throws IOException{
ServerSocket SC = new ServerSocket(4800);
Socket socket = SC.accept();
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
System.out.println(in.readDouble());
System.out.println("hi");
// out.writeChars("hello");
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class class2 {
public static void main(String[] args) throws UnknownHostException, IOException{
Socket sock = new Socket("localhost",4800);
DataInputStream in = new DataInputStream(sock.getInputStream());
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeDouble(5);
//System.out.println(in.readChar());
}
}
java.net.ConnectException: Connection refused: connect
当您 运行 客户端时,您的服务器未 运行ning。