JAVA connect被拒绝

huasheng724 2010-05-28 11:15:10
PC类:
package example;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PC extends JFrame
implements Runnable,ActionListener{

private static final long serialVersionUID = -3060057565518151951L;
JButton connection,computer;
JTextField inputA;
JTextArea showResult;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
public PC(){
socket=new Socket(); //待连接的套接字
connection=new JButton("连接服务器");
computer=new JButton("求平方根");
computer.setEnabled(false); //没有和服务器连接之前,该按钮不可用
inputA=new JTextField("0",12);

Box boxV1=Box.createVerticalBox();
boxV1.add(new JLabel("输入一个整数"));

Box boxV2=Box.createVerticalBox();
boxV2.add(inputA);

Box baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(boxV2);
Container con=getContentPane();
con.setLayout(new FlowLayout());
showResult=new JTextArea(8,18);
con.add(connection);
con.add(baseBox);
con.add(computer);
con.add(new JScrollPane(showResult));
computer.addActionListener(this);
connection.addActionListener(this);
thread = new Thread(this);
setBounds(100,100,360,310);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run(){
while(true){
try{ double root=in.readDouble(); //堵塞状态,除非读取到信息

showResult.append("\n平方根:"+root);
showResult.setCaretPosition((showResult.getText()).length());
}
catch(IOException e){
showResult.setText("与服务器已断开");
computer.setEnabled(false);
break;
}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==connection){
try{ if(socket.isConnected()) {}
else{
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
computer.setEnabled(true);
thread.start();
}
}
catch (IOException ee){
System.out.println(ee);
socket=new Socket();
}
}
if(e.getSource()==computer){
try{ double a=Double.parseDouble(inputA.getText());


if(a>=0){
out.writeDouble(a);

}
else{
inputA.setText("负数无平方根!");
}
}
catch(Exception ee){
inputA.setText("请输入数字字符");
}
}
}
public static void main(String args[]){
PC win=new PC();
}
}

Fuwu类:
package example;


import java.io.*;
import java.net.*;

public class Fuwu{
public static void main(String args[]){
ServerSocket server=null;

Socket you=null;
while(true){
try{ server=new ServerSocket(4331);
}
catch(IOException e1){
System.out.println("正在监听"); //ServerSocket对象不能重复创建
}
try{ you=server.accept();
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e){
System.out.println("正在等待客户");
}
if(you!=null)
new ServerThread(you).start(); //为每个客户启动一个专门的线程
else
continue;
}
}
}
class ServerThread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s=null;
ServerThread(Socket t){
socket=t;
try{ in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
}
public void run(){
while(true){
double a=0;
try{ a=in.readDouble(); //堵塞状态,除非读取到信息

double root=Math.sqrt(a);

out.writeDouble(root);

}
catch (IOException e){
System.out.println("客户离开");
break;
}
}
}
}


connect被拒绝,请问是代码中哪里错了?
...全文
150 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
niejimaoo 2010-05-30
  • 打赏
  • 举报
回复
端口被占用?
wenzheng38 2010-05-30
  • 打赏
  • 举报
回复
有可能是网络的不稳定
设置下尝试链接的次数跟时延。。
kangxiaolin123456 2010-05-28
  • 打赏
  • 举报
回复
服务器没有开启。或者是防火墙开着
zihuzihuzihu 2010-05-28
  • 打赏
  • 举报
回复

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧