日期:2014-05-20 浏览次数:20984 次
package com.nothing.Lab; import java.io.DataInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; public class S { public ServerSocket serverSocket = null; public ObjectInputStream ois = null; public ObjectOutputStream oos = null; public boolean started = false, bConnect = false; public static void main(String[] args){ new S().go(); } public void go(){ try { serverSocket = new ServerSocket(8888); started = true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ while(started){ Socket s = serverSocket.accept(); System.out.println("a clients in"); Clients c = new Clients(s); bConnect = true; new Thread(c).start(); } }catch(IOException e){ e.printStackTrace(); } } public class Clients implements Runnable{ Socket s; String str = ""; public Clients(Socket s){ this.s = s; } @Override public void run() { // TODO Auto-generated method stub try{ while(bConnect){ ois = new ObjectInputStream(s.getInputStream()); //第二次接收时抛异常 str = ois.readObject().toString(); System.out.println(str); } }catch(ClassNotFoundException e1){ e1.printStackTrace(); }catch(IOException e){ System.out.println("quit"); e.printStackTrace(); }finally{ try { if(ois != null) ois.close(); if(s != null) s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
package com.nothing.Lab;
import java.awt.BorderLayout;
public class C extends JFrame {
public Socket socket = null;
public ObjectInputStream ois = null;
public ObjectOutputStream oos = null;
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
C frame = new C();
frame.go();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public C() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 308, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
textField = new JTextField();
contentPane.add(textField, BorderLayout.SOUTH);
textField.setColumns(10);
JTextArea textArea = new JTextArea();
contentPane.add(textArea, BorderLayout.CENTER);
textField.addActionListener(new EnterDown());
}
public void go(){
try{
socket = new Socket("127.0.0.1", 8888);
oos = new ObjectOutputStream(socket.getOutputStream());
System.out.println("connection");
}catch(IOException e){
e.printStackTrace();
}
}
public class EnterDown implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
String str = textField.getText();
System.out.println("c:"+str);
oos.writeObject(new String("a"));
// oos.flush();
// oos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public class recv implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
}
}
}