栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带套接字的Java客户端/服务器应用程序?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带套接字的Java客户端/服务器应用程序?

服务器监听连接。客户端建立连接时。客户端可以发送数据。在当前示例中,客户端发送消息“ Hi my
server”。为了终止连接,客户端发送消息“再见”。然后服务器也发送消息“再见”。最后,连接结束,服务器等待其他连接。这两个程序应在同一台计算机上运行。但是,如果要在两台不同的计算机上运行它们,则可以简单地通过运行服务器的计算机的IP地址来更改地址“
localhost”。

服务器

import java.io.*;import java.net.*;public class Provider{    ServerSocket providerSocket;    Socket connection = null;    ObjectOutputStream out;    ObjectInputStream in;    String message;    Provider(){}    void run()    {        try{ //1. creating a server socket providerSocket = new ServerSocket(2004, 10); //2. Wait for connection System.out.println("Waiting for connection"); connection = providerSocket.accept(); System.out.println("Connection received from " + connection.getInetAddress().getHostName()); //3. get Input and Output streams out = new ObjectOutputStream(connection.getOutputStream()); out.flush(); in = new ObjectInputStream(connection.getInputStream()); sendMessage("Connection successful"); //4. The two parts communicate via the input and output streams do{     try{         message = (String)in.readObject();         System.out.println("client>" + message);         if (message.equals("bye"))  sendMessage("bye");     }     catch(ClassNotFoundException classnot){         System.err.println("Data received in unknown format");     } }while(!message.equals("bye"));        }        catch(IOException ioException){ ioException.printStackTrace();        }        finally{ //4: Closing connection try{     in.close();     out.close();     providerSocket.close(); } catch(IOException ioException){     ioException.printStackTrace(); }        }    }    void sendMessage(String msg)    {        try{ out.writeObject(msg); out.flush(); System.out.println("server>" + msg);        }        catch(IOException ioException){ ioException.printStackTrace();        }    }    public static void main(String args[])    {        Provider server = new Provider();        while(true){ server.run();        }    }}

客户端

import java.io.*;import java.net.*;public class Requester{    Socket requestSocket;    ObjectOutputStream out;    ObjectInputStream in;    String message;    Requester(){}    void run()    {        try{ //1. creating a socket to connect to the server requestSocket = new Socket("localhost", 2004); System.out.println("Connected to localhost in port 2004"); //2. get Input and Output streams out = new ObjectOutputStream(requestSocket.getOutputStream()); out.flush(); in = new ObjectInputStream(requestSocket.getInputStream()); //3: Communicating with the server do{     try{         message = (String)in.readObject();         System.out.println("server>" + message);         sendMessage("Hi my server");         message = "bye";         sendMessage(message);     }     catch(ClassNotFoundException classNot){         System.err.println("data received in unknown format");     } }while(!message.equals("bye"));        }        catch(UnknownHostException unknownHost){ System.err.println("You are trying to connect to an unknown host!");        }        catch(IOException ioException){ ioException.printStackTrace();        }        finally{ //4: Closing connection try{     in.close();     out.close();     requestSocket.close(); } catch(IOException ioException){     ioException.printStackTrace(); }        }    }    void sendMessage(String msg)    {        try{ out.writeObject(msg); out.flush(); System.out.println("client>" + msg);        }        catch(IOException ioException){ ioException.printStackTrace();        }    }    public static void main(String args[])    {        Requester client = new Requester();        client.run();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号