TCP网络程序-2

来源:互联网 发布:超轻粘土淘宝 编辑:程序博客网 时间:2024/05/21 11:07

1.       简单的TCP服务器程序

package TcpServer;

import java.io.*;

import java.net.*;

public class TcpServer {

 

 

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

       // TODO Auto-generated method stub

       ServerSocket ss=new ServerSocket(8004);//定义服务器端的ServerSocket

       Socket s=ss.accept();//由接收到的数据产生一个Socket对象

       InputStream ips=s.getInputStream();//Soket获得输入流

       OutputStream ops=s.getOutputStream();//Soket获得输出流

       ops.write("welcome to beijing".getBytes());//在输出流里写入welecom..

       BufferedReader br=new BufferedReader(new InputStreamReader(ips));//包装输入流

       System.out.println(br.readLine());//显示输入的信息

       //byte [] buf=new byte[1024];

       //int len=ips.read(buf);

       //System.out.println(new String(buf,0,len));

      

       br.close();//释放资源

       //ips.close();

       ops.close();

       s.close();

       ss.close();

    }

 

}

在运行里调用:telnet 127.0.0.1 8004  显示:welcome to  Beijing

输入:abcd   控制台窗口里显示:abcd

 

2.       完善的TCP服务器程序模型

Socket的线程类:

package TcpServer;

import java.net.*;

import java.io.*;

public class Servicer implements Runnable {

    public Socket s=null;

    public Servicer(Socket s)

    {

       this.s=s;

    }

    public void run() {

       try {

           InputStream ips=s.getInputStream();

           OutputStream ops=s.getOutputStream();

           ops.write("welcome to beijing".getBytes());//在输出流里写入welecom..

           BufferedReader br=new BufferedReader(new InputStreamReader(ips));

           PrintWriter pw=new PrintWriter(ops,true);

           boolean pass=true;

           while (pass) {

              String strLine=br.readLine();

              if (strLine.equalsIgnoreCase("quit")) {

                  break;

              }

              System.out.println(strLine);

              String strEcho=new StringBuffer(strLine).reverse().toString();

              pw.println(strLine+"-->"+strEcho);

           }

           br.close();

           pw.close();

           s.close();

       } catch (Exception e) {

           e.printStackTrace();

       }

      

      

    }

 

}

主类:

package TcpServer;

import java.net.*;

public class ReverseServer {

 

    /**

     * @param args

     */

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

       ServerSocket ss=new ServerSocket(8006);

       boolean bRunning=true;

       while (bRunning) {

           Socket s=ss.accept();

           new Thread(new Servicer(s)).start();

       }

       ss.close();

    }

 

}

TCP服务器的编写要点:

1)  TCP服务器要想接受多个客户端的连接,需要循环调用ServerSocket.acceptfangfa

2)  服务器与每个客户端连接的会话过程不能互相影响,需要在独立的线程中运行。

3)  一个线程服务对象与一个服务器端Socket对象相关联,共同完成与一个客户端的会话。

 

3TCP客户端程序的编写

package TcpServer;

import java.net.*;

import java.io.*;

public class TcpClient {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

       try {

           Socket s=new Socket("127.0.0.1",8008);

           InputStream ips=s.getInputStream();

           OutputStream ops=s.getOutputStream();

           BufferedReader brNet=new BufferedReader(new InputStreamReader(ips));

           PrintWriter pw=new PrintWriter(ops,true);

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

           boolean pass=true;

           while (pass) {

              String strWord=brKeyBoard.readLine();

              pw.println(strWord);

              if (strWord.equalsIgnoreCase("quit")) {

                  break;

              }

              System.out.println(brNet.readLine());

           }

           pw.close();

           brNet.close();

           brKeyBoard.close();

           s.close();

       } catch (Exception e) {

           e.printStackTrace();

       }

      

 

    }

 

}

4.在TCP网络连结上传递对象

实体类:

package TcpServer;

 

import java.io.Serializable;

 

public class Student implements Serializable{

    int id;

    String name;

    int age;

    String department;

    public Student(int id,String name,int age,String department)

    {

       this.id=id;

       this.name=name;

       this.age=age;

       this.department=department;

    }

   

}

服务器类:

package TcpServer;

import java.net.*;

import java.io.*;

public class ObjectServer {

 

    /**

     * @param args

     */

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

       ServerSocket ss=new ServerSocket(8000);

       Socket s=ss.accept();

       OutputStream ops=s.getOutputStream();

       ObjectOutputStream oos=new ObjectOutputStream(ops);

       Student stu=new Student(19,"wangwu",22,"huaxue");

       oos.writeObject(stu);

       oos.close();

       s.close();

       ss.close();

    }

 

}

客户端类:

package TcpServer;

import java.net.*;

import java.io.*;

public class ObjectClient {

 

    /**

     * @param args

     */

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

       // TODO Auto-generated method stub

       Socket s=new Socket("127.0.0.1",8000);

       InputStream ips=s.getInputStream();

       ObjectInputStream ois=new ObjectInputStream(ips);

       Student stu=(Student)ois.readObject();

       System.out.println("id is:"+stu.id);

       System.out.println("name is:"+stu.name);

       System.out.println("age is:"+stu.age);

       System.out.println("department is:"+stu.department);

      

       ois.close();

       s.close();

    }

 

}

 

 

原创粉丝点击