问题描述
packageTest1;importjava.io.*;importjava.net.*;publicclassClientF{Sockets;ObjectOutputStreamoos;publicClientF(){try{s=newSocket("127.0.0.1",9999);oos=newObjectOutputStream(s.getOutputStream());//ThreadServerts=newThreadServer();//ts.start();//s.close();}catch(UnknownHostExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}TServertts=newTServer();tts.start();}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubClientFcf=newClientF();}classThreadServerextendsThread{booleanflag=true;publicvoidrun(){try{ObjectInputStreamois=newObjectInputStream(s.getInputStream());while(flag){System.out.println(ois.readObject().toString());}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(ClassNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}classTServerextendsThread{booleanflag=true;publicvoidrun(){try{MessageFmf=newMessageF("AAA");oos.writeObject(mf);//oos.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}-------------------------------------------------------------------------------------------packageTest1;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;importjavax.swing.*;publicclassServerSocketF{PrintStreamps;booleanflag=true;JFramejf=newJFrame();JButtonjb=newJButton("发送");JTextAreajta1=newJTextArea();JTextAreajta2=newJTextArea();JPaneljp=newJPanel();JScrollPanejspoutput=newJScrollPane(jta1);JScrollPanejspinput=newJScrollPane(jta2);ObjectOutputStreamos;ServerSocketss;Sockets;publicServerSocketF(){jp.setLayout(newGridLayout(2,1,3,3));jp.add(jspoutput);jp.add(jspinput);jf.add(jp,BorderLayout.CENTER);jf.add(jb,BorderLayout.SOUTH);jf.setSize(200,200);jf.setVisible(true);jb.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//mf.setMsg(jta2.getText());MessageFmf=newMessageF(jta2.getText());try{os.writeObject(mf);}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}jta2.setText("");}});try{ss=newServerSocket(9999);while(flag){s=ss.accept();os=newObjectOutputStream(s.getOutputStream());ThreadServerts=newThreadServer();ts.start();}//s.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicstaticvoidmain(String[]args){ServerSocketFsf=newServerSocketF();}classThreadServerextendsThread{booleanflag=true;publicvoidrun(){try{ObjectInputStreamois=newObjectInputStream(s.getInputStream());while(flag){System.out.println(ois.readObject().toString()+"n");}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(ClassNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}------------------------------------------------------------------------packageTest1;importjava.io.Serializable;publicclassMessageFimplementsSerializable{Stringmsg;publicMessageF(){}publicMessageF(Stringmsg){this.msg=msg;}publicStringtoString(){return"@:"+msg;}}----------------------------------------------------------------------------------报错为什么@:AAAjava.net.SocketException:Connectionresetatjava.net.SocketInputStream.read(UnknownSource)atjava.net.SocketInputStream.read(UnknownSource)atjava.io.ObjectInputStream$PeekInputStream.peek(UnknownSource)atjava.io.ObjectInputStream$BlockDataInputStream.peek(UnknownSource)atjava.io.ObjectInputStream$BlockDataInputStream.peekByte(UnknownSource)atjava.io.ObjectInputStream.readObject0(UnknownSource)atjava.io.ObjectInputStream.readObject(UnknownSource)atTest1.ServerSocketF$ThreadServer.run(ServerSocketF.java:96)
解决方案
解决方案二:
9999端口开了吗?你机器是不是有防火墙9999被拦截了吧
解决方案三:
该回复于2010-12-13 13:35:44被版主删除