麻烦大家帮我看看这个socket通信的程序有什么问题好么?我在main函数里调用while循环就可以通信,可如果把服务器段的while循环放到线程里 就会出错 代码:
服务器段:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;public class test implements  Runnable
{
ServerSocket server = null;
Socket kehu = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread jieshou;
public static void main(String[] args) {
test te=new test();
te.danteng();
} public void danteng(){
jieshou=new Thread();
jieshou.setName("jieshou");
try {

server = new ServerSocket(4000);
kehu = server.accept();
in = new DataInputStream(kehu.getInputStream());
out = new DataOutputStream(kehu.getOutputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("链接到客户端");

jieshou.start();
}

@Override
public void run() {
if(Thread.currentThread().getName().equals("jieshou"))
{while (true) { try {
System.out.println("执行到jieshou" );
int strOutput=in.readInt();
System.out.println("接受到客户端请求" );
System.out.println(strOutput);
out.writeInt(strOutput);
} catch (IOException e1) { e1.printStackTrace();
}
}
}

}

}客户端:
import java.awt.Container;
import java.awt.FlowLayout;
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.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.JButton;
import javax.swing.JFrame;public class testcli extends JFrame implements  ActionListener{
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
JButton connection;
InetAddress address = null;
Thread jieshou;
public static void main(String[] args) {
testcli test=new testcli();
test.danteng();
}
public void danteng()
{
   connection=new JButton("开始运行");
        Container con=getContentPane();
        con.setLayout(new FlowLayout());
        con.add(connection);
        connection.addActionListener(this);
jieshou=new Thread();
jieshou.setName("jieshou");
    setBounds(100,100,360,310);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} InetSocketAddress socketAddress = new InetSocketAddress(address, 4000);
try {
socket = new Socket();
socket.connect(socketAddress);
} catch (IOException e) { e.printStackTrace();
}
try {
in = new DataInputStream(socket.getInputStream());
} catch (IOException e) { e.printStackTrace();
}
try {
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) { e.printStackTrace();
}

}

@Override
public void actionPerformed(ActionEvent e) { if(e.getSource()==connection){

try { out.writeInt(0);
int strInput=in.readInt();
System.out.println("客户端开始接受数据");
out.writeInt(strInput);
System.out.println(strInput);

} catch (IOException e1) {
e1.printStackTrace();
}
      }
}

}
请大家帮帮我 谢谢了!!

解决方案 »

  1.   

    运行时会提示这个问题java.net.SocketException: Connection reset by peer: socket write error
    提示错误是在客户端代码的
    try { out.writeInt(0);
    那行
      

  2.   

    客户端:package s;
    import java.awt.Container;
    import java.awt.FlowLayout;
    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.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;import javax.swing.JButton;
    import javax.swing.JFrame;public class testcli extends JFrame implements ActionListener
    {
        Socket socket = null;
        
        DataInputStream in = null;
        
        DataOutputStream out = null;
        
        JButton connection;
        
        InetAddress address = null;
        
        Thread jieshou;
        
        public static void main(String[] args)
        {
            testcli test = new testcli();
            test.danteng();
        }
        
        public void danteng()
        {
            connection = new JButton("开始运行");
            Container con = getContentPane();
            con.setLayout(new FlowLayout());
            con.add(connection);
            connection.addActionListener(this);
            jieshou = new Thread();
            jieshou.setName("jieshou");
            setBounds(100, 100, 360, 310);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            try
            {
                address = InetAddress.getLocalHost();
            }
            catch (UnknownHostException e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            InetSocketAddress socketAddress = new InetSocketAddress(address, 4000);
            try
            {
                socket = new Socket();
                socket.connect(socketAddress);
            }
            catch (IOException e)
            {
                
                e.printStackTrace();
            }
            try
            {
                in = new DataInputStream(socket.getInputStream());
            }
            catch (IOException e)
            {
                
                e.printStackTrace();
            }
            try
            {
                out = new DataOutputStream(socket.getOutputStream());
            }
            catch (IOException e)
            {
                
                e.printStackTrace();
            }
            
        }
        
        public void actionPerformed(ActionEvent e)
        {
            
            if (e.getSource() == connection)
            {
                
                try
                {
                    out.writeInt(0);
                    out.flush();
                    int strInput = in.readInt();
                    System.out.println("客户端开始接受数据");
                    out.writeInt(strInput);
                    System.out.println(strInput);
                    
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
        }
        
    }
      

  3.   

    服务端:package s;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;public class test implements Runnable
    {
        ServerSocket server = null;
        
        Socket kehu = null;
        
        DataInputStream in = null;
        
        DataOutputStream out = null;
        
        Thread jieshou;
        
        public static void main(String[] args)
        {
            test te = new test();
            te.danteng();
        }
        
        public void danteng()
        {
            try
            {
                
                server = new ServerSocket(4000);
                
                while(true)
                {
                    kehu = server.accept();
                    in = new DataInputStream(kehu.getInputStream());
                    out = new DataOutputStream(kehu.getOutputStream());
                    jieshou = new Thread(this);
                    jieshou.setName("jieshou");
                    System.out.println("链接到客户端");
                    
                    jieshou.start();
                }
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }
            
        }
        
        public void run()
        {
            if (Thread.currentThread().getName().equals("jieshou"))
            {
                try
                {
                    while(true)
                    {
                        System.out.println("执行到jieshou");
                        int strOutput = in.readInt();
                        System.out.println("接受到客户端请求");
                        System.out.println(strOutput);
                        out.writeInt(strOutput);
                    }
                    
                }
                catch (IOException e1)
                {
                    
                    e1.printStackTrace();
                }
            }
            
        }
        
    }
      

  4.   

    感谢楼上的兄弟 能告诉我下为什么要加  out.flush();这条语句么 
      

  5.   


    刷新输出流 啊
    看看API
      

  6.   


    你用的是outputStream,这个方法可以去掉,如果用的是bufferedOutputStream,那么实际上你写入流的时候只是在缓存中,并没有实际发送出去,当缓存满的时候才发出去,可以提高性能,减少IO操作,调用flush可以立即将缓存中的数据发送出去。