五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

使用TCP實(shí)現(xiàn)多用戶登錄的代碼

2020-04-19 22:44 作者:小垃圾kiki  | 我要投稿
package cn.jd.tcp;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

/*
?* 模擬登錄?? 多個(gè)客戶端請(qǐng)求
?* 創(chuàng)建客戶端
?* 1.使用Socket創(chuàng)建客戶端(這里實(shí)際上就是和服務(wù)器建立連接)
?* 所以需要指定服務(wù)器的地址和端口
?* 2.輸入輸出流操作
?* 3.釋放資源
?*/
public class LoginMultiClient {
?? ?public static void main(String[] args) throws UnknownHostException, IOException {
?? ??? ?System.out.println("------client----------");
?? ??? ?// 1.使用Socket創(chuàng)建客戶端(這里實(shí)際上就是和服務(wù)器建立連接)
?? ??? ?Socket client = new Socket("localhost", 8888);
?? ??? ?// 2.輸入輸出流操作 先請(qǐng)求后響應(yīng)
?? ??? ?new Send(client).send();
?? ??? ?new Receive(client).receive();
?? ??? ?// 3.釋放資源
?? ??? ?client.close();
?? ?}

?? ?// 發(fā)送
?? ?static class Send {
?? ??? ?private Socket client;
?? ??? ?private DataOutputStream dos;
?? ??? ?// InputStreamReader是從字節(jié)流到字符流的橋:它讀取字節(jié),并使用指定的charset將其解碼為字符
?? ??? ?private BufferedReader console;
?? ??? ?private String msg;

?? ??? ?public Send(Socket client) {
?? ??? ??? ?console = new BufferedReader(new InputStreamReader(System.in));
?? ??? ??? ?this.msg=init();
?? ??? ??? ?this.client = client;
?? ??? ??? ?try {
?? ??? ??? ??? ?dos = new DataOutputStream(client.getOutputStream());
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}

?? ??? ?private String? init() {
?? ??? ??? ?try {
?? ??? ??? ??? ?System.out.println("請(qǐng)輸入用戶名:");
?? ??? ??? ??? ?String uname = console.readLine();
?? ??? ??? ??? ?System.out.println("請(qǐng)輸入密碼:");
?? ??? ??? ??? ?String upwd = console.readLine();
?? ??? ??? ??? ?return? "用戶名:" + uname + "&" + "密碼:" + upwd;
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?return "";

?? ??? ?}

?? ??? ?public void send() {
?? ??? ??? ?try {
?? ??? ??? ??? ?dos.writeUTF(msg);
?? ??? ??? ??? ?dos.flush();
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}

?? ??? ?}

?? ?}

?? ?// 接收
?? ?static class Receive {
?? ??? ?private Socket client;
?? ??? ?private DataInputStream dis;

?? ??? ?public Receive(Socket client) {
?? ??? ??? ?this.client = client;
?? ??? ??? ?try {
?? ??? ??? ??? ?dis = new DataInputStream(client.getInputStream());
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}

?? ??? ?public void receive() {
?? ??? ??? ?String result;
?? ??? ??? ?try {
?? ??? ??? ??? ?result = dis.readUTF();
?? ??? ??? ??? ?System.out.println(result);
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?} // 接收數(shù)據(jù)

?? ??? ?}

?? ?}

}

package cn.jd.tcp;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/*
?* 模擬登錄 多個(gè)客戶端請(qǐng)求
?* 創(chuàng)建服務(wù)器
?* 1.指定端口:使用ServerSocket創(chuàng)建服務(wù)器
?* 2.阻塞式的等待連接
?* 3.輸入輸出流操作
?* 4.釋放資源
?*/
public class LoginMultiServer {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?System.out.println("------server----------");
?? ??? ?// 1.指定端口:使用ServerSocket創(chuàng)建服務(wù)器
?? ??? ?ServerSocket server = new ServerSocket(8888);
?? ??? ?boolean isRunning = true;
?? ??? ?// 2.阻塞式等待連接accept
?? ??? ?while (isRunning) {
?? ??? ??? ?// 建立了連接之后啟動(dòng)多線程
?? ??? ??? ?Socket client = server.accept();// 一次accept就是一個(gè)連接
?? ??? ??? ?System.out.println("一個(gè)客戶端建立了連接");
?? ??? ??? ?new Thread(new Channel(client)).start();
?? ??? ?}
?? ??? ?server.close();
?? ?}
?? ?//一個(gè)channel就代表一個(gè)客戶端
?? ?// 內(nèi)部類可以使用外部類的任何變量和方法
?? ?static class Channel implements Runnable {
?? ??? ?private Socket client;
?? ??? ?// 輸入流
?? ??? ?private DataInputStream dis;
?? ??? ?// 輸出流
?? ??? ?private DataOutputStream dos;
?? ??? ?//構(gòu)造器構(gòu)建好輸入流和輸出流
?? ??? ?public Channel(Socket client) {
?? ??? ??? ?this.client = client;
?? ??? ??? ?try {
?? ??? ??? ??? ?dis = new DataInputStream(client.getInputStream());
?? ??? ??? ??? ?dos = new DataOutputStream(client.getOutputStream());
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?release();
?? ??? ??? ?}
?? ??? ??? ?// 輸出

?? ??? ?}

?? ??? ?// 接收數(shù)據(jù)
?? ??? ?private String receive() {
?? ??? ??? ?String datas = "";
?? ??? ??? ?try {
?? ??? ??? ??? ?datas = dis.readUTF();
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?return datas;
?? ??? ?}

?? ??? ?// 釋放資源
?? ??? ?private void release() {
?? ??? ??? ?// 4.釋放資源
?? ??? ??? ?try {
?? ??? ??? ??? ?if(dos!=null) {
?? ??? ??? ??? ??? ?dos.close();
?? ??? ??? ??? ?}?? ?
?? ??? ??? ?} catch (IOException e1) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e1.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?try {
?? ??? ??? ??? ?if(dis!=null) {
?? ??? ??? ??? ??? ?dis.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?try {
?? ??? ??? ??? ?if(client!=null) {
?? ??? ??? ??? ??? ?client.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?}

?? ??? ?// 發(fā)送數(shù)據(jù)
?? ??? ?private void send(String msg) {
?? ??? ??? ?try {
?? ??? ??? ??? ?dos.writeUTF(msg);
?? ??? ??? ??? ?dos.flush();
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}

?? ??? ?}

?? ??? ?@Override
?? ??? ?public void run() {

?? ??? ??? ?// 3.輸入輸出流操作
?? ??? ??? ?String uname = "";
?? ??? ??? ?String upwd = "";
?? ??? ??? ?// 分析
?? ??? ??? ?String[] dataArray = receive().split("&");
?? ??? ??? ?for (String info : dataArray) {
?? ??? ??? ??? ?String[] userInfo = info.split(":");
//?? ??? ??? ?System.out.println(userInfo[0]+"-->"+userInfo[1]);
?? ??? ??? ??? ?if (userInfo[0].equals("用戶名")) {
?? ??? ??? ??? ??? ?System.out.println("你的用戶名為:" + userInfo[1]);
?? ??? ??? ??? ??? ?uname = userInfo[1];
?? ??? ??? ??? ?} else if (userInfo[0].equals("密碼")) {
?? ??? ??? ??? ??? ?System.out.println("你的密碼為:" + userInfo[1]);
?? ??? ??? ??? ??? ?upwd = userInfo[1];
?? ??? ??? ??? ?}
?? ??? ??? ?}

?? ??? ??? ?if (uname.equals("abc") && upwd.equals("123")) {// 成功
?? ??? ??? ??? ?send("登錄成功");
?? ??? ??? ?} else {// 失敗
?? ??? ??? ??? ?send("用戶名和密碼錯(cuò)誤");
?? ??? ??? ?}
?? ??? ??? ?release();

?? ??? ?}

?? ?}

}

使用TCP實(shí)現(xiàn)多用戶登錄的代碼的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
朔州市| 宽城| 澄迈县| 南平市| 宁阳县| 南丹县| 漳州市| 辉县市| 巴彦淖尔市| 庄浪县| 大兴区| 杭锦旗| 塔河县| 安庆市| 淮阳县| 澳门| 万全县| 马公市| 聂荣县| 延安市| 温宿县| 民勤县| 府谷县| 昌都县| 綦江县| 嘉黎县| 长寿区| 石首市| 榕江县| 阳春市| 施秉县| 莱芜市| 仁化县| 宁强县| 秭归县| 长寿区| 彩票| 庆城县| 江陵县| 南华县| 井冈山市|