我试了你的代码,没问题啊!我用jdk1.4.2编译的

解决方案 »

  1.   

    编译没错误
    不过你的服务器应用程序应该用1024以上的端口
    因为1024以下的端口一般为知名的网络服务或系统服务所用另外,DataInputStream的readLine()方法sun不推荐使用
      

  2.   

    谢谢大家!对了,好像我没有说清楚,我是想做一个telnet的客户端,执行起来就好像在dos窗口下的命令行一样,但是我没有得到这样的效果,请问问题出在什么地方?如何解决?谢谢了!
      

  3.   

    刚做的一个,实现聊天
    服务器端程序:
    import java.io.*;
    import java.net.*;class TcpSever
    {
    private static ServerSocket server=null; 

    public static void main(String[] agrs)
    {
        try
        {
        server=new ServerSocket(3000);
       
        while(true)
        {
           Socket s=server.accept();
        
         (new Thread(new DialogToClient(s))).start();
        }
    }
        catch(Exception e)
        {
        e.printStackTrace();
        
    }
    }
    }class DialogToClient implements Runnable
    {
    private Socket DialogSocket=null;

    public DialogToClient(Socket DialogSocket)
    {
    this.DialogSocket=DialogSocket;
    }

    public void run()
    {
    try
    {
    //System.out.println("in DialogToClient");
         InputStream input=DialogSocket.getInputStream();
         OutputStream output=DialogSocket.getOutputStream();
         (new Thread(new InputData(input))).start();
         (new Thread(new OutputData(output))).start();
        }
        catch(Exception e)
    {

    }
    }
    }class OutputData implements Runnable

        private BufferedReader read=null; 
        
        private OutputStream output=null;
        
    public OutputData(OutputStream output)
    {
    read=new BufferedReader(new InputStreamReader(System.in));

    this.output=output;
    }

    public void run()
    {
    while(true)
    {
    if(Flag.f.getFlag()==false)
       break;
       
    try
    {
    output.write(((read.readLine())+System.getProperty("line.separator")).getBytes());

    }
    catch(Exception e)
    {
    }

    }

    try
    {
    read.close();
    output.close();
    }
    catch(Exception e)
    {
    }
    }
    }class InputData implements Runnable
    {
        private BufferedReader input=null;
        
    public InputData(InputStream input)
    {
    this.input=new BufferedReader(new InputStreamReader(input));

    }

    public void run()
    {
    while(true)
    {
    byte[] b=new byte[2000];

    try
    {
    String str=input.readLine();

    if(str.equals("end"))
               break;
               
    System.out.println("client: "+str);
    }
    catch(Exception e)
    {

    }
    }

    Flag.f.setFlag(false);

    try
    {
    input.close();
    }
    catch(Exception e)
    {

    }

    }

    }class Flag
    {
    private boolean flag=true;

    public void setFlag(boolean flag)
    {
    this.flag=flag;
    }

    public boolean getFlag()
    {
        return flag;
    }

    private Flag()
    {

    }

    public static Flag f=new Flag();
    }客户端程序:
    import java.net.*;
    import java.io.*;class TcpClient
    {
    private BufferedReader read=null;
    private OutputStream output=null;

    public TcpClient(Socket client)
    {
    try
    {
    read=new BufferedReader(new InputStreamReader(System.in));
    output=client.getOutputStream();
    }
    catch(Exception e)
    {
    System.out.println("error in TcpClient constractor");
    }
    }
    public void writeData()
    {
    while(true)
    {
    try
    {
    String str=read.readLine();
    output.write((str+System.getProperty("line.separator")).getBytes());

         if(str.equals("end"))
           break;
    }
    catch(Exception e)
    {
    System.out.println("error in writeData");
    }
    }

    release();
    }

    public void release()
    {
    try
    {
    read.close();
    output.close();
    }
    catch(Exception e)
    {
    System.out.println("error in release");
    }
    }

    public static void main(String[] agrs)
    {
    try
    {
    Socket client=new Socket(InetAddress.getByName("192.168.1.8"),3000);



    Thread t=new Thread(new DialogToServer_ReadData(client));

        t.setDaemon(true);
    t.start();

    TcpClient x=new TcpClient(client);

    x.writeData();

    client.close();
    }
    catch(Exception e)
    {
    System.out.println("error in main");
    }

     
    }
    }class DialogToServer_ReadData implements Runnable 
    {
    private BufferedReader input=null;

    public DialogToServer_ReadData(Socket client)
    {
    try
    {
    input=new BufferedReader(new InputStreamReader(client.getInputStream()));
    }
    catch(Exception e)
    {
                      System.out.println("error in DialogToServer_ReadData constractor");
    }
    }
    public void run()
    {
    while(true)
    {
    try
    {
         System.out.println("server: "+input.readLine());
         }
         catch(Exception e)
         {
         System.out.println("error in run");
         }
    }
    }
    }
    可在同一机器上运行,要先运行服务器程序
    关闭时,应先关闭客户机程序
      

  4.   

    谢谢大家,可是我的目的是想实现与标准的telnet服务器进行通信,可能要遵守telnet协议吧,但是我不知道如何来做,谁有简单的telnet客户端程序,请贴出来好吗?谢谢!