Socket编程简单示例

服务器端

public class TcpServer

{

    public static void main(String[] args) throws Exception

    {

       ServerSocket ss = new ServerSocket(5000);

      

       while(true)

       {

           Socket socket = ss.accept();

          

           new ServerInputThread(socket).start();

           new ServerOutputThread(socket).start();

       }

    }

}

 

客户端

public class TcpClient

{

    public static void main(String[] args) throws Exception

    {

       Socket socket = new Socket("127.0.0.1", 5000);

 

       new ClientInputThread(socket).start();

       new ClientOutputThread(socket).start();

    }

}

 

服务器端读数据

public class ServerInputThread extends Thread

{

    private Socket socket;

 

    public ServerInputThread(Socket socket)

    {

       this.socket = socket;

    }

 

    @Override

    public void run()

    {

       try

       {

           InputStream is = socket.getInputStream();

 

           while (true)

           {

              byte[] buffer = new byte[1024];

 

              int len = is.read(buffer);

 

              String str = new String(buffer, 0, len);

 

              System.out.println(str);

           }

       }

       catch (IOException e)

       {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

 

服务器端写数据

public class ServerOutputThread extends Thread

{

    private Socket socket;

 

    public ServerOutputThread(Socket socket)

    {

       this.socket = socket;

    }

 

    @Override

    public void run()

    {

       try

       {

           OutputStream os = socket.getOutputStream();

 

           while (true)

           {

              BufferedReader br = new BufferedReader(new InputStreamReader(

                     System.in));

              String line = br.readLine();

              os.write(line.getBytes());

           }

       }

       catch (IOException e)

       {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

 

客户端读数据

public class ClientInputThread extends Thread

{

    private Socket socket;

 

    public ClientInputThread(Socket socket)

    {

       this.socket = socket;

    }

 

    @Override

    public void run()

    {

       try

       {

           InputStream is = socket.getInputStream();

 

           while (true)

           {

              byte[] buffer = new byte[1024];

 

              int len = is.read(buffer);

 

              String str = new String(buffer, 0, len);

 

              System.out.println(str);

           }

       }

       catch (IOException e)

       {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

 

客户端写数据

public class ClientOutputThread extends Thread

{

    private Socket socket;

 

    public ClientOutputThread(Socket socket)

    {

       this.socket = socket;

    }

 

    @Override

    public void run()

    {

       try

       {

           OutputStream os = socket.getOutputStream();

 

           while (true)

           {

              BufferedReader br = new BufferedReader(new InputStreamReader(

                     System.in));

              String line = br.readLine();

              os.write(line.getBytes());

           }

       }

       catch (IOException e)

       {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

}

 

 

posted @ 2011-08-17 21:46  水之原  阅读(361)  评论(0编辑  收藏  举报