发生异常java.net.ConnectException: Connection refused: connect
程序代码:
服务器端:import java.net.*;
import java.util.*;
import java.io.*;
public class AppServer {
ServerSocket server;
Socket fromClient;
Thread serverThread;
public AppServer(){
System.out.print("聊天服务器启动........");
try{
server=new ServerSocket(1001);
serverThread=new Thread();
serverThread.start();
}catch(Exception e){
System.out.println("不能启动该线程:"+e);
}
}
public static void main(String[] args){
new AppServer();
}
............
........省略
............
}
客户端:public class login impelments ActionListener{
...........
...........
.......创建登录界面
.............
下面是实现事件监听的函数
public void actionPerformed(ActionEvent e1){
JButton button=(JButton)e1.getSource();
if(button.equals(btnCancel)){
this.dispose();
}
else if(button.equals(btnRegister)){
new Register();
this.dispose();
}else{
try{
toServer=new Socket("我的机器名",1001);
streamFromServer=new ObjectInputStream(toServer.getInputStream());
streamToServer=new PrintStream(toServer.getOutputStream());
streamToServer.println("登录信息");
userName=txtUserName.getText();
userPwd=txtUserPwd.getPassword();
strPwd=new String(userPwd);
streamToServer.println(userName+":"+strPwd);
String frmServer=(String)streamFromServer.readObject();
if(frmServer.equals("欢迎")){
new clientInt(userName);
this.dispose();
}else {
showdlg();
}
}catch(Exception e){
System.out.println("发生异常"+e);
}
}
}
public static void main(String[] args){
new Login();
}
}
程序出现异常,发生异常java.net.ConnectException: Connection refused: connect。是1001端口的原因呢,还是因为我没有在eclipse中配置什么。我的防火墙没开着。
服务器端:import java.net.*;
import java.util.*;
import java.io.*;
public class AppServer {
ServerSocket server;
Socket fromClient;
Thread serverThread;
public AppServer(){
System.out.print("聊天服务器启动........");
try{
server=new ServerSocket(1001);
serverThread=new Thread();
serverThread.start();
}catch(Exception e){
System.out.println("不能启动该线程:"+e);
}
}
public static void main(String[] args){
new AppServer();
}
............
........省略
............
}
客户端:public class login impelments ActionListener{
...........
...........
.......创建登录界面
.............
下面是实现事件监听的函数
public void actionPerformed(ActionEvent e1){
JButton button=(JButton)e1.getSource();
if(button.equals(btnCancel)){
this.dispose();
}
else if(button.equals(btnRegister)){
new Register();
this.dispose();
}else{
try{
toServer=new Socket("我的机器名",1001);
streamFromServer=new ObjectInputStream(toServer.getInputStream());
streamToServer=new PrintStream(toServer.getOutputStream());
streamToServer.println("登录信息");
userName=txtUserName.getText();
userPwd=txtUserPwd.getPassword();
strPwd=new String(userPwd);
streamToServer.println(userName+":"+strPwd);
String frmServer=(String)streamFromServer.readObject();
if(frmServer.equals("欢迎")){
new clientInt(userName);
this.dispose();
}else {
showdlg();
}
}catch(Exception e){
System.out.println("发生异常"+e);
}
}
}
public static void main(String[] args){
new Login();
}
}
程序出现异常,发生异常java.net.ConnectException: Connection refused: connect。是1001端口的原因呢,还是因为我没有在eclipse中配置什么。我的防火墙没开着。
...全文
相关推荐
Socket长连接异常处理
socket连接时的异常解释及其产生原因 doc文档
Python93道经典题目(及解析)
Python93道经典题目(及解析)题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…
javaerror处理资料
常用的java处理 网络JAVA的基本知识 和报错的处理方法
JAVA常见异常BUG
JAVA常见异常,JAVA异常,bugJAVA常见异常,JAVA异常,bug
Java连接拒绝问题分析[可运行源码]
文章分析了Java中常见的`java.net.ConnectException: Connection refused: connect`错误,指出该错误通常发生在客户端尝试连接服务器时被拒绝。主要原因包括目标服务器未启动、网络连接问题、端口未开放或被防火墙阻止,以及URL配置错误。作者特别提到,从代码上下文来看,问题可能出现在`conn.connect()`或后续的网络交互中,并分享了自己遇到该问题的具体原因——需要连接的服务器端未启动。


Eclipse
