2020-11-10

1.编写完整的基于Socket的多客户/服务器通信程序

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=new ServerSocket(4700); 
       }catch(IOException e) {
          System.out.println("Could not listen on port:4700.");
          System.exit(-1);
       }
       while(listening){ 
        new ServerThread(serverSocket.accept(),clientnum).start();
         clientnum++; 
      }
       serverSocket.close(); 
   }
 }
    
package MultiSocket;
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=new ServerSocket(4700);
         }catch(IOException e) {
             System.out.println("Could not listen on port:4700.");
             System.exit(-1); 
         while(listening){ 
             new ServerThread(serverSocket.accept(),clientnum).start();
             clientnum++;          
             }
         serverSocket.close(); 
     }
 }

 package MultiSocket;
 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=new ServerSocket(4700);
         }catch(IOException e) {
             System.out.println("Could not listen on port:4700.");
             System.exit(-1); 
         }
         while(listening){ 
             new ServerThread(serverSocket.accept(),clientnum).start();
             clientnum++; 
         }
         serverSocket.close(); 
 }

2.编写完整的基于数据报的多客户/服务器通信程序

 import java.io.*;
 import java.net.*;
 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");
          while(!(readLine = sin.readLine()).equals("bye")) {
          byte[] buf = readLine.getBytes();
          DatagramPacket packet=new DatagramPacket(buf, buf.length, address, 4445);
          socket.send(packet); 
          buf = new byte[256];
          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(); 
     }
 }
 
 import java.io.*;
 import java.net.*; 
 class QuoteServerThread extends Thread
 {  
     protected DatagramSocket socket=null;
     protected BufferedReader in=null;
     protected boolean moreQuotes=true;
     public QuoteServerThread() throws IOException {
     this("QuoteServerThread");
     }
     public QuoteServerThread(String name) throws IOException {
         super(name); 
         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);
                 socket.receive(packet);                 
                 System.out.println("Client : "+new String(packet.getData())); 
                 String dString= in.readLine();    
                 if(dString.equals("bye")){moreQuotes=false;}
                 buf=dString.getBytes();
                 InetAddress address=packet.getAddress();
                 int port=packet.getPort(); 
                 packet=new DatagramPacket(buf,buf.length,address,port);
                 socket.send(packet);
             }catch(IOException e) { 
                 e.printStackTrace(); 
                 moreQuotes=false; 
             }
          }
          socket.close(); 
        }    
 } 
 public class QuoteServer{
     public static void main(String args[]) throws java.io.IOException
         {
         new QuoteServerThread().start();
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值