网络编程考试题(网络编程考试题及答案)

http://www.itjxue.com  2023-02-10 10:00  来源:未知  点击次数: 

C#网络编程作业题目,应该很简单,不过本人愚笨呐,哪位高手帮个忙啊?感激不尽呀~

题1

static void Main(string[] args)

{

int num;

int i = 1;

int j = 1;

int x = 1;

for (i = 1; i = 5;i++ )

{

num = i * i;

Console.WriteLine(num);

}

while (j 6)

{

num = i * i;

Console.WriteLine(num);

i++;

}

do

{

num = i * i;

Console.WriteLine(num);

i++;

} while (x 6);

}

题2

public static bool character(string input)

{

if (input.Length != 5)

{

Console.WriteLine("输入长度不符合,请重新输入!");

return false;

}

else if (input == input.ToLower())

{

Console.WriteLine("请输入大写字母!");

return false;

}

else

{

Console.WriteLine("输入成功!");

return true;

}

}

static void Main(string[] args)

{

for (; ; )

{

string input;

Console.WriteLine("请输入五个大写字母");

input = Console.ReadLine();

if (character(input))

{

break;

}

}

}

题3

static void Main(string[] args)

{

for (; ; )

{

int num;

Console.WriteLine("请输入一个数:");

num = int.Parse(Console.ReadLine());

if (num 0)

{

for (int i = 1; i = num; i++)

{

Console.Write(i + " ");

}

Console.WriteLine();

}

else

{

Console.WriteLine("程序退出!");

break;

}

}

}

题4 俺数学丑...........汗一个..........

求大神帮忙做几道java题,感谢 !有关于网络编程的

第二题 一个doDemo方法搞定 这个是列出D盘下所有文件及文件目录,然后再列出所有的.txt后缀的文件。static List allList = new ArrayList();static List txtList = new ArrayList();public static void doDemo() {File file = new File("D:" + File.separator);if (null == file)return;allList.clear();txtList.clear();listAllFile(file);for (String p : allList) {System.out.println("file: " + p);}for (String txt : txtList) {System.out.println("txt file: " + txt);}}public static void listAllFile(File dir) {if (null == dir || !dir.exists()) {return;}if (dir.isDirectory()) {File[] files = dir.listFiles();if (null != files) {for (int i = 0; i files.length; i++) {if (files[i].isDirectory()) {listAllFile(files[i]);} else {String fileName = files[i].getName();if (fileName.endsWith(".txt")) {txtList.add(files[i].getPath());}allList.add(files[i].getPath());}}}} else {allList.add(dir.getPath());}}

JAVA网络编程基础试题!300分!请一定要帮我做一下,两个解答题。一个编程题,重点是编程题,谢谢了!!

2.百度百科搜索很详细

3.附件中的代码仅供参考

TcpSocketServerDemo.java

package?yaoshun.InetAddress;

import?java.io.DataInputStream;

import?java.io.DataOutputStream;

import?java.io.IOException;

import?java.net.ServerSocket;

import?java.net.Socket;

import?java.util.ArrayList;

import?java.util.List;

import?java.util.Random;

/*

?*?客户端:TCP服务器

?*/

public?class?TcpSocketServerDemo?{

private?ServerSocket?serverSocket;//?服务器连接

private?int?port;?//?服务器监听端口

private?ListSocket?sockets;

private?int?ranNumber;

/**

?*?服务器结束的标志

?*/

public?boolean?ISEND;

public?TcpSocketServerDemo(int?port)?{

this.port?=?port;

serverSocket?=?null;

sockets?=?new?ArrayListSocket();

ranNumber?=?new?Random().nextInt(50);

}

/*

?*?启动服务器

?*/

public?void?starServer()?{

try?{

//?第一步:建立服务器连接(绑定监听端口)

serverSocket?=?new?ServerSocket(port);

System.out.println("姚舜的服务器已经启动...");

Socket?socket;//?客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)

ServerThread?serverThread;

Thread?t2;

while?(!ISEND)?{

//?第二步:监听端口(获取客户端连接)

socket?=?serverSocket.accept();

if?(ISEND)?{

while?(!socket.isClosed())?{

socket.close();

Thread.sleep(100);

}

}?else?{

sockets.add(socket);

serverThread?=?new?ServerThread(this,?socket);

t2?=?new?Thread(serverThread);

t2.start();

}

}

for?(Socket?s?:?sockets)?{

s.close();

}

serverSocket.close();

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

public?static?void?main(String[]?args)?{

TcpSocketServerDemo?tDemo?=?new?TcpSocketServerDemo(1000);

tDemo.starServer();

}

/**

?*?校验

?*?

?*?@param?i

?*?@return

?*/

public?int?check(int?i)?{

if?(i??ranNumber)?{

return?-1;

}?else?if?(i??ranNumber)?{

return?1;

}

return?0;

}

}

/**

?*?服务端处理线程

?*

?*/

class?ServerThread?implements?Runnable?{

private?TcpSocketServerDemo?tDemo;

private?Socket?socket;

private?DataInputStream?dis;//?输入流(读取数据)

private?DataOutputStream?dos;//?输出流(发送数据)

public?ServerThread(TcpSocketServerDemo?tDemo,?Socket?socket)?{

super();

this.tDemo?=?tDemo;

this.socket?=?socket;

try?{

dis?=?new?DataInputStream(socket.getInputStream());

dos?=?new?DataOutputStream(socket.getOutputStream());

}?catch?(IOException?e)?{

e.printStackTrace();

}

}

public?void?run()?{

String?message?=?"";

int?i;

int?index?=?0;

int?result?=?0;

while?(!message.equals("Quit")??index??5)?{

try?{

message?=?dis.readUTF();

}?catch?(IOException?e)?{

e.printStackTrace();

}

try?{

i?=?Integer.parseInt(message);

result?=?tDemo.check(i);

if?(result?==?1)?{

dos.writeUTF("数字大了");

}?else?if?(result?==?-1)?{

dos.writeUTF("数字小了");

}

if?(result?==?0)?{

dos.writeUTF("猜中了");

break;

}

}?catch?(NumberFormatException?|?IOException?e)?{

continue;

}

index++;

}

try?{

dis.close();

dos.close();

socket.close();

}?catch?(IOException?e)?{

e.printStackTrace();

}

}

}

TcpSocketClientDemo.java

package?yaoshun.InetAddress;

import?java.io.DataInputStream;

import?java.io.DataOutputStream;

import?java.io.IOException;

import?java.net.Socket;

import?java.net.UnknownHostException;

import?java.util.Scanner;

/*

?*?演示类:TCP客户端

?*/

public?class?TcpSocketClientDemo?{

private?Socket?socket;//?客户端连接(如果建立连接时,本机网络不容或服务器程序未启动则会抛出异常)

private?DataInputStream?dis;//?输入流(读取数据)

private?DataOutputStream?dos;//?输出流(发送数据)

//?private?String?serverIP;//?服务器IP地址

//?private?int?serverPort;//?服务器监听端口

private?String?sendData;//?发送的数据

public?TcpSocketClientDemo(String?serverIP,?int?serverPort)?throws?UnknownHostException,?IOException?{

//?this.serverIP?=?serverIP;

//?this.serverPort?=?serverPort;

socket?=?new?Socket(serverIP,?serverPort);

dis?=?new?DataInputStream(socket.getInputStream());

dos?=?new?DataOutputStream(socket.getOutputStream());

sendData?=?null;

startClient();

}

public?void?startClient()?{

try?{

//?第一步:创建客户端连接

System.out.println("===我的客户端界面===");

//?第二步:客户端向服务器发送数据

int?i?=?0;

Scanner?input?=?new?Scanner(System.in);

while?(i??5)?{

System.out.println("请输入你的内容(客户端的):");

sendData?=?input.nextLine();

if?(sendData.equals("quit"))?{

break;

}

dos.writeUTF(sendData);

sendData?=?dis.readUTF();

System.out.println("接收到服务器内容:"?+?sendData);

if?(i?==?5)?{

System.out.println("超过允许次数,请重新连接服务器.");

}

if?(sendData.equals("猜中了"))?{

i?=?5;

}

i++;

}

input.close();

}?catch?(Exception?e)?{

e.printStackTrace();

}?finally?{

//?第四步:关闭输入流、输出流和客户端连接

try?{

dis.close();

dos.close();

socket.close();

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

}

public?static?void?main(String[]?args)?{

try?{

new?TcpSocketClientDemo("127.0.0.1",?1000);

}?catch?(IOException?e)?{

e.printStackTrace();

}

}

}

(责任编辑:IT教学网)

更多

推荐时间特效文章