Java網(wǎng)絡(luò)編程,定義用戶類,實現(xiàn)序列化接口,客戶端發(fā)送內(nèi)容,登錄注冊【詩書畫唱】
我和我文字的故事:開始寫作的契機(jī)是我想記錄我認(rèn)為重要的一切!是為了不枉活過人生這一遭!是為了證明我曾存在過!
定義用戶類,該類實現(xiàn)序列化接口,定義屬性uname,pwd,
提示用戶輸入用戶名和密碼保存到對應(yīng)的屬性中,將內(nèi)容使用對象
傳入到服務(wù)器端,解析傳入的用戶名和密碼,如果用戶名是”給詩書畫唱三連關(guān)注“,密碼
是”給詩書畫唱三連關(guān)注“,就登錄成功,否則登錄失敗,在其基礎(chǔ)上,判斷用戶名和密碼是否正確,正確則服務(wù)器向客戶端
發(fā)送內(nèi)容,登錄成功,客戶端接收服務(wù)器發(fā)送的內(nèi)容,錯誤則發(fā)送登錄失敗
也是由客戶端進(jìn)行接收服務(wù)器發(fā)送的內(nèi)容

package dengLu;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class fuWuDuan {
public static void main(String[] args) throws Exception{
fuWuDuanFangFa();
}
private static void fuWuDuanFangFa() throws?
SocketException, IOException,
ClassNotFoundException {
System.out.println("UDP的服務(wù)器端開啟了"
+ "用戶名和密碼都為"
+ "“給詩書畫唱三連關(guān)注”");
DatagramSocket DatagramSocket
=new DatagramSocket(27222);
byte[] byteArray1=new byte[1024];
DatagramPacket DatagramPacket1=new?
DatagramPacket(byteArray1,byteArray1.length);
DatagramSocket.receive(DatagramPacket1);
//:堵塞性的接收內(nèi)容
byte[] byteArray2=DatagramPacket1.getData();
int len2=DatagramPacket1.getLength();
ByteArrayInputStream ByteArrayInputStream2=new?
ByteArrayInputStream(byteArray2,0,len2);
ObjectInputStream ObjectInputStream=new?
ObjectInputStream(ByteArrayInputStream2);
yongHuClass yongHuClass=(yongHuClass)
ObjectInputStream.readObject();
if(yongHuClass.uname.trim().
equals("給詩書畫唱三連關(guān)注")
&&yongHuClass.pwd.trim()
.equals("給詩書畫唱三連關(guān)注")){
String String="登錄成功";
DatagramPacket DatagramPacket=
new DatagramPacket(String.getBytes(),
String.getBytes().length,new?
InetSocketAddress("localhost",17111));
DatagramSocket.send(DatagramPacket);
}else{
String StringFalse="登錄失敗";
DatagramPacket DatagramPacketFalse=new DatagramPacket
(StringFalse.getBytes(),
StringFalse.getBytes().length,
new InetSocketAddress("localhost",17111));
DatagramSocket.send(DatagramPacketFalse);
}
DatagramSocket.close();
}}


package dengLu;
import java.util.*;
import java.io.*;
import java.net.*;
public class keHuDuan {
public static void main(String[] args)throws Exception {
keHuDuanFangFa();
}
private static void keHuDuanFangFa() throws?
SocketException, IOException {
System.out.println("UDP的客戶端開啟了");
Scanner s=new Scanner(System.in);
System.out.println("請輸入用戶名");
String uname=s.next();
System.out.println("請輸入用戶密碼");
String pwd=s.next();
yongHuClass yongHuClass=new yongHuClass(uname,pwd);
DatagramSocket DatagramSocket
=new DatagramSocket(17111);
ByteArrayOutputStream ByteArrayOutputStream=
new ByteArrayOutputStream();
ObjectOutputStream ObjectOutputStream=
new ObjectOutputStream(ByteArrayOutputStream);
ObjectOutputStream.writeObject(yongHuClass);
byte[] byteArray1=ByteArrayOutputStream.toByteArray();
DatagramPacket DatagramPacket1
=new DatagramPacket(byteArray1,byteArray1.length
,new InetSocketAddress("localhost",27222));
DatagramSocket.send(DatagramPacket1);
byte[] byteArray2=new byte[1024*60];
DatagramPacket DatagramPacket2=new DatagramPacket
(byteArray2,byteArray2.length);
DatagramSocket.receive(DatagramPacket2);
byte[] byteArray3=DatagramPacket2.getData();
int len3=DatagramPacket2.getLength();
String StringPrintOut=new String(byteArray3,0,len3);
System.out.println(StringPrintOut);
DatagramSocket.close();
}
}
//用戶類:
class yongHuClass implements Serializable{
String uname,pwd;
public yongHuClass(String uname,String pwd){
this.uname=uname;
this.pwd=pwd;
}
@Override
public String toString() {
return "yonghu [uname=" + uname?
+ ", pwd=" + pwd + "]";
}
}


