目录
四、LonginClient and Longinserver
一、InetAddressDemo
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress=InetAddress.getLocalHost();
System.out.println(inetAddress);
InetAddress address = InetAddress.getByName("localhost");
System.out.println(address);
System.out.println(address.getHostName());
System.out.println(address.getHostAddress());
}
}
二、client and server
@Auther:kongshan
@Date:2022/4/28-04-28-19:59
@Description:IntelliJ IDEA
@version:
*/
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
//创建Socket套接字对象
Socket clean=new Socket("localhost",10086);
//从socket中获取输出流
OutputStream outputStream=clean.getOutputStream();
//使用DataOutputStream包装流
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
//发送一条信息给服务器
dataOutputStream.writeUTF("你好,服务器,我是客户端");
dataOutputStream.close();
outputStream.close();
clean.close();
}
}
@Auther:kongshan
@Date:2022/4/28-04-28-20:04
@Description:IntelliJ IDEA
@version:
*/
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
// 创建ServerSocket对象
ServerSocket serverSocket=new ServerSocket(10086);
System.out.println("服务器已开启");
// 开启一个监听
Socket server=serverSocket.accept();
//从socket对象中获取输入流
InputStream inputStream = server.getInputStream();
//使用InputStream包装流
DataInputStream dataInputStream=new DataInputStream(inputStream);
//读取从客户端发过来的数据
String s=dataInputStream.readUTF();
System.out.println("客户端说"+s);
//关闭资源
dataInputStream.close();
inputStream.close();
server.close();
serverSocket.close();
}
}
三、client2 and server2
@Auther:kongshan
@Date:2022/4/28-04-28-20:13
@Description:IntelliJ IDEA
@version:
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client2 {
public static void main(String[] args) throws IOException {
//创建Socket对象
Socket client=new Socket("127.0.0.1",10000);
//从socket中获取输出流
OutputStream outputStream = client.getOutputStream();
outputStream.write("你好,服务器,我是客户端".getBytes());
InputStream inputStream=client.getInputStream();
byte[]buffer=new byte[1024];
int length=inputStream.read(buffer);
System.out.println("服务器说"+new String(buffer,0,length));
inputStream.close();
outputStream.close();
client.close();
}
}
@Auther:kongshan
@Date:2022/4/28-04-28-20:13
@Description:IntelliJ IDEA
@version:
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
public static void main(String[] args) throws IOException {
// 创建ServerSocket对象
ServerSocket serverSocket=new ServerSocket(10000);
Socket socket=serverSocket.accept();
InputStream inputStream=socket.getInputStream();
byte[]buffer=new byte[1024];
int length = inputStream.read(buffer);
System.out.println("客户端说"+new String(buffer,0,length));
OutputStream outputStream = socket.getOutputStream();
outputStream.write(("我已收到你的消息,我是服务器" ).getBytes());
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
四、LonginClient and Longinserver
@Auther:kongshan
@Date:2022/4/30-04-30-7:47
@Description:IntelliJ IDEA
@version:
*/
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class LoginClient {
public static void main(String[] args) throws IOException {
// //创建Socket对象
// Socket socket=new Socket("123.0.0.1",10086);
// //从socket中获取输出流
// OutputStream outputStream=socket.getOutputStream();
// //接收键盘输入的User
// User user=getUser();
// //使用ObjectOutputStream包装流
// ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
// objectOutputStream.writeObject(user);
// socket.shutdownOutput();
//
// //接收服务器端相应的验证消息
// InputStream inputStream=socket.getInputStream();
// DataInputStream dataInputStream=new DataInputStream(inputStream);
// String msg=dataInputStream.readUTF();
// System.out.println("服务器响应消息"+msg);
// socket.shutdownOutput();
// //关闭资源
// dataInputStream.close();
// inputStream.close();
// objectOutputStream.close();
// outputStream.close();
// socket.close();
while (true){
Socket socket=new Socket("127.0.0.1",10008);
//从socket中获取输出流
OutputStream outputStream=socket.getOutputStream();
//接收键盘输入的User
User user=getUser();
//使用ObjectOutputStream包装流
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(user);
socket.shutdownOutput();
//接收服务器端相应的验证消息
InputStream inputStream=socket.getInputStream();
DataInputStream dataInputStream=new DataInputStream(inputStream);
String msg=dataInputStream.readUTF();
System.out.println("服务器响应消息"+msg);
socket.shutdownOutput();
//关闭资源
dataInputStream.close();
inputStream.close();
objectOutputStream.close();
outputStream.close();
}
}
public static User getUser(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入用户名:");
String username=scanner.next();
System.out.println("请输入密码:");
String pwd=scanner.next();
User user=new User(username,pwd);
return user;
}
}
@Auther:kongshan
@Date:2022/4/30-04-30-8:06
@Description:IntelliJ IDEA
@version:
*/
import com.tensent.class4_2网络.client.User;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class LoginServer {
public static void main(String[] args) throws Exception {
// ServerSocket serverSocket = new ServerSocket(10008);
// System.out.println("服务器已开启");
// Socket server = serverSocket.accept();
// InputStream inputStream = server.getInputStream();
// ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
// User user = (User) objectInputStream.readObject();
// //验证是否合法
// String str = "";
// if ("空衫".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
// str = "登陆成功";
// } else {
// str = "登录失败";
// }
// server.shutdownOutput();
// OutputStream outputStream = server.getOutputStream();
// DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
// dataOutputStream.writeUTF(str);
// //关闭资源
// dataOutputStream.close();
// outputStream.close();
// objectInputStream.close();
// server.close();
// serverSocket.close();
/*服务器循环监听客户端的请求*/
ServerSocket serverSocket=new ServerSocket(10008);
System.out.println("服务器启动");
while (true){
Socket server = serverSocket.accept();
InputStream inputStream = server.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
User user = (User) objectInputStream.readObject();
//验证是否合法
String str = "";
if ("空衫".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
str = "登陆成功";
} else {
str = "登录失败";
}
server.shutdownOutput();
OutputStream outputStream = server.getOutputStream();
DataOutputStream dataOutputStream=new DataOutputStream(outputStream);
dataOutputStream.writeUTF(str);
//关闭资源
dataOutputStream.close();
outputStream.close();
objectInputStream.close();
server.close();
serverSocket.close();
}
}
}
五、PicClient and PicServer
@Auther:kongshan
@Date:2022/4/30-04-30-8:33
@Description:IntelliJ IDEA
@version:
*/
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class PicClient {
public static void main(String[] args) throws Exception {
FileOutputStream fileOutputStream=new FileOutputStream("24_KoBi.jpg");
//创建ServerSocket对象
ServerSocket serverSocket=new ServerSocket(10086);
System.out.println("********服务器已开启*********");
//开启监听
Socket server=serverSocket.accept();
//从socket中获取输入流
InputStream inputStream=server.getInputStream();
//读取客户端发送的文件数据,同时写入到服务端
byte[]buffer=new byte[1024];
int length=0;
while ((length=inputStream.read(buffer))!=-1){
fileOutputStream.write(buffer,0,length);
}
server.shutdownOutput();
//服务器给客户端相应一条信息
OutputStream outputStream=server.getOutputStream();
outputStream.write("上传成功!".getBytes());
server.shutdownOutput();
//关闭资源
outputStream.close();
inputStream.close();
fileOutputStream.close();
server.close();
serverSocket.close();
}
}
@Auther:kongshan
@Date:2022/4/30-04-30-8:43
@Description:IntelliJ IDEA
@version:
*/
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class PicServer {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream=new FileInputStream("2.jpg");
// 创建Socket对象
Socket socket=new Socket("localhost",10086);
//从socket中获取输出流
OutputStream outputStream=socket.getOutputStream();
//读取文件数据写入到服务器
int read=0;
while ((read=fileInputStream.read())!=-1){
outputStream.write(read);
}
//截断输出流
socket.shutdownOutput();
//读取服务端相应的消息
InputStream inputStream=socket.getInputStream();
byte[]buffer=new byte[1024];
int length=inputStream.read(buffer);
System.out.println("服务响应的消息:"+new String(buffer,0,length));
//关闭资源
inputStream.close();
outputStream.close();
fileInputStream.close();
socket.close();
}
}
六、UDPClient and UDPServer
@Auther:kongshan
@Date:2022/4/30-04-30-8:53
@Description:IntelliJ IDEA
@version:
*/
import java.net.*;
import java.util.Scanner;
public class UDPClient {
public static void main(String[] args) throws Exception {
//创建DatagramSocket对象
DatagramSocket datagramSocket=new DatagramSocket(10001);
//定义一个DatagramPacket数据包来封装要传输的数据
Scanner scanner=new Scanner(System.in);
String msg=scanner.nextLine();
DatagramPacket datagramPacket=new DatagramPacket(msg.getBytes(),msg.getBytes().length, InetAddress.getByName("MSI"),10086);
//发送数据
datagramSocket.send(datagramPacket);
//关闭资源
datagramSocket.close();
}
}
@Auther:kongshan
@Date:2022/4/30-04-30-9:00
@Description:IntelliJ IDEA
@version:
*/
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer {
public static void main(String[] args) throws Exception {
//创建DatagramSocket对象
DatagramSocket datagramSocket=new DatagramSocket(10086);
//接收消息
byte[]buffer=new byte[1024];
DatagramPacket datagramPacket=new DatagramPacket(buffer,buffer.length);
datagramSocket.receive(datagramPacket);
System.out.println("UDPClient说:"+new String(datagramPacket.getData(),0,datagramPacket.getLength()));
//关闭资源
datagramSocket.close();
}
}
七、 User
@Auther:kongshan
@Date:2022/4/30-04-30-7:56
@Description:IntelliJ IDEA
@version:
*/
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = -6558683375052993939L;
private String username;
private String password;
public User(){
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}