java socket編程執行個體代碼講解_java

來源:互聯網
上載者:User

1、所謂socket通常也稱作"通訊端",用於描述IP地址和連接埠,是一個通訊鏈的控制代碼。應用程式通常通過"通訊端"向網路發出請求或者應答網路請求。

操作java socket時用到的最多的三個方法為:

accept():主要用於伺服器端產生“阻塞”,等待用戶端的連結請求,並且返回一個用戶端的Socket執行個體;

getInputStream():方法主要用來獲得網路連接輸入,同時返回一個InputStream對象執行個體;

getOutputStream():方法和上面的getInputStream相反。

2、一般要建立Java的Socket串連,應首先明確伺服器端和用戶端,伺服器端使用ServerSocket監聽指定的連接埠,使用accept等待用戶端請求,網站連結接,開始會話、完成會話後,關閉連結。(注意,一般socket的關閉都應該是伺服器端來進行的,後面說);用戶端使用Socket對網路中的某個伺服器的某個連接埠發出連結請求,串連成功,開始會話,會話完成,Socket關閉。

2、範例程式碼:

伺服器端:

複製代碼 代碼如下:

package com.icer.server;

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

/**
 * Server
 *
 * @author Icer
 *
 */
public class Server {

    private ServerSocket ss;
    private Socket s;
    private BufferedReader br;
    private PrintWriter pw;

    public Server() {
        try {
                ss = new ServerSocket(10000);
                System.out.println("Server is starting...");
                s = ss.accept();
                br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                pw = new PrintWriter(s.getOutputStream(),true);
                String line = br.readLine();
                System.out.println(line);
                pw.println("your word is:" + line);
                //pw.println("helloworld");
                br.close();
                pw.close();

        } catch (IOException ie) {
            ie.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        new Server();
    }
}

用戶端:

複製代碼 代碼如下:

package com.icer.client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Client
 * @author Icer
 *
 */
public class Client {

    private Socket s;
    private BufferedReader br;
    //private BufferedReader line;
    private PrintWriter pw;
    private String line = "";
    public Client() {
        try{
            s = new Socket("127.0.0.1",10000);
            pw = new PrintWriter(s.getOutputStream(),true);
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw.println("hello");
            line = br.readLine();
            System.out.println(line);

            br.close();
            pw.close();
        }catch(IOException ie){
            ie.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        new Client();
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.