回 帖 发 新 帖 刷新版面

主题:请谁帮我看一下这个程序!

import java.io.*;
import java.net.*;
public class TServer{    
    public static void main(String[] args) {        
        try{
            ServerSocket ss=new ServerSocket(4700);
            Socket s=ss.accept();
            BufferedReader sysIn=new BufferedReader(new InputStreamReader(System.in));            
            PrintWriter writer=new PrintWriter(s.getOutputStream());            
            BufferedReader reader=new BufferedReader(new InputStreamReader(s.getInputStream()));
            System.out.println("client:"+reader.readLine());
            String line=sysIn.readLine();
            while(!line.equals("goodbye")){
                writer.print(line);
                writer.flush();
                System.out.println("server:"+line);
                System.out.println("client:"+reader.readLine());
                line=sysIn.readLine();
                
            }
            reader.close();
            writer.close();
            ss.close();
            s.close();
        }catch (Exception e){

            e.printStackTrace();
        }

    }

}
---------------------------------------------------------


-------------------------------------------------------------------
import java.io.*;
import java.net.*;
public class TClient {    
    public static void main(String[] args) {        
        try{
            Socket s=new Socket("127.0.0.1",4700);    
            BufferedReader sysIn=new BufferedReader(new InputStreamReader(System.in));
            PrintWriter writer=new PrintWriter(s.getOutputStream());
            BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line=sysIn.readLine();
            while(!line.equals("goodbye")){
                writer.println(line);
                writer.flush();
                System.out.println("client:"+line);
                System.out.println("server:"+reader.readLine());
                line=sysIn.readLine();
            }
            writer.close();
            reader.close();
            s.close();
            
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
----------------------------------------------
---------------------------------------------------------
是一个点对点程序。程序的功能是在自己电脑上开两个MSDOS窗口,两一个窗口进行对话。
出现的问题是:当两个窗口各自输入一句话后就出现阻塞了,得强行退出。
本人基础不行,实在看不出是哪里出现了问题。还希望谁能帮助一下!谢谢!

回复列表 (共3个回复)

沙发

服务器端和客户端的两个Socket的读写最好分别由2个线程分别控制。

服务器代码:

import java.io.*;
import java.net.*;

public class TServer
{    
    Socket server;
    ServerSocket ss ;
    BufferedReader read;
    BufferedWriter write;
    BufferedReader br;
    
    public TServer()
    {
        try 
        {
            ss = new ServerSocket(4700);
            server = ss.accept();
            read = new BufferedReader(new InputStreamReader(server.getInputStream()));
            write = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
            br = new BufferedReader(new InputStreamReader(System.in));
            
            new ServerRead().start();
            new ServerWrite().start();
            
        } 
        catch (Exception e) 
        {
            
        }
    }
    public static void main(String[] args) 
    {        
        new TServer();
    }
    
    class ServerRead extends Thread  //服务器获取从客户端发来信息的线程
    {
        private String str;
        public void run()
        {
            try
            {
                while(!(str=read.readLine()).equals("goodbye"))
                {
                    System.out.println("Client:"+str);
                }
                read.close();
            } 
            catch (Exception e)
            {
                
            }
        }
    }
    
    class ServerWrite extends Thread //服务器向客户端发消息的线程
    {
        private String str;
        public void run()
        {
            try
            {
                while(!(str=br.readLine()).equals("goodbye"))
                {
                    write.write(str+"\r\n");
                    write.flush();
                }
                write.write("goodbye\r\n");
                br.close();
                write.close();
            } 
            catch (Exception e)
            {
                
            }
        }
    }
    
    public void finalize()  //在析构方法中关闭Socket和ServerSocket
    {
        try 
        {
            server.close();
            ss.close();
        }
        catch (Exception e) 
        {
            
        }
    }
}

板凳

客户端代码

import java.io.*;
import java.net.*;

public class TClient 
{    
    private Socket client;
    private BufferedReader read;
    private BufferedWriter write;
    private BufferedReader br;
    
    public TClient()
    {
        try 
        {
            client = new Socket("127.0.0.1",4700);
            read = new BufferedReader(new InputStreamReader(client.getInputStream()));
            write = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            br = new BufferedReader(new InputStreamReader(System.in));
            
            new ClientWrite().start();
            new ClientRead().start();
        }
        catch (Exception e) 
        {
            
        }
    }
    public static void main(String[] args) 
    {        
       new TClient();
    }
    
    class ClientRead extends Thread  //客户端获取服务器消息的线程
    {
        private String str;
        public void run()
        {
            try 
            {
                while(!(str=read.readLine()).equals("goodbye"))
                {
                    System.out.println("Server:"+str);
                }
                read.close();
            }
            catch (Exception e) 
            {
                
            }
        }
    }
    
    class ClientWrite extends Thread 客户端向服务器发消息的线程
    {
        private String str;
        
        public void run()
        {
            try 
            {
                while(!(str=br.readLine()).equals("goodbye"))
                {
                    write.write(str+"\r\n");
                    write.flush();
                }
                write.write("goodbye\r\n");
                br.close();
                write.close();
            } 
            catch (Exception e) 
            {
                
            }
        }
    }
    
    public void finalize()
    {
        try 
        {
            client.close();
        }
        catch (Exception e) 
        {
            
        }
    }
}

3 楼

谢谢楼上的,发现我的程序是reader,writer变量定义有问题.

我来回复

您尚未登录,请登录后再回复。点此登录或注册