糯米文學吧

位置:首頁 > 計算機 > 計算機二級

2016計算機二級考試Java語言程序設計輔導

2016年3月計算機考試在3月26日-29日進行,為了幫助考生全面備戰,下面是本站小編為大家搜索整理的`2016計算機二級考試Java語言程序設計輔導,供大家參考學習,預祝考生考試順利。

2016計算機二級考試Java語言程序設計輔導

  練習一

如下的GreetingClient 是一個客户端程序,該程序通過socket連接到服務器併發送一個請求,然後等待一個響應。

// 文件

import .*;

import .*;

public class GreetingClient

{

public static void main(String [] args)

{

String serverName = args[0];

int port = eInt(args[1]);

try

{

tln("Connecting to " + serverName

+ " on port " + port);

Socket client = new Socket(serverName, port);

tln("Just connected to "

+ emoteSocketAddress());

OutputStream outToServer = utputStream();

DataOutputStream out =

new DataOutputStream(outToServer);

eUTF("Hello from "

+ ocalSocketAddress());

InputStream inFromServer = nputStream();

DataInputStream in =

new DataInputStream(inFromServer);

tln("Server says " + UTF());

e();

}catch(IOException e)

{

tStackTrace();

}

}

}