国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
java socket 實現服務端與客戶端

一對一的服務端和客戶機:一個服務端只能同時服務一個客戶端
服務端:

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()方法說明:
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.

 

 

客戶端
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();
 }
}

一對多的服務端和客戶機:一個服務端只能同時服務多個客戶端,需要使用多線程來實現

服務端:調用具體的處理線程完成處理
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實例初始化具體的處理線程對象
                //使用Runnable接口和使用extends Thread的區(qū)別:
                // 如果是使用Runnable接口,需要這么寫:
                   new Thread(child).start();               
                //注意:不能寫成:child.run();
                // 如果使用extends Thread,只需要child.start()就可以,因為start()是Thread的方法
                }
        }
}       

處理線程類:
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{
                                                               
              }
        }
       
}

客戶端:
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();
 }
}

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

聯系客服