import java.io.*; import java.net.*; public class MultiTalkServer{ static int clientnum=0; //静态成员变量,记录当前客户的个数 public static void main(String args[]) throws IOException { ServerSocket serverSocket=null; boolean listening=true; try{ //创建一个ServerSocket在端口4700监听客户请求 serverSocket=new ServerSocket(4700); }catch(IOException e) { System.out.println("Could not listen on port:4700."); System.exit(-1); //退出 } while(listening){ //循环监听 //监听到客户请求,根据得到的Socket对象和客户计数创建服务线程,并启动之 new ServerThread(serverSocket.accept(),clientnum).start(); clientnum++; //增加客户计数 } serverSocket.close(); //关闭ServerSocket } }二.仿照例15.5,编写基于UDP数据报的多客户/服务器通信程序
(以下参照博客园)
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.io.BufferedReader; import java.io.InputStreamReader; public class QuoteClient { public static void main(String[] args) throws IOException { DatagramSocket socket=new DatagramSocket();//创建数据报套接字 BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); String readLine; InetAddress address=InetAddress.getByName("127.0.0.1");//Server的IP信息 while(!(readLine = sin.readLine()).equals("bye")) { byte[] buf = readLine.getBytes(); //创建DatagramPacket对象 DatagramPacket packet=new DatagramPacket(buf, buf.length, address, 4445); socket.send(packet); //发送 buf = new byte[256]; //创建新的DatagramPacket对象,用来接收数据报 packet=new DatagramPacket(buf,buf.length); socket.receive(packet); //接收 buf = packet.getData(); //根据接收到的字节数组生成相应的字符串 String received=new String(buf); //打印生成的字符串 System.out.println("Quote of the Sever: "+received ); } socket.close(); //关闭套接口 } }
public class QuoteServer { public static void main(String args[])throws java.io.IOException{ new QuoteServerThread().start();// 启动一个QuoteServerThread线程 } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class QuoteServerThread extends Thread// 服务器线程 { protected DatagramSocket socket = null;// DatagramSocket protected BufferedReader in = null;// Reader protected boolean moreQuotes = true;// 标志变量。是否继续 *** 作 public QuoteServerThread() throws IOException {// this("QuoteServerThread");// QuoteServerThread } public QuoteServerThread(String name) throws IOException { super(name); // socket = new DatagramSocket(4445);// 创建数据报套接字端口4445 in = new BufferedReader(new InputStreamReader(System.in)); } public void run() // 线程主体 { while (moreQuotes) { try { byte[] buf = new byte[256]; // 创建缓冲区 DatagramPacket packet = new DatagramPacket(buf, buf.length); // 由缓冲区构建DatagramPacket对象 socket.receive(packet); // 接收数据报 // 输出客户端发送的内容 System.out.println(new String(packet.getData())); // 从屏幕获取输入内容,作为发送给客户端的内容 String dString = in.readLine(); // 如果是bey,则向客户端发完消息后退出 if (dString.equals("bye")) { moreQuotes = false; } buf = dString.getBytes();// 把String转换成字节数组,以便传送 // 从Client端传来的Packet中得到Client地址 InetAddress address = packet.getAddress(); int port = packet.getPort(); // 端口号 // 根据客户端信息构建DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port); socket.send(packet); // 发送数据报 } catch (IOException e) { // 异常处理 e.printStackTrace(); // 输出异常栈信息 moreQuotes = false; // 标志变量置false,以结束循环 } } socket.close(); // 关闭数据报套接字 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)