我写了一个服务器和一个客户端,两个xml文件,D盘的xml文件保存客户端的数据,E盘的xml文件保存服务器的数据。
服务器是一开始就启动的,从服务器的xml文件读数据写到客户端的xml。  s.close()
客户端有一个窗口,窗口里面有一个按钮添加,当按下的时候,就把我的TEXTFILED里面的数据写进客户端的xml文件 就从客户端的xml文件写回服务器端,但是出现java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.write(Unknown Source)
at com.wjb.net.ServerAgenda.NomegetData(ServerAgenda.java:41)
at com.wjb.net.ServerAgenda.ServerRun(ServerAgenda.java:23)
at com.wjb.Main.ServerMain.main(ServerMain.java:9)
 

解决方案 »

  1.   

    package com.wjb.net;
    import java.io.*;
    import java.net.*;
    public class ServerAgenda {

     DataOutputStream dos;
     DataInputStream dis;
     Socket s;
     OutputStream out;
     InputStream in;
    //服务器启动
    public void ServerRun(){
    try {

    ServerSocket ss=new ServerSocket(8888);
    while(true){
    s=ss.accept();
    System.out.println("A client is getin ");



     
     this.NomegetData();

    }
    } catch (IOException e) {

    e.printStackTrace();
    } }//把客户名字从服务器写进客户端
    public  void NomegetData(){
     try{
     out=s.getOutputStream();
     dos=new DataOutputStream(out);
     File file=new File("e:/Agenda/NomeWrite.xml");
    int b=0;
        FileInputStream fr=new FileInputStream(file);
        while((b=fr.read())!=-1)
        {
         dos.write(b);
        }
    fr.close();
        dos.flush();
    dos.close();

    s.close();

     } catch(IOException e) {

    e.printStackTrace();
    }
     
    }
     //  把客户资料写进客户端
    public  void getData(){
     try{
     out=s.getOutputStream();
     dos=new DataOutputStream(out);
     File file=new File("e:/Agenda/Agenda.xml");
    int b=0;
        FileInputStream fr=new FileInputStream(file);
        while((b=fr.read())!=-1)
        {
         dos.write(b);
        }
    fr.close();
        dos.flush();
    dos.close();

    s.close();

     } catch(IOException e) {

    e.printStackTrace();
    }
     
    }
    //把客户资料从客户端读到服务器端
     public void setData(){
       try{
       in=s.getInputStream();
     dis=new DataInputStream(in);
       int b=0;
       File file=new File("e:\\Agenda\\Agenda.xml");
       
       FileOutputStream fos=new FileOutputStream(file);
       while((b=dis.read())!=-1){
    fos.write(b);
    }
       
    dis.close();
    s.close();

       }
       catch(IOException e1){
    e1.printStackTrace();   
       }
     }
       //把客户名字从客户端读到服务器端
    public void NomesetData() {
     try{
       in=s.getInputStream();
     dis=new DataInputStream(in);
       int b=0;
       File file=new File("e:\\Agenda\\NomeWrite.xml");
       
       FileOutputStream fos=new FileOutputStream(file);
       while((b=dis.read())!=-1){
    fos.write(b);
    }
       
    dis.close();
    s.close();

       }
       catch(IOException e1){
    e1.printStackTrace();   
       }

    }
     }
    这个是server端的
      

  2.   

    package com.wjb.net;import java.net.Socket;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import javax.swing.JFrame;import javax.swing.JOptionPane;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.JButton;import com.wjb.Frame.MainFrame;
    public class ClientAgenda extends JFrame implements ActionListener {
    private JTextField firstfield;
    private JTextField thirdfield;
    private JTextField secondfield;
    Socket s;
    int port=8888;
    String firstcode;
    String secondcode;
    String thirdcode;
    String ipAddress;
    String forthcode;
    private JTextField forthtextField;
       //客户端窗口    
    public  ClientAgenda(){
    this.setBounds(100, 30, 750, 700);
    this.setResizable(false);
    firstfield = new JTextField();
    firstfield.setColumns(10);

    JLabel lblNewLabel = new JLabel(".");

    thirdfield = new JTextField();
    thirdfield.setColumns(10);

    secondfield = new JTextField();
    secondfield.setColumns(10);

    JLabel lblNewLabel_1 = new JLabel(".");

    JLabel lblNewLabel_2 = new JLabel("SERVER IP");

    JButton btnNewButton = new JButton("Connect");//连接按钮
    btnNewButton.addActionListener(this);//连接按钮监听,

    forthtextField = new JTextField();
    forthtextField.setColumns(10);
    GroupLayout groupLayout = new GroupLayout(getContentPane());
    groupLayout.setHorizontalGroup(
    groupLayout.createParallelGroup(Alignment.TRAILING)
    .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
    .addGap(155)
    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
    .addComponent(btnNewButton)
    .addGroup(groupLayout.createSequentialGroup()
    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
    .addComponent(lblNewLabel_2)
    .addGroup(groupLayout.createSequentialGroup()
    .addComponent(firstfield, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(ComponentPlacement.RELATED)
    .addComponent(lblNewLabel_1)
    .addPreferredGap(ComponentPlacement.RELATED)
    .addComponent(secondfield, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(ComponentPlacement.RELATED)
    .addComponent(lblNewLabel)))
    .addPreferredGap(ComponentPlacement.RELATED)
    .addComponent(thirdfield, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)))
    .addPreferredGap(ComponentPlacement.RELATED)
    .addComponent(forthtextField, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
    .addContainerGap(405, Short.MAX_VALUE))
    );
    groupLayout.setVerticalGroup(
    groupLayout.createParallelGroup(Alignment.LEADING)
    .addGroup(groupLayout.createSequentialGroup()
    .addGap(94)
    .addComponent(lblNewLabel_2)
    .addPreferredGap(ComponentPlacement.UNRELATED)
    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
    .addComponent(lblNewLabel)
    .addComponent(thirdfield, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(secondfield, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(lblNewLabel_1)
    .addComponent(firstfield, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    .addComponent(forthtextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    .addPreferredGap(ComponentPlacement.RELATED)
    .addComponent(btnNewButton)
    .addContainerGap(499, Short.MAX_VALUE))
    );
    getContentPane().setLayout(groupLayout);


    this.setVisible(true);


    }
    //获取客户输入的要连接的服务器端IP
    private String getIp(){
     this.firstcode=firstfield.getText();
    System.out.println(firstcode);  
     this.secondcode=secondfield.getText();
     this.thirdcode=thirdfield.getText();
     this.forthcode=forthtextField.getText();
        ipAddress=new String(firstcode+"."+secondcode+"."+thirdcode+"."+this.forthcode);
    System.out.println(ipAddress);
        return ipAddress;
    }//从客户端读取数据到服务器
    public void ServerConnect() {


    int i=0;

       while(i==0){
    try{
    s=new Socket(ipAddress,port);
    i=1;
    InputStream is=s.getInputStream();

    DataInputStream dis=new DataInputStream(is);

    File file=new File("d:/Agenda/Agenda.xml");
    FileOutputStream fos=new FileOutputStream(file);
    int b=0;
    while((b=dis.read())!=-1){
    fos.write(b);
    }
    dis.close();
    s.close();
    }
    catch(IOException e){
    JOptionPane.showConfirmDialog(null, "服务器未开启","标题",JOptionPane.DEFAULT_OPTION);
    this.ServerConnect();
    }


       }
    }
    //从服务器端读取客户名字到客户端
    public void NomeServerConnect() {

    int i=0;

       while(i==0){
    try{
    s=new Socket(ipAddress,port);
    i=1;
    InputStream is=s.getInputStream();

    DataInputStream dis=new DataInputStream(is);

    File file=new File("d:/Agenda/NomeWrite.xml");
    FileOutputStream fos=new FileOutputStream(file);
    int b=0;
    while((b=dis.read())!=-1){
    fos.write(b);
    }
    dis.close();
    s.close();
    }
    catch(IOException e){
    JOptionPane.showConfirmDialog(null, "服务器未开启","标题",JOptionPane.DEFAULT_OPTION);
    this.NomeServerConnect();
    }


       }
    }
    //从服务器端读取资料到客户端
    public void dataSavatoServer(){


    int i=0;

       while(i==0){
    try{
    s=new Socket(ipAddress,port);
    i=1;
    OutputStream out=s.getOutputStream();

    DataOutputStream dos=new DataOutputStream(out);

    File file=new File("d:/Agenda/Agenda.xml");
    FileInputStream fis=new FileInputStream(file);
    int b=0;
    while((b=fis.read())!=-1){
    dos.write(b);
    }
    dos.close();
    s.close();
    }
    catch(IOException e){
    JOptionPane.showConfirmDialog(null, "服务器未开启","标题",JOptionPane.DEFAULT_OPTION);
    this.dataSavatoServer();
    }


       }

    }//connect按钮监听
    public void actionPerformed(ActionEvent arg0) {
    this.getIp();
    this.ServerConnect();
    this.NomeServerConnect();
    MainFrame mf=new MainFrame();
    this.setVisible(false);
    mf.setVisible(true);

    }
    //从客户端读取客户名字去服务器端
    public void NomeSavatoServer() {


    int i=0;

       while(i==0){
    try{
    s=new Socket(ipAddress,port);
    i=1;
    OutputStream out=s.getOutputStream();

    DataOutputStream dos=new DataOutputStream(out);

    File file=new File("d:/Agenda/NomeWrite.xml");
    FileInputStream fis=new FileInputStream(file);
    int b=0;
    while((b=fis.read())!=-1){
    dos.write(b);
    }
    dos.close();
    s.close();
    }
    catch(IOException e){
    JOptionPane.showConfirmDialog(null, "服务器未开启","标题",JOptionPane.DEFAULT_OPTION);
    this.NomeSavatoServer();
    }
    }
    }}这是客户端
      

  3.   

    package com.wjb.Main;import com.wjb.net.ServerAgenda;
    //创建一个服务器并启动服务器
    public class ServerMain {
    public static void main(String args[]){

    ServerAgenda sa=new ServerAgenda();
    sa.ServerRun();
    }}
    启动服务器类
      

  4.   

    看这个异常像是两边同时在往一个socket里面写东西?
      

  5.   

    但是我已经用s.close()把第一次连接的socket关掉了
      

  6.   

    但我点击添加按钮的时候我又做了new了一个新的客户端类,只调用了其中的一个方法
      

  7.   

    package com.wjb.Main;
    import com.wjb.Frame.*;
    import com.wjb.net.ClientAgenda;
    public class Agenda {
    //启动客户端类
    public static void main(String args[]){
    ClientAgenda ca=new ClientAgenda();


    }


    }
      

  8.   

    at com.wjb.net.ServerAgenda.NomegetData(ServerAgenda.java:41)
    最关键的类都没发上来啊。
      

  9.   

    我在2楼的时候已经发了,那个是一个方法,在serverAgenda类里面 }//把客户名字从服务器写进客户端
        public  void NomegetData(){
             try{
                 out=s.getOutputStream();
                 dos=new DataOutputStream(out);
             File file=new File("e:/Agenda/NomeWrite.xml");
                int b=0;
                FileInputStream fr=new FileInputStream(file);
                while((b=fr.read())!=-1)
                {
                    dos.write(b);
                }
                fr.close();
                dos.flush();
                dos.close();
                 
                s.close();
                 
             } catch(IOException e) {
                 
                e.printStackTrace();
            }
          
        }