serversocket 堵塞

来源:互联网 发布:如何投诉一橙网络 编辑:程序博客网 时间:2024/04/27 16:59

 

 

救命 救命

服务器端 

 package web;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Administrator
 */
public class TcpServer {
   
    /** Creates a new instance of TcpServer */
    public TcpServer() {
    }
    public static void main(String[] args){
    ServerSocket svesoc=null;
    Socket soc=null;
    InputStream is=null;
    OutputStream os=null;
    DataInputStream in=null;
    PrintStream out=null;
        try {
            svesoc=new ServerSocket(8000,3);
            svesoc.setSoTimeout(2000000);
            soc=svesoc.accept();
            is=soc.getInputStream();
            os=soc.getOutputStream();
            out=new PrintStream(os);
           
            InetAddress clientIP=soc.getInetAddress();
            System.out.println("Client's IP address  "+clientIP);
            System.out.println("Client's port "+soc.getPort());
            out.println("welcome");
            byte[] bmsg=new byte[1024];
        
            in.read(bmsg);// 总是在这里出现空指针异常    它为什么不堵塞呢?

                                        //
          String  str=new String(bmsg).trim();
            while(!str.equals("quit")){System.out.println("client said:"+str);
            in.read(bmsg);
            str=new String(bmsg).trim();
            }
           
            System.out.println("client wants to leave");
            is.close();
            os.close();
            soc.close();
            System.exit(0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    
    
    }
 
}
 

 

 

客户端package web;

import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

/**
 *
 * @author Administrator
 */
public class TcpClient {
   
    /** Creates a new instance of TcpClient */
    public TcpClient() {
    }
    public static void main(String[] args){
 
    Socket soc=null;
    InputStream is=null;
    OutputStream os=null;
    DataInputStream in=null;
    PrintStream out=null;
    String strin=null;
        try {
            soc=new Socket("localhost",8000);
            System.out.println("Connecting to the server");
          
            is=soc.getInputStream();
            os=soc.getOutputStream();
            out=new PrintStream(os);
            in=new DataInputStream(is);
           
            strin=in.readLine();
           
            System.out.println("Server said   "+strin);
           
            byte[] bmsg=new byte[200];
            System.in.read(bmsg);
            String msg=new String(bmsg).trim();
           
            while(!msg.equals("quit")){
                 out.println(msg);
                 System.in.read(bmsg);
                 msg=new String(bmsg).trim();
                 }
           
            out.println(msg);
           
          
           
         
           
           
        
            is.close();
            os.close();
            soc.close();
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    
    }
}