我写的服务端接受时出现异常
     java.net.SocketException: Connection reset用java做客户端发送信息,不会出现异常,而接受用c++发送的信息的时候出现以上异常谁能告诉我什么原因啊?

解决方案 »

  1.   

    程序如下:
    import java.net.*;
    //import java.util.*;
    import java.io.*;public class Server
    {
        public Server()
        {
            
        }
        
        public static void main(String[] args) throws IOException
        {
            System.out.println("------365Maga services --------\n");
            System.out.println("Server Starting........");
            ServerSocket server = new ServerSocket(10000);
            while (true)
            {
                Socket s = server.accept();
                System.out.println("Accepting Connection.......");
                System.out.println("A new Thread opened.");
                PrintWriter pw = null;
                pw = new PrintWriter(s.getOutputStream(),true);
                pw.println("sssssssssssssssssfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
                new ServerThread(s).start();
            }
        }      
    }class ServerThread extends Thread
    {
        private Socket s;
        public ServerThread(Socket server)
        {
            this.s = server;
        }
        public void run()
        {
            BufferedReader br = null;
            PrintWriter pw = null;
            try
            {
                InputStreamReader isr;
                isr  = new InputStreamReader(s.getInputStream());
                br = new BufferedReader(isr);
                String m = br.readLine();
                System.out.println("Receive message:  "+m);
                //String subM = m.substring(0,5);
                //if ("Login".equals(subM))
                pw = new PrintWriter(s.getOutputStream(),true);
                pw.println("Your message is:"+ m);
                //System.out.println(m.split(":"));
                System.out.println(m.length());
                
                if(m.startsWith("Login"))
                {
                    System.out.println("one member login.");
                }
                
            }
            catch(IOException e)
            {
                System.out.println("Server error:"+ e.toString());
            }
            finally
            {
                System.out.println("Connection shuting .......");
                try
                {
                    if(br!=null)
                    {
                        br.close();
                    }
                    if(pw!=null)
                    {
                        pw.close();
                    }
                    if(s!=null)
                    {
                        s.close();
                    }
                }
                catch(IOException e)
                {
                    System.out.println("Shuting error:"+e.getMessage());
                }
                //System.out.println("Services Shuted...\n");
                System.out.println("The Thread closed.");
            }
        }
    }
      

  2.   

    package socketclient;import java.io.*;
    import javax.swing.*;
    import java.util.Calendar;/**
     * <p>Title: Socketclient</p>
     * <p>Description: Socket communication client</p>
     * <p>Copyright: JGD 2002.11.08</p>
     * <p>Company: JGD</p>
     * @author Jiabinbin(JGD's part two team two)
     * @version 1.0
     */
    public class SocketClient
    {
        /**
         * The programme examines the Socket.ini file Firstly.<br><br>
         * If the file existed programme creates and showes main dialog.<br>
         * If the file didn't existed the system will exit the programme.
         */
        public static void main(String[] args)
        {
            final String con_startUpFile = "Socket.ini";
            final String con_logFile = "socketClient.trc";
            final String con_errorDialogTitle = "Error";
            final String con_openFileError = "Open Socket.ini error!";
            final String con_changeStyleError = "Change the Style error";
            File startUpFile = new File(con_startUpFile);        if (!startUpFile.exists())
            {
                JOptionPane.showMessageDialog( null , con_openFileError ,
                        con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }        try
            {
                javax.swing.UIManager.setLookAndFeel(
                        javax.swing.UIManager.getSystemLookAndFeelClassName());
            }
            catch (Exception ex)
            {
                JOptionPane.showMessageDialog( null , con_changeStyleError ,
                        con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }        MainDialog mainDialog = new MainDialog();
            mainDialog.show();        try
            {
                //write log file.
                BufferedWriter writeLog =
                        new BufferedWriter(new FileWriter(con_logFile , true));
                writeLog.write("***********************************");
                writeLog.newLine();
                writeLog.write("[" + Calendar.getInstance().getTime() + "]" +
                               "Socket.ini is exist,programme run!");
                writeLog.newLine();
                writeLog.write("-----------------------------------");
                writeLog.newLine();
                writeLog.close();
            }
            catch (IOException ex)
            {
                mainDialog.setLogError();
            }
        }
    }我以前写的和C++通讯的socket,你参考一下!
      

  3.   

    我晕,贴错代码了,从来
        public void sendMessage()
        {
            try
            {
                out = new PrintWriter(socketClient.getOutputStream(), true);
                message = "";
                message = message + con_sendMessageTitle1 + sendMessageTitle2;
                message = message + comboBox.getSelectedItem().toString();
                message = message + con_resultTitle1 + textField1.getText() + con_resultTail1;
                message = message + con_resultTitle2 + textField2.getText() + con_resultTail2;
                message = message + "</" + comboBox.getSelectedItem().toString();
                message = message + ">\n" + con_sendMessageTail1 + con_sendMessageTail2;
                out.print(message);
                out.flush();
                log("Sent message is : " + message , 3);
            }
            catch (IOException e)
            {
                log(con_sentMessageError , 2);
                JOptionPane.showMessageDialog(this , con_sentMessageError ,
                        con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
                socketClose();
            }
        }    /**
         * This method's function is that accept the stream from server and
         *     transfer inFile() to analyse this stream.<br>
         * Return null if system could not accept the message at definite time.
         */
        public void socketIncept()
        {
            try
            {
                String str = new String();
                message = "";
                in = new BufferedReader(
                        new InputStreamReader(socketClient.getInputStream()));
                while ((str = in.readLine()) != null)
                {
                    message = message + str + "\n";
                    str = str.trim();
                    if (str.endsWith(con_sendMessageTail2))
                    {
                        break;
                    }
                }
                log("Accept message is : " + message , 3);
            }
            catch (IOException e)
            {
                log(con_timeOutError , 1);
                JOptionPane.showMessageDialog(this , con_timeOutError ,
                        con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
                socketClose();
                return;
            }
            inFile();
        }
      

  4.   

    socket.getOutputStream 或者 socket.getInputStream 之后,不要简单地构造成 PrintWriter 和 Reader,应该与 C++ 约定好编码后,使用 InputStreamReader 和 OutputStreamWriter 指定相应的编码。详情请参考文章:
    http://www.regexlab.com/zh/encoding.htm