各自努力,最高处见!加油!
题目一:
服务器端程序
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Homework01Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(9999);
System.out.println("服务器正在监听9999端口,等待连接");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String readLine = bufferedReader.readLine();
String sendData;
if("name".equals(readLine)){
sendData="我是服务器Server";
}else if ("hobby".equals(readLine)){
sendData="编写java程序";
}else{
sendData="你说啥呢?";
}
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(sendData);
bufferedWriter.flush();
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
socket.close();
}
}
客户端程序
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Homework01Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("shabi");
bufferedWriter.newLine();
bufferedWriter.flush();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String readLine = bufferedReader.readLine();
System.out.println("客户端回复:"+readLine);
bufferedWriter.close();
bufferedReader.close();
socket.close();
}
}
题目二:
接收端代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPReceverA {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket=new DatagramSocket(8888);
byte[] data=new byte[64*1024];
DatagramPacket datagramPacket=new DatagramPacket(data,data.length);
System.out.println("wait...");
datagramSocket.receive(datagramPacket);
int length=datagramPacket.getLength();
byte[] data1 = datagramPacket.getData();
String s = new String(data1, 0, length);
System.out.println("来自B端的信息:"+ s);
if ("四大名著".equals(s)){
data1="《西游记》、《红楼梦》、《三国演义》、《水浒传》".getBytes();
}else{
data1="what?".getBytes();
}
datagramPacket=new DatagramPacket(data1,data1.length, InetAddress.getLocalHost(),9999);
datagramSocket.send(datagramPacket);
System.out.println("回复完毕!");
datagramSocket.close();
}
}
发送端代码
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSenderB {
public static void main(String[] args) throws IOException {
DatagramSocket datagramSocket=new DatagramSocket(9999);
byte[] bytes = "四大名著".getBytes();
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8888);
datagramSocket.send(datagramPacket);
byte[] data = new byte[64 * 1024];
DatagramPacket datagramPacket1 = new DatagramPacket(data, data.length);
datagramSocket.receive(datagramPacket1);
int length = datagramPacket1.getLength();
byte[] data1 = datagramPacket1.getData();
String s = new String(data1, 0, length);
System.out.println("A端信息:"+s);
System.out.println("数据发送完毕,B端退出");
datagramSocket.close();
}
}
题目三:TCP文件下载
服务器端代码
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Homework03Server {
public static void main(String[] args) throws Exception {
String filePath="D:\\Java_code\\InternetLearn\\src\\";
String filePathDefault="D:\\Java_code\\InternetLearn\\src\\default.txt";
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器正在监听8888端口,等待连接。。。");
Socket socket = serverSocket.accept();
System.out.println("连接成功");
InputStream inputStream = socket.getInputStream();//输入流用于接收客户端的文件名
// String readString = StreamUtils.streamToString(inputStream);//得到客户端传来的请求文件名!!!!注意这个用法不能用,这个方法返回的
//字符串比实际的字符串长两个字节
// int readLen=0;
// byte[] buf=new byte[1024];
// while ((readLen=inputStream.read(buf))!=-1){
//
// }
byte[] bytes = inputStream.readAllBytes();
String readString = new String(bytes, 0, bytes.length);
System.out.println("客户端想要:"+readString);
FileInputStream fileInputStream=null;//创建输入流检测本地文件中是否存在客户端需要的文件,存的话将请求文件转换成byte数组
//否则将默认文件转换成一个byte数组。
File file = new File(filePath+readString);
if (file.exists()){
fileInputStream=new FileInputStream((filePath+readString));
System.out.println("找到指定文件");
}else{
fileInputStream=new FileInputStream(filePathDefault);
System.out.println("文件不存在,发送默认文件");
}
byte[] data = StreamUtils.streamToByteArray(fileInputStream);
OutputStream outputStream = socket.getOutputStream();//创建网络输出流,准备发送文件
outputStream.write(data);
socket.shutdownOutput();
inputStream.close();//关流
fileInputStream.close();
outputStream.close();
socket.close();
}
}
客户端代码
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Homework03Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(),8888);
OutputStream outputStream = socket.getOutputStream();
byte[] buf="library.jpg".getBytes();
outputStream.write(buf);//向服务器端发送文件名字
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();//得到一个网络输入流,准备接收服务器端发来的文件,得到一个byte数组
byte[] bytes = inputStream.readAllBytes();
// byte[] data = StreamUtils.streamToByteArray(inputStream);
String filePathServer="D:\\Java_code\\InternetLearn\\src\\ServerFilePath"+"\\library.jpg";
FileOutputStream fileOutputStream = new FileOutputStream(filePathServer);
fileOutputStream.write(bytes);
outputStream.close();
inputStream.close();
fileOutputStream.close();
socket.close();
}
}