class Client extends Thread{
    private Socket socket;
    private InputStream socketInputStream;
    private OutputStream socketOutputStream;    public Client(String ip, int port) throws Exception{
        socket = new Socket(ip,port);
        this.socket.setSoTimeout(30000); //数据超时为30秒
        this.socketOutputStream = socket.getOutputStream();
        this.socketInputStream = socket.getInputStream();
        start();
    }    public void run() throws Throwable {
        int readSize = 0;
        while (true) {
            byte[] in = new byte[1024];
            try{
                readSize = this.socketInputStream.read(in);
                // 处理你的数据 in                send(....);
            } catch(Exception e){
                try{
                    socketInputStream.close();
                    socketOutputStream.close();
                    socket.close();
                }catch(Exception ex){
                }
            }
        }
    }    private void send(byte[] ob){
        socketOutputStream.write(ob, 0, ob.length);
        socketOutputStream.flush();
    }
}

解决方案 »

  1.   

    import java.net.*;
    import java.io.*;public class myClient2
    {
       public static void main( String[ ] args )
       {
          try
          {
             Socket s = new Socket( "10.0.0.143", 5656 );
             PrintStream out = new PrintStream( s.getOutputStream( ) );
             BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream( ) ) );
             BufferedReader std = new BufferedReader( new InputStreamReader( System.in ) );
             String x = in.readLine( );
             String x1 = std.readLine( );
             if( !x.equals( "password") )
             {
                System.out.println( "Server Wrong" );
                System.exit( 0 );
             }
             else
             {
                out.println( x1 );
             }
             x = in.readLine( );
             if( x.equals( "wrong" ) )
             {
                System.out.println( "wrong password" );
             }
             else
             {
                System.out.println( x );
             }
             out.close( );
             s.close( );
             in.close( );
          }
          catch( IOException e )
          {
          }
       }
    }
    import java.net.*;
    import java.io.*;
    import java.util.*;public class myServer2
    {
       public static void main( String[ ] args )
       {
          try
          {
             ServerSocket ss = new ServerSocket( 5656 );
             while( true )
             {
                Socket s = ss.accept( );
                PrintStream out = new PrintStream( s.getOutputStream( ) );
                BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream( ) ) );         
                out.println( "password" );
                String pass = in.readLine( );
                System.out.println( pass );
                if( !pass.equals( "xiaoqiang" ) )
                {
                   out.println( "wrong" );
                }
                else
                {
                   Date d = new Date( );
                   String t = d.toString( );
                   out.println( t );
                }
                out.close( );
                s.close( );
                in.close ( );
             }
          }
          catch( IOException e )
          {
          }
       }   
    }不知是否符合楼主的要求
      

  2.   

    import java.net.*;
    import java.io.*;public class TestSocketClient{
    public  static  void  main(String[]  args)
    {  
    String  serviceHost  =  "10.34.52.51";  
    int  servicePort  =  7777;  

    java.net.Socket  socket=null;
    BufferedReader reader=null;
    PrintWriter out=null;
    try
    {
    socket  =  new  java.net.Socket(serviceHost,servicePort);  
    System.out.println("client  socket:  "  +  socket); 

    reader = new BufferedReader (new InputStreamReader(System.in)); 

    out=new PrintWriter(socket.getOutputStream()); 

    String str="";
    do{
    str=reader.readLine();
    System.out.println("out:"+str);
    out.println(str);
    }while(!str.equals("."));

    }
    catch  (IOException  ex)
    {  
    ex.printStackTrace(); 
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    try
    {
    reader.close();
    out.close();
    socket.close();
    }
    catch  (IOException  ex)
    {  
    ex.printStackTrace();  

    }
    }
    }