import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class Server implements ActionListener {

    
public Server(){
        JFrame jf 
= new JFrame("服务器窗口");
        JMenu jm 
= new JMenu("Admin");
        JMenuItem jmi 
= new JMenuItem("Start");
        jmi.addActionListener(
this);
        JMenuBar jmb 
= new JMenuBar();
        jm.add(jmi);
        jmb.add(jm);
        jf.setJMenuBar(jmb);
        jf.setSize(
200300);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(
true);
    }

    
public static void main(String[] args) {
        
new Server();

    }

    
public void actionPerformed(ActionEvent e) {
        
try {
            ServerSocket ss 
= new ServerSocket(8888);
            Socket so 
= ss.accept();
            
new ServerThread(so).start();
        }
 catch (Exception e1) {
            e1.printStackTrace();
        }

        
    }


}

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


public class ServerThread extends Thread{
    
private Socket so;
    
private String word;
    
public ServerThread(Socket so){
        
this.so = so;
    }

    
public void run(){
        
try {
            DataInputStream dis 
= new DataInputStream(so.getInputStream());
            DataOutputStream dos 
= new DataOutputStream(so.getOutputStream());
            
while(true){
                String temp 
= dis.readUTF();
                
if("bye".equals(temp)) break;
                word 
= "Server:"+temp;
                dos.writeUTF(word);
            }

            dos.close();dis.close();so.close();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

    }

}

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Client implements ActionListener{
    JFrame jf 
= new JFrame("客户端窗口");
    JButton jb1 
= new JButton("Send");
    JTextField jtf 
= new JTextField(15);
    JTextArea jta 
= new JTextArea();
    
public Client(){    
        JPanel jp1 
= new JPanel();
        jp1.add(jtf);jp1.add(jb1);
        jb1.addActionListener(
this);
        jf.setLayout(
new BorderLayout());
        jf.add(jp1,
new BorderLayout().NORTH);
        jf.add(jta);
        jf.setSize(
300400);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(
true);
    }

    
public static void main(String args[]){
        
new Client();
    }

    
public void actionPerformed(ActionEvent e) {
        
if(e.getSource()==jtf || e.getSource()==jb1){
            
try {
                Socket so 
= new Socket("127.0.0.1",8888);
                DataInputStream dis 
= new DataInputStream(so.getInputStream());
                DataOutputStream dos 
= new DataOutputStream(so.getOutputStream());
                
while(true){
                    String temp 
= jtf.getText();
                    dos.writeUTF(temp);
                    
if("bye".equals(temp)) break;
                    String str 
= dis.readUTF();
                }

                dos.close();dis.close();so.close();
            }
catch (IOException e1) {
                e1.printStackTrace();
            }

            
        }

        
    }

}

运行时,老是报错误:
java.net.SocketException: Connection reset
 at java.net.SocketInputStream.read(Unknown Source)
 at java.net.SocketInputStream.read(Unknown Source)
 at java.io.DataInputStream.readUnsignedShort(Unknown Source)
 at java.io.DataInputStream.readUTF(Unknown Source)
 at java.io.DataInputStream.readUTF(Unknown Source)
 at ServerThread.run(ServerThread.java:18)
--------------------------------------------------------------------------------------------------
有人说,我的客户端里要再写一个线程,因为main线程主要是用于维护图形界面,要另写个线程来发送数据到客户端
是这样吗?