我正试图这样做:
import java.net.*;
import java.io.*;
public class MainProgram {
private static String host;
public static void main(String[] args)
{
host = "localhost";
for (int i = 1; i < 10001 ; i++)
{
try {
Socket socket = new Socket(host , i);
}catch (UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
但输出始终是Connection Refused。以下是输出的一部分:
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)
有什么方法可以解决这个问题吗?我的远程Java应用程序的调试配置如下所示:
http://i.stack.imgur.com/Vbm0l.png
我安装了WAMP服务器,也许我应该使用它?我不知道。
答案 0 :(得分:1)
使用您的本地IP地址,而不是使用&#34; local&#34; 使用&#34; cmd&gt; IPCONFIG&#34;找到你的localhost的IP地址,然后按如下所示添加:
import java.net.*;
import java.io.*;
public class MainProgram {
private static String host;
public static void main(String[] args)
{
host = "192.168.0.13";
for (int i = 1; i < 10001 ; i++)
{
try {
Socket socket = new Socket(host , i);
}catch (UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
请记住,这是我本地主机的IP!你可以成为别的东西。
答案 1 :(得分:0)
您的localhost中没有监听serverocket。您需要运行服务器套接字来侦听将由客户端连接的端口。例如:
Server Socket:
+++++++++++++++++++++++++++++++++++++++++++
enter code here
package com.jdbc.test.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Socket socket=null;
BufferedReader br=null;
PrintWriter pw=null;
try {
ServerSocket server=new ServerSocket(3081);
while(true){
socket=server.accept();
System.out.println("server:"+socket.getRemoteSocketAddress());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pw.close();
br.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
+++++++++++++++++++++++++++++++++++++++++++
Cleint Socket:
`enter code here`
+++++++++++++++++++++++++++++++++++++++++++
package com.jdbc.test;
import java.net.*;
import java.io.*;
public class MainProgram {
private static String host;
public static void main(String[] args)
{
host = "localhost";
//for (int i = 10000; i < 10001 ; i++)
{
try {
Socket socket = new Socket(host , 3081);
}catch (UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
+++++++++++++++++++++++++++++++++++++++++++
答案 2 :(得分:0)
如果你有任何想法,请不要开始阅读Socket Tutorial。
我建议您编写自己的服务器套接字来监听客户端代码的连接(类似于您编写的代码)。 使用高于5000的高端口号码以避免保留端口和计算机程序可能使用的其他端口。