小练习03
java网络编程小练习
练习1:模拟用户登录的功能
需求说明 模拟用户登录的功能 实现客户发送登录用户信息,服务器端显示登录信并响应给客户端登录成功
客户端ClientDemo1类
package sockt.socktpractice1;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ClientDemo1 {
public static void testClient(){
System.out.println("正在向服务器请求连接。。。");
Socket socket = null;
Scanner keybordscanner = null;
Scanner inScanner = null;
PrintWriter pwtoserver = null;
try {
socket = new Socket("127.0.0.1", 6666);
inScanner = new Scanner(socket.getInputStream());
System.out.println(inScanner.nextLine());
pwtoserver = new PrintWriter(socket.getOutputStream());
System.out.print("请你输入用户名:");
keybordscanner = new Scanner(System.in);
while(keybordscanner.hasNextLine()){
String username = keybordscanner.nextLine();
pwtoserver.println(username);
System.out.print("密码为:");
String password = keybordscanner.nextLine();
pwtoserver.println(password);
pwtoserver.flush();
String indata = inScanner.nextLine();
System.out.println("服务端:"+indata);
if (indata.equals("登录成功")){
break;
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
keybordscanner.close();
pwtoserver.close();
inScanner.close();
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testClient();
}
}
服务器ServerDemo1类
package sockt.socktpractice1;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerDemo1 {
public static void testServer() {
System.out.println("等待客户端连接。。。");
PrintWriter pwtoclien = null;
Scanner keybordscanner = null;
Scanner inScanner = null;
ServerSocket ss = null;
try {
ss = new ServerSocket(6666);
Socket socket = ss.accept();
System.out.println(socket.getInetAddress() + "已成功连接到此台服务器上。");
pwtoclien = new PrintWriter(socket.getOutputStream());
pwtoclien.println("已成功连接到远程服务器!" + "\t" + "请您先发言。");
pwtoclien.flush();
keybordscanner = new Scanner(System.in);
inScanner = new Scanner(socket.getInputStream());
while (inScanner.hasNextLine()) {
String username = inScanner.nextLine();
System.out.println("username:" + username);
String password = inScanner.nextLine();
System.out.println("password:" + password);
System.out.print("我(服务端):");
String keyborddata = keybordscanner.nextLine();
pwtoclien.println(keyborddata);
pwtoclien.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
pwtoclien.close();
keybordscanner.close();
inScanner.close();
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testServer();
}
}
练习2:模拟控制台1以1聊天
需求说明 1客户端发出信息,等待服务器端应答 2服务端接受到客户端消息,作出应答 然后重复1,2的操作,服务端询问客户端用户是否还有问题,直到客户端输入“N”,对话结束!
客户端ClientDemo2类
package sockt.socktpractice2;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ClientDemo2 {
public static void testClient(){
System.out.println("正在向服务器请求连接。。。");
Socket socket = null;
Scanner keybordscanner = null;
Scanner inScanner = null;
PrintWriter pwtoserver = null;
try {
socket = new Socket("127.0.0.1", 6666);
inScanner = new Scanner(socket.getInputStream());
System.out.println(inScanner.nextLine());
pwtoserver = new PrintWriter(socket.getOutputStream());
System.out.print("我(客户端):");
keybordscanner = new Scanner(System.in);
while(keybordscanner.hasNextLine()){
String keyborddata = keybordscanner.nextLine();
pwtoserver.println(keyborddata);
pwtoserver.flush();
if (keyborddata.equalsIgnoreCase("n")){
break;
}
String indata = inScanner.nextLine();
System.out.println("服务端:"+indata);
System.out.print("我(客户端):");
}
} catch (IOException e) {
e.printStackTrace();
}catch (NoSuchElementException e) {
e.printStackTrace();
}
finally {
keybordscanner.close();
pwtoserver.close();
inScanner.close();
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testClient();
}
}
服务器ServerDemo2类
package sockt.socktpractice2;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ServerDemo2 {
public static void testServer() {
System.out.println("等待客户端连接。。。");
PrintWriter pwtoclien = null;
Scanner keybordscanner = null;
Scanner inScanner = null;
ServerSocket ss = null;
try {
ss = new ServerSocket(6666);
Socket socket = ss.accept();
System.out.println(socket.getInetAddress() + "已成功连接到此台服务器上。");
pwtoclien = new PrintWriter(socket.getOutputStream());
pwtoclien.println("已成功连接到远程服务器!!!!请您先发言。");
pwtoclien.flush();
keybordscanner = new Scanner(System.in);
inScanner = new Scanner(socket.getInputStream());
while (inScanner.hasNextLine()) {
String indata = inScanner.nextLine();
System.out.println("客户端:" + indata);
if (indata.equalsIgnoreCase("n")){
break;
}
System.out.print("我(服务端):");
String keyborddata = keybordscanner.nextLine();
pwtoclien.println(keyborddata);
pwtoclien.flush();
}
} catch (IOException | NoSuchElementException e) {
e.printStackTrace();
} finally {
pwtoclien.close();
keybordscanner.close();
inScanner.close();
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testServer();
}
}
练习3:多客户端用户登录
需求说明 升级前一个上机内容,实现多客户端用户登录 分析 创建服务器端线程类,run()方法中实现对一个请求的响应处理 修改服务器端代码,实现循环监听状态 服务器端每监听到一个请求,创建一个处理线程
用户类User
package sockt.bean;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String pwd;
public User(){}
public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
登录客户端LoginClient类
package sockt.socktpractice3;
import org.junit.Test;
import sockt.bean.User;
import java.io.*;
import java.net.Socket;
public class LoginClient {
@Test
public void LoginClienttest1(){
Socket socket = null;
OutputStream os = null;
ObjectOutputStream oos = null;
InputStream is = null;
BufferedReader br = null;
try {
socket = new Socket("localhost",5000);
os = socket.getOutputStream();
oos = new ObjectOutputStream(os);
User user = new User("admin1","000000");
oos.writeObject(user);
socket.shutdownOutput();
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String reply;
while ((reply = br.readLine())!=null){
System.out.println("服务器响应说:"+reply);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
is.close();
os.close();
oos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void LoginClienttest2(){
Socket socket = null;
OutputStream os = null;
ObjectOutputStream oos = null;
InputStream is = null;
BufferedReader br = null;
try {
socket = new Socket("localhost",5000);
os = socket.getOutputStream();
oos = new ObjectOutputStream(os);
User user = new User("admin2","111111");
oos.writeObject(user);
socket.shutdownOutput();
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String reply;
while ((reply = br.readLine())!=null){
System.out.println("服务器响应说:"+reply);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
is.close();
os.close();
oos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void LoginClienttest3(){
Socket socket = null;
OutputStream os = null;
ObjectOutputStream oos = null;
InputStream is = null;
BufferedReader br = null;
try {
socket = new Socket("localhost",5000);
os = socket.getOutputStream();
oos = new ObjectOutputStream(os);
User user = new User("admin3","222222");
oos.writeObject(user);
socket.shutdownOutput();
is = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String reply;
while ((reply = br.readLine())!=null){
System.out.println("服务器响应说:"+reply);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
is.close();
os.close();
oos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
创建服务器端线程类LoginThread
package sockt.socktpractice3;
import sockt.bean.User;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
public class LoginThread extends Thread{
Socket socket = null;
public LoginThread(Socket socket) {
this.socket=socket;
}
public void run(){
InputStream is = null;
try {
is = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(is);
} catch (IOException e) {
e.printStackTrace();
}
User user = null;
try {
user = (User)ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("客户端说:"+user.getName()+"-"+user.getPwd());
OutputStream os = null;
try {
os = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
String reply = "欢迎登录!";
try {
os.write(reply.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
ois.close();
is.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
创建服务器类LoginServer
package sockt.socktpractice3;
import org.junit.Test;
import sockt.bean.User;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class LoginServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(5000);
while (true) {
Socket socket = serverSocket.accept();
LoginThread thread = new LoginThread(socket);
thread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
练习4:基于UDP的客户咨询问题
需求说明 实现客户咨询问题,客服人员答复问题 分析 咨询时,客户是发送方,客服人员是接收方 答复时,客服人员是发送方,客户是接收方,实现思路和咨询时相同
创建Receive类
package sockt.socktpractice4;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
public class Receive {
public static void main(String[] args) {
DatagramPacket dp=null;
DatagramSocket ds=null;
DatagramPacket dpto=null;
try{
byte[] buf=new byte[1024];
dp=new DatagramPacket(buf,buf.length);
ds=new DatagramSocket(9999);
Thread.sleep(10000);
ds.receive(dp);
String mess=new String(dp.getData(),0,dp.getLength());
System.out.println(dp.getAddress().getHostAddress()+"说:"+mess);
String reply="你好,我在,请咨询!";
System.out.println("我 说:"+reply);
SocketAddress sa=dp.getSocketAddress();
dpto=new DatagramPacket(reply.getBytes(),reply.getBytes().length ,sa);
ds.send(dpto);
}catch (Exception e) {
e.printStackTrace();
}finally{
ds.close();
}
}
}
创建Send类
package sockt.socktpractice4;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Send {
public static void main(String[] args) {
InetAddress ia=null;
DatagramSocket ds=null;
try{
String mess="你好,我想咨询一个问题。";
System.out.println("我 说:"+mess);
ia= InetAddress.getByName("localhost");
DatagramPacket dp=new DatagramPacket(mess.getBytes(),mess.getBytes().length ,ia,9999);
ds=new DatagramSocket();
Thread.sleep(10000);
ds.send(dp);
byte[] buf=new byte[1024];
DatagramPacket dpre=new DatagramPacket(buf,buf.length);
ds.receive(dpre);
String reply=new String(dpre.getData(),0,dpre.getLength());
System.out.println(dpre.getAddress().getHostAddress()+"说:"+reply);
}catch (UnknownHostException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
ds.close();
}
}
}
|