第19章网络通讯

服务器端程序:

package ninteen;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class tong {
	    public void server(){
	        ServerSocket ss = null;
	        Socket s = null;
	        InputStream is = null;
	        OutputStream os = null;
	        try {
	            //创建一个ServerSocket 对象,并指明端口号
	            ss = new ServerSocket(2000);
	            System.out.println("服务端已启动,等待客户端连接!");
	            //调用accept()方法,返回一个Socket的对象
	            s = ss.accept();
	            //调用Socket对象的getInputStream()方法获取一个从客户端发送过来的输入流
	            is = s.getInputStream();
	            //对获取的输入流进行操作
	            byte[] b = new byte[20];
	            int len;
	            while((len = is.read(b)) != -1){
	                String str = new String(b,0,len);
	                System.out.println(str);
	            }
	            //向客户端返回信息
	            os = s.getOutputStream();
	            os.write("已收到请求!".getBytes());
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            //关闭对应的流及其相关的资源
	            if(is != null){
	                try {
	                    is.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(s != null){
	                try {
	                    s.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(ss != null){
	                try {
	                    ss.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(os != null){
	                try {
	                    os.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	    }

	    public static void main(String[] args) {
	        tong s = new tong();
	        s.server();
	    }
	}

客户端程序:

package ninteen;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class ke {
	
	    public  ke(){
	        Socket s = null;
	        OutputStream os = null;
	        InputStream is = null;
	        try {
	            //创建一个Socket对象,通过构造器指明服务端的IP地址,以及其接受程序的端口号
	            s = new Socket(InetAddress.getByName("10.12.12.7"),2000);
	            //发送数据,方法返回OutputStream对象
	            os = s.getOutputStream();
	            //数据输出
	            os.write("我是客户端!请求连接!".getBytes());
	            //结束数据输出
	            s.shutdownOutput();
	            //接受数据
	            is = s.getInputStream();
	            byte[] b = new byte[20];
	            int len;
	            while((len = is.read(b)) != -1){
	                String str = new String(b,0,len);
	                System.out.println(str);
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally{
	            //关闭对应的流及其相关资源
	            if(os != null){
	                try {
	                    os.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(s != null){
	                try {
	                    s.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(is != null){
	                try {
	                    is.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	    }

	    public static void main(String[] args) {
	        ke c = new ke();
	       
	    }
	}

运行结果:
服务器 服务端已启动,等待客户端连接!
客户端 已收到请求!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值