使用基于TCP的网络编程方法,完成远程计算等差数列的前n项和功能。
? (1)客户端将一等差数列的首项a1,公差d和项数n发送给服务器;
? ?(2)服务器端接收到数据后对接收到的数据进行解析,将前n项和的计算结果发送给客户端;
? ?(3)客户端收到后输出到控制台。
? ?要求上传源代码和服务器端和客户端的程序运行结果截图。
========================================================= 服务端
package MoocPart11_01_01;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String[] args) throws IOException {
ServerSocket listener=new ServerSocket(8888);
System.out.println("服务端已启动...");
Socket socket = listener.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line=br.readLine();
String[] input = line.split(",");
int a1 = Integer.parseInt(input[0]);
int d = Integer.parseInt(input[1]);
int n = Integer.parseInt(input[2]);
double result = n*a1+n*(n-1)/d;
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter( socket.getOutputStream()));
bw.write("首项为:"+a1+",公差为:"+d+",项数为:"+n+",的前n项和为:" +result);
bw.newLine();
bw.flush();
bw.close();
br.close();
socket.close();
listener.close();
}
}
客户端
package MoocPart11_01_01;
import java.io.*;
import java.net.Socket;
public class TCPclient {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",8888);
String str = "1,2,3";
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter( socket.getOutputStream()));
bw.write(str);
bw.newLine();
bw.flush();
System.out.println("输入内容:"+str);
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = br.readLine();
System.out.println(s);
br.close();
bw.close();
socket.close();
}
}
使用基于TCP的网络编程方法,完成远程小写转换和校验功能。
? (1)客户端键盘录入几个字符,发送到服务器端。
? (2)服务器端将接收到的字符进行小写处理(即将所有字母转化为小写),并向客户端发出转换后的字符串作为响应。
? (3)客户端对收到的字符串进行处理校验。如果校验成功,返回“处理成功”给服务器作为响应,并将其保存到character.txt文件中;否则返回“处理错误”给服务器作为响应。
? 要求上传源代码和服务器端和客户端程序运行结果截图。
========================================================= 服务端
package MoocPart10_01_02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String[] args) throws IOException {
ServerSocket listener=new ServerSocket(8888);
System.out.println("服务端已启动...");
Socket socket = listener.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line=br.readLine();
String lower = line.toLowerCase();
System.out.println(lower);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter( socket.getOutputStream()));
if (checkString(lower)) {
bw.write("校验成功!");
toTxt(lower);
}else {
bw.write("校验错误!");
}
bw.newLine();
bw.flush();
bw.close();
br.close();
socket.close();
listener.close();
}
private static boolean checkString(String str) {
char ch;
boolean capitalFlag = false;
boolean lowerCaseFlag = false;
boolean numberFlag = false;
for(int i=0;i < str.length();i++) {
ch = str.charAt(i);
if( Character.isDigit(ch)) {
numberFlag = true;
}
else if (Character.isUpperCase(ch)) {
capitalFlag = true;
} else if (Character.isLowerCase(ch)) {
lowerCaseFlag = true;
}
if(numberFlag && capitalFlag && lowerCaseFlag) {
return false;
}
}
return true;
}
private static void toTxt(String str) throws IOException {
FileWriter fileWriter = new FileWriter("D:/character.txt");
fileWriter.write(str);
fileWriter.close();
}
}
客户端
package MoocPart10_01_02;
import java.io.*;
import java.net.Socket;
public class TCPclient {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("127.0.0.1",8888);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter( socket.getOutputStream()));
String str = "JavaProgramingTechnique";
bw.write(str);
System.out.println("输入内容:"+str);
bw.newLine();
bw.flush();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = br.readLine();
System.out.println(s);
br.close();
bw.close();
socket.close();
}
}
|