当前位置: 代码迷 >> Java相关 >> 关于socket的问题
  详细解决方案

关于socket的问题

热度:282   发布时间:2012-02-05 20:06:03.0
关于socket的问题
package aa;


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

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();
        }
    }
   
}

异常信息

java.net.BindException: Address already in use: JVM_Bind
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(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  

----------------解决方案--------------------------------------------------------
Address already in use: JVM_Bind,多看看异常信息嘛,这句话说JVM已经使用了这个地址,serversocket1=new ServerSocket(1888);把参数改为18888试试
----------------解决方案--------------------------------------------------------
  相关解决方案