| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 617 人关注过本帖
标题:关于socket的问题
只看楼主 加入收藏
kd83kdi93
Rank: 2
等 级:论坛游民
帖 子:65
专家分:52
注 册:2011-9-18
结帖率:70%
收藏
已结贴  问题点数:10 回复次数:1 
关于socket的问题
package aa;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import
import
import
import

import javax.swing.*;

class A1{
    public static void main(String args[]){
        A2 a2=new A2();
        Thread thread1=new Thread(a2);
        thread1.start();
        B1 b1=new B1();
    }
}

class A2 extends JFrame implements Runnable{
    ServerSocket serversocket1=null;
    Socket socket1=null;
    DataInputStream input1=null;
    JTextField text1,text2;
   
    JButton button1;
   
    public A2() {
        this.setSize(400,400);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);
        text2=new JTextField();
        this.add(text2);
        try{
        serversocket1=new ServerSocket(1888);
        socket1=serversocket1.accept();
        input1=new DataInputStream(socket1.getInputStream());
        }catch(Exception e){
            e.printStackTrace();
        }
   
        }

    @Override
    public void run() {
            
        try {
            
            while(true){
                text2.setText(input1.readUTF());
            }
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        
    }
}


class B1 extends JFrame{
   
    JButton button1;
    static JTextField text1;
    Socket socket1;
    static DataOutputStream output1;
   
    public B1(){
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setResizable(false);
        this.setSize(400,400);
        button1=new JButton("发送");
        text1=new JTextField();
        this.add(text1);
        this.add(button1,BorderLayout.SOUTH);
        button1.addActionListener(new Act());
        try {
            socket1=new Socket("127.0.0.1",1888);
            output1=new DataOutputStream(socket1.getOutputStream());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
   
}

class Act implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent arg0) {
        try{
        B1.output1.writeUTF(B1.text1.getText());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
   
}

异常信息

Address already in use: JVM_Bind
    at (Native Method)
    at (Unknown Source)
    at (Unknown Source)
    at (Unknown Source)
    at (Unknown Source)
    at aa.A2.<init>(A1.java:41)
    at aa.A1.main(A1.java:18)
java.lang.NullPointerException
    at aa.A2.run(A1.java:56)
    at java.lang.Thread.run(Unknown Source)


我是想做个聊天的工具   但是出错了不知道怎么回事
搜索更多相关主题的帖子: class public package void import 
2012-02-05 20:06
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:10 
Address already in use: JVM_Bind,多看看异常信息嘛,这句话说JVM已经使用了这个地址,serversocket1=new ServerSocket(1888);把参数改为18888试试

My life is brilliant
2012-02-06 16:04
快速回复:关于socket的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017771 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved