server端的import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
ServerSocket ss = null;
Socket s =null;
DataInputStream c = null;
String str = null;
public void Lunch(){
try {
ss = new ServerSocket(5656);
} catch (IOException e1) {
e1.printStackTrace();
}
while (true){
try {
this.s = ss.accept(); //这里有SocketException异常
System.out.println("新的客户端已经连接");
Client a = new Client(s);
new Thread(a).start();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class Client implements Runnable{
private Socket s;
Client(Socket s){
this.s = s;
try {
c = new DataInputStream(s.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while(true){
str = c.readUTF();
System.out.println(str);
}
}
catch (IOException e) {
System.out.println("已断开连接");
}
finally{
try {
if(c != null) c.close();
if(s !=null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
public static void main(String[] args) {
new Server().Lunch();}}

解决方案 »

  1.   

    这是client端的
    import java.awt.Frame;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.net.Socket;
    import java.io.*;public class Client {
    DataOutputStream c = null;
    TextField t1 = new TextField();
    TextArea t2 = new TextArea();
    Frame a = new Frame();
    String str = null;
    Socket s = null;
    TextLister tt = new TextLister();
    public void add(){
    a.add(t1 , "South");
    a.add(t2 , "North");
    a.pack();
    }
    public void close(){
    a.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    try {
    s.close();
    c.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    System.exit(0);
    }
    });
    }
    public void addText(){
    t1.addActionListener(tt);
    }
    public void Lunch(){
    this.add();
    this.close();
    this.addText();
    a.setVisible(true);
    }
    class TextLister implements ActionListener{public void actionPerformed(ActionEvent e) {
    try {
    s = new Socket("127.0.0.1" , 5656);
    c = new DataOutputStream(s.getOutputStream());
    } catch (IOException e1) {
    System.out.println("找不到服务器");
    }
    str = t1.getText();
    t2.setText(str);
    try {
    c.writeUTF(str);
    c.flush();
    } catch (IOException e1) {
    System.out.println("服务器无法接受");
    }
    t1.setText("");
    }
    }
    public static void main(String[] args) {
    new Client().Lunch();
    }
    }
      

  2.   


    这是错误的提示
    java.net.SocketException: Socket is closed
    at java.net.ServerSocket.accept(ServerSocket.java:486)
    at Server.Lunch(Server.java:20)
    at Server.main(Server.java:68)
      

  3.   

    改了改楼主参考一下:import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class Server
    {
    ServerSocket ss = null;
    Socket s =null;
    //DataInputStream c = null;
    //String str = null;
    public void Lunch()
    {
    try 
    {
    ss = new ServerSocket(5656);
    System.out.println("serversocket");
    //循环放在这里.
    while (true)
    {
    try
    {
    this.s = ss.accept(); //这里有SocketException异常
    System.out.println("新的客户端已经连接");
    Client a = new Client(s);
    new Thread(a).start();
    }
            catch (IOException e) 
    {
    e.printStackTrace();
    }
    }
    }
            catch (IOException e1) 
    {
    e1.printStackTrace();
    } finally
    {
    try
            {
    ss.close();
    }
            catch (IOException e)
            {
    e.printStackTrace();
    }
    }
    }

    class Client implements Runnable
    {
    DataInputStream c = null; //为每个线程独立分配.
    String str = null;
    private Socket s;
    Client(Socket s)
    {
    this.s = s;
    try 
    {
    c = new DataInputStream(s.getInputStream());
    }
            catch (IOException e) 
    {
    e.printStackTrace();
    }
    }
    public void run() 
    {
    try 
    {
    while(true)
    {
    str = c.readUTF();
    System.out.println(str);
    }
    }
    catch (IOException e) 
    {
    System.out.println("已断开连接");
    }
    finally
    {
    try
            {
    if(c != null)
    {
            c.close();
    }
    if(s !=null)
    {
    s.close();
    }
    }
            catch (IOException e)
            {
    e.printStackTrace();
    }
    }
    }
    } public static void main(String[] args) 
    {
    new Server().Lunch();
    }
    }import java.awt.Frame;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.net.Socket;
    import java.io.*;public class Client 
    {
    DataOutputStream c = null;
    TextField t1 = new TextField();
    TextArea t2 = new TextArea();
    Frame a = new Frame();
    String str = null;
    Socket s = null;
    TextLister tt = new TextLister();
    public void add()
    {
    //连接部分放这。
    try 
    {
    s = new Socket("127.0.0.1" , 5656);
    c = new DataOutputStream(s.getOutputStream());
    }
    catch (IOException e1) 
    {
    System.out.println("找不到服务器");
    }
    a.add(t1 , "South");
    a.add(t2 , "North");
    a.pack();
    }
    public void close()
    {
    a.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    try
            {
    s.close();
    c.close();
    }
            catch (IOException e1) 
    {
    e1.printStackTrace();
    }
    System.exit(0);
    }
    });
    }
    public void addText()
    {
    t1.addActionListener(tt);
    }
    public void Lunch()
    {
    this.add();
    this.addText();
    this.close();
    a.setVisible(true);
    }
    class TextLister implements ActionListener
    {
    public void actionPerformed(ActionEvent e) 
    {
    str = t1.getText();
    t2.setText(str);
    try
            {
    c.writeUTF(str);
    c.flush();
    }
            catch (IOException e1) 
    {
    System.out.println("服务器无法接受");
    }
    t1.setText("");
    }
    }
    public static void main(String[] args) 
    {
    new Client().Lunch();
    }
      

  4.   

    谢谢你
    但我还有个问题
    client端假如还按照我原来的写法  同时按下图操作为什么运行结果就会和你给我的客户端以及服务端运行结果一样呢?
      

  5.   

    上面的图打错了 应该是把黑框里的成员变量移到lunch下成为局部变量