标题:socket问题
取消只看楼主
specilize
Rank: 4
等 级:业余侠客
帖 子:126
专家分:247
注 册:2011-2-20
结帖率:100%
 问题点数:0 回复次数:1 
socket问题
大家帮我看下这个程序有没有问题,为什么我测试时什么从客户端的控制台输入,服务端的控制台没有输出呢
客户端
import *;
import *;

public class ClientSide {

    private Socket socket;
    private BufferedReader reader;
    private PrintWriter writer;
   
    public ClientSide()
    {
        try{
            socket=new Socket(InetAddress.getByName("localhost"),4700);
            reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
        }
        catch(IOException e){
            System.out.println("Build Socket Error:"+e);
            System.exit(1);
        }
    }
    public void ClientStart()
    {
        try{
            BufferedReader readConsole=new BufferedReader(new InputStreamReader(System.in));
            String str;
            while((str=readConsole.readLine())!=null){
                writer.write(str);
                str=reader.readLine();
                System.out.println(str);
            }
            reader.close();
            writer.close();
            socket.close();
        }
        catch(IOException e){
            System.out.println("Error while client start:"+e);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args)throws IOException {
        // TODO Auto-generated method stub
        ClientSide client=new ClientSide();
        client.ClientStart();
    }

}
服务端
import *;
import *;

public class ServerSide {

    private ServerSocket serversocket;
    private Socket socket;

    public ServerSide(){
        try{
            serversocket=new ServerSocket(4700);
        }
        catch(IOException e){
             System.out.println("Build ServerSocket Error:"+e);
             System.exit(1);
        }
    }
    public void ServerStart() throws IOException{
        try{
            while(true){
                socket=serversocket.accept();
                new ServerThread(socket);
            }
        }
        catch(IOException e){
            System.out.println("ServerSocket can't accept a socket:"+e);
        }
        finally{
            socket.close();
            serversocket.close();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            ServerSide server=new ServerSide();
            server.ServerStart();
        }
        catch(IOException e){
            System.out.println("Can't no close ServerSocket");
        }
    }

}


class ServerThread extends Thread{
    private BufferedReader reader;
    private PrintWriter writer;
   
    public ServerThread(Socket soc){
        try{
            reader=new BufferedReader(new InputStreamReader(soc.getInputStream()));
            writer=new PrintWriter(new OutputStreamWriter(soc.getOutputStream()),true);
            start();
        }
        catch(IOException e){
            System.out.println("Bulid ServerThread Error:"+e);
            System.exit(1);
        }
    }
    public void run() {
        try{
            String str;
            while((str=reader.readLine())!="BYT"){
                writer.write(str);
                System.out.println(str);
            }
            reader.close();
            writer.close();
        }
        catch(IOException e){
            System.out.println("Read ClientSide Input Error:"+e);
        }
    }
}
搜索更多相关主题的帖子: localhost 测试 客户端 private Socket 
2011-10-20 13:12
specilize
Rank: 4
等 级:业余侠客
帖 子:126
专家分:247
注 册:2011-2-20
得分:0 
悲剧啊,忘了启动线程了。
2011-10-20 14:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-352999-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.320853 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved