我写了一个服务器和一个客户端,两个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)
解决方案 »
- linux下安装jdk的问题
- 关于immutable objects在线程应用中的句话的理解????
- 根本不会JAVA的人提出的问题,请赐教
- 求助TUXEDO在AIX系统下的开发
- 请大家推荐一个开源免费前途光明的 java IDE .
- Javadoc使用一问
- EXE
- 我愿提供100分,请高手为我说明一下,为什么在jdk1.4中demo中的stylepad输入中文就会出问题,该如何解决?
- servlet中out.println()输出的东西是不是存到response对象中了
- Scanner in=new Scanner(System.in);运行时错误不知道怎么办?麻烦大家看看代码哪里错了?
- 老师给了个JAVA题目不会做,跪求大侠解救
- 在线等Java登录界面连接数据库问题
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端的
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();
}
}
}}这是客户端
//创建一个服务器并启动服务器
public class ServerMain {
public static void main(String args[]){
ServerAgenda sa=new ServerAgenda();
sa.ServerRun();
}}
启动服务器类
import com.wjb.Frame.*;
import com.wjb.net.ClientAgenda;
public class Agenda {
//启动客户端类
public static void main(String args[]){
ClientAgenda ca=new ClientAgenda();
}
}
最关键的类都没发上来啊。
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();
}
}