Flex和java的socket通訊(一)串連

來源:互聯網
上載者:User

服務端:java jdk1.5 (使用工具:eclipse3.2)

用戶端:flex 2

(一)串連

目的:實現flex和java的串連

服務端代碼:Server1.java

===============================================================================

import java.net.*;

import java.io.*;

public class Server1 {

public static void main(String[] args) {

ServerSocket server=null;

try{

server=new ServerSocket(8888);
//x1

System.out.println("伺服器通訊端已經被建立");

while(true){

System.out.println("等待客戶機");

Socket newSocket=server.accept(); //x2

System.out.println("已與客戶機串連");

}

}catch(IOException ie)

{

System.out.println(ie);

}finally{

Try

{

if(server!=null) server.close(); //x3

}catch(IOException ie){}

}

}

}

知識點:

X1:使用指定的連接埠號碼建立了一伺服器通訊端對象。

X2:調用accept()方法來等待客戶機的串連請求。

X3:關閉伺服器通訊端。如果一直沒有客戶機器提出串連請求,伺服器通訊端將會繼續等待。

編譯後運行如圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.