欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
java socket 實(shí)現服務(wù)端與客戶(hù)端

一對一的服務(wù)端和客戶(hù)機:一個(gè)服務(wù)端只能同時(shí)服務(wù)一個(gè)客戶(hù)端
服務(wù)端:

import java.io.*;
import java.net.*;

public class MyServer {
 public static void main(String[] args) throws IOException{
  ServerSocket server=new ServerSocket(5678);
  Socket client=server.accept();
        BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
        PrintWriter out=new PrintWriter(client.getOutputStream());
        while(true){
                 String str=in.readLine();
                System.out.println(str);
                out.println("has receive....");
                 out.flush();
                 if(str.equals("end"))
                         break;
                }
                client.close();
        }
}

accept()方法說(shuō)明:
public Socket accept() throws IOException
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.
A new Socket s is created and, if there is a security manager, the security manager‘s checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

 

 

客戶(hù)端
import java.net.*;
import java.io.*;

public class Client{
 static Socket server;
 
 public static void main(String[] args)throws Exception{
  server=new Socket(InetAddress.getLocalHost(),5678);
         BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
         PrintWriter out=new PrintWriter(server.getOutputStream());
         //標準輸入
  BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
 
  while(true){
           String str=wt.readLine();
           out.println(str);
           out.flush();
           if(str.equals("end")){
                    break;
          }
           System.out.println(in.readLine());
  }
  server.close();
 }
}

一對多的服務(wù)端和客戶(hù)機:一個(gè)服務(wù)端只能同時(shí)服務(wù)多個(gè)客戶(hù)端,需要使用多線(xiàn)程來(lái)實(shí)現

服務(wù)端:調用具體的處理線(xiàn)程完成處理
import java.io.*;
import java.net.*;

public class MyMultiServer {
 public static void main(String[] args) throws IOException{
  ServerSocket server=new ServerSocket(5678);
  while (true){
                Socket client=server.accept();
                ChildTh child=new ChildTh(client);//用socket實(shí)例初始化具體的處理線(xiàn)程對象
                //使用Runnable接口和使用extends Thread的區別:
                // 如果是使用Runnable接口,需要這么寫(xiě):
                   new Thread(child).start();               
                //注意:不能寫(xiě)成:child.run();
                // 如果使用extends Thread,只需要child.start()就可以,因為start()是Thread的方法
                }
        }
}       

處理線(xiàn)程類(lèi):
import java.io.*;
import java.net.*;
public class ChildTh implements Runnable{
        private Socket client;
        public ChildTh(Socket client){
                this.client=client;
        }
       
        public void run(){       
           try{       
             BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
             PrintWriter out=new PrintWriter(client.getOutputStream());
             while(true){
                     String str=in.readLine();
                     System.out.println(str);
                     out.println("has receive....");
                     out.flush();
                     if(str.equals("end"))
                        break;
             }       
                       
             client.close();
            }catch(Exception e){
                  System.out.println("error in the close the socket!");                       
                  e.printStackTrace();
              }
              finally{
                                                               
              }
        }
       
}

客戶(hù)端:
import java.net.*;
import java.io.*;

public class Client{
 static Socket server;
 
 public static void main(String[] args)throws Exception{
  server=new Socket(InetAddress.getLocalHost(),5678);
         BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
         PrintWriter out=new PrintWriter(server.getOutputStream());
         //標準輸入
  BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
 
  while(true){
           String str=wt.readLine();
           out.println(str);
           out.flush();
           if(str.equals("end")){
                    break;
          }
           System.out.println(in.readLine());
  }
  server.close();
 }
}

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
淺出Java Socket 編程
java socket編程指南
一篇不錯的介紹Java Socket編程的文章3 — IT技術(shù)
服務(wù)器端應用程序
Socket通道續3---io框架模型演化
【android】Socket簡(jiǎn)單用法
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久