博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络编程-第三节
阅读量:5895 次
发布时间:2019-06-19

本文共 6117 字,大约阅读时间需要 20 分钟。

服务器单独显示客户信息

在上星期实验4.3的基础上,修改服务端程序,使得服务器在接受到客户连接后弹出如下所示对话框,并将所有客户端发回的信息显示在这个对话框里。

 

代码:

EchoClient.java

package cn.edu.sise;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.InetSocketAddress;import java.net.Socket;import java.net.SocketAddress;import java.nio.charset.Charset;public class EchoClient {    public static void main(String[] args) {        Socket clientSocket = null;        BufferedReader in = null;        BufferedWriter out = null;        try {            //1.创建客户机套接字            clientSocket = new Socket();            SocketAddress remoteAddr=new InetSocketAddress("172.16.28.19",5000);             System.out.println("1.创建客户机套接字成功!");            //2.连接服务器            clientSocket.connect(remoteAddr);            System.out.println("2.客户机连接服务器localhost端口5000成功!");            System.out.println("客户机使用的地址和端口:"+clientSocket.getLocalSocketAddress());            //与服务器会话                       in = new BufferedReader(                 new InputStreamReader(                 clientSocket.getInputStream()));                       out = new BufferedWriter(                  new OutputStreamWriter(                              clientSocket.getOutputStream()));                       BufferedReader localIn = new BufferedReader(                    new InputStreamReader(                    System.in));                               String sendStr=localIn.readLine();            out.write(sendStr);  //向服务器发送字符串            out.newLine();            out.flush();            System.out.println("3.1向服务器发送字符串成功!"+sendStr);            String recvStr=in.readLine();  //从服务器接收字符串             System.out.println("3.2从服务器接收回送字符串成功!"+recvStr);        } catch (IOException ex) {            System.out.println("异常信息:"+ex.getMessage());        }        //关闭套接字和流        try {            if (in != null) in.close();            if (out != null)  out.close();            if (clientSocket != null)   clientSocket.close();             System.out.println("4.关闭套接字和流成功!");        } catch (IOException ex) {             System.out.println("异常信息:"+ex.getMessage());        }     }}

EchoServer.java

package cn.edu.sise;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketAddress;import java.nio.charset.Charset;public class EchoServer {    public static void main(String[] args) {        //1.启动服务器        ServerSocket listenSocket = null;         Socket clientSocket = null;         BufferedReader in = null;        BufferedWriter out = null;        try {            listenSocket = new ServerSocket();            SocketAddress serverAddr=new InetSocketAddress(InetAddress.getByName("172.16.28.19"),5000);            listenSocket.bind(serverAddr);             System.out.println("1.服务器启动成功!开始在localhost的5000端口侦听连接...");            //2.处理连接                                  clientSocket = listenSocket.accept();            System.out.println("2.客户机连接成功!客户机地址和端口:"+clientSocket.getRemoteSocketAddress());            //3.与客户机会话                       in = new BufferedReader(                 new InputStreamReader(                 clientSocket.getInputStream()));                       out = new BufferedWriter(                  new OutputStreamWriter(                              clientSocket.getOutputStream()));                       String recvStr=in.readLine();  //从客户机接收字符串            System.out.println("3.1 服务器收到字符串:"+recvStr);            out.write(recvStr);  //向客户机回送字符串                       out.newLine();            out.flush();             System.out.println("3.2 服务器回送字符串成功:"+recvStr);        } catch (IOException ex) {            System.out.println("异常信息:"+ex.getMessage());        }        //4.关闭套接字和流        try {              if (in != null)in.close();            if (out != null)  out.close();            if (listenSocket != null)   listenSocket.close();            if (clientSocket != null)   clientSocket.close();             System.out.println("4.关闭套接字和流成功!");        } catch (IOException ex) {           System.out.println("异常信息"+ex.getMessage());        }    }}

TestBuffered.java

package cn.edu.sise;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Date;public class TestBuffered {    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub        //InputStream fis = new FileInputStream("file");//������52�룩        InputStream fis =new BufferedInputStream( new FileInputStream("file"));//������1�룩        System.out.println(new Date() + ": ���Կ�ʼ...");            while (fis.read()!=-1)            {                //��ȡ�����ļ�������            }            System.out.println(new Date() + ": ���Խ���");        fis.close();    }}

TestInputStream.java

package cn.edu.sise;import java.io.BufferedInputStream;import java.io.IOException;public class TestInputStream {    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub        BufferedInputStream bis = new BufferedInputStream(System.in);        int data = bis.read();        while (data!=-1){        System.out.println(data + " " + (char)data);        if(data==13) break;        data = bis.read();        }        bis.close();    }}

TestReader.java

package cn.edu.sise;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class TestReader {    public static void main(String[] args) throws IOException {        // TODO Auto-generated method stub        BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));        int data = bis.read();        while (data!=-1){        System.out.println(data + " " + (char)data);        if(data==13) break;        data = bis.read();        }        bis.close();    }}

 

转载于:https://www.cnblogs.com/wei01/p/weihua03.html

你可能感兴趣的文章
gitlab 完整部署实例
查看>>
影响企业信息化成败的几点因素
查看>>
SCCM 2016 配置管理系列(Part8)
查看>>
struts中的xwork源码下载地址
查看>>
我的友情链接
查看>>
PHP 程序员的技术成长规划
查看>>
python基础教程_学习笔记19:标准库:一些最爱——集合、堆和双端队列
查看>>
js replace,正则截取字符串内容
查看>>
javascript继承方式详解
查看>>
lnmp环境搭建
查看>>
自定义session扫描器精确控制session销毁时间--学习笔记
查看>>
仿射变换
查看>>
视频直播点播nginx-rtmp开发手册中文版
查看>>
PHP队列的实现
查看>>
单点登录加验证码例子
查看>>
[T-SQL]从变量与数据类型说起
查看>>
occActiveX - ActiveX with OpenCASCADE
查看>>
BeanUtils\DBUtils
查看>>
python模块--os模块
查看>>
linux下单节点oracle数据库间ogg搭建
查看>>