用Socket編程,讀服務器幾個字符,再寫入本地顯示 – JAVA編程語言程序開發技術文章

服務器

[java] package com.zhangry.interview.java.socket; 
 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 
 
public class Server { 
    private ServerSocket ss; 
    private Socket socket; 
    private BufferedReader in; 
    private PrintWriter out; 
    public Server(){ 
         try { 
            ss = new ServerSocket(8888); 
            while(true){ 
                socket = ss.accept(); 
                String remoteIP =  socket.getInetAddress().getHostAddress(); 
                String remotePort = ":"+socket.getLocalPort(); 
                System.out.println("客戶端接入 ! IP :"+remoteIP+remotePort); 
                in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
                String line = in .readLine(); 
                System.out.println("客戶端說 :"+ line); 
                out= new PrintWriter(socket.getOutputStream(),true); 
                out.println("你的信息已收到!"); 
                out.close(); 
                in.close(); 
                socket.close(); 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
     
    public static void main(String[] args){ 
        new Server(); 
    } 

package com.zhangry.interview.java.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket ss;
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    public Server(){
         try {
            ss = new ServerSocket(8888);
            while(true){
                socket = ss.accept();
                String remoteIP =  socket.getInetAddress().getHostAddress();
                String remotePort = ":"+socket.getLocalPort();
                System.out.println("客戶端接入 ! IP :"+remoteIP+remotePort);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line = in .readLine();
                System.out.println("客戶端說 :"+ line);
                out= new PrintWriter(socket.getOutputStream(),true);
                out.println("你的信息已收到!");
                out.close();
                in.close();
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   
    public static void main(String[] args){
        new Server();
    }
}
 

 

客戶端

[java] package com.zhangry.interview.java.socket; 
 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 
 
public class Client { 
    Socket socket; 
    BufferedReader in; 
    PrintWriter out; 
    public Client(){ 
        try { 
            System.out.println("Try to connect to 127.0.0.1:8888"); 
            socket = new Socket("127.0.0.1",8888); 
            System.out.println("已經成功連接至服務器"); 
            System.out.println("請輸入要發向服務器的話:"); 
            BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); 
            out = new PrintWriter(socket.getOutputStream(),true); 
            out.println(line.readLine()); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
            System.out.println(in.readLine()); 
            out.close(); 
            in.close(); 
            socket.close(); 
        } catch (UnknownHostException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
     
    public static void main(String[] args){ 
        new Client(); 
    } 

 

摘自  張仁陽專欄

發佈留言