年初正是JavaFX热的的时候,于是便学习了相关基础编程,作了个小聊天室。
虽然NetBeans 6.0支持JavaFX集成,但Editor的完成度并不高。当时为了javafx和java class之间互相传值可真是费了不少周折:<
更多JavaFX Script™资料请见官网:https://openjfx.dev.java.net
Matrix上有不少中文资料:http://www.matrix.org.cn
主要逻辑:
首先启动Server监听端口,然后启动Client。当Client输入聊天信息,会发送给Server,然后Server再群发到每一个Client上。
运行效果图:
Server端console:
Client端console:
Client端前台画面:
Server端的部分代码:
Server.java
1 package server;
2
3 import java.net.DatagramSocket;
4 import java.net.InetAddress;
5 import java.util.Vector;
6
7 public class Server{
8
9 //Server IP
10 public static InetAddress iaServer;
11 //Temporary Client IP
12 private static InetAddress iaClient;
13 //Clients IP
14 public static Vector<InetAddress> iaClientArr;
15 //Server datagram packets
16 public static DatagramSocket dsServer;
17
18 //Add client IP
19 public static void addClientIp(byte b[]){// }
20
21 //Delete client IP
22 public static void delClientIp(InetAddress ia){// }
23
24 public static void main(String args[]){
25 try{
26 iaServer=InetAddress.getByName(InetAddress.getLocalHost().getHostName());
27 //server的端口设为1234
28 dsServer = new DatagramSocket(1234,iaServer);
29 iaClientArr = new Vector<InetAddress>();
30
31 ServerRecieve tr = new ServerRecieve();
32 tr.start();
33
34 System.out.println("Server started.");
35 }
36 catch(Exception e){
37 e.printStackTrace();
38 System.exit(1);
39 }
40 }
41 }
ServerRecieve.java
1 package server;
2
3 import java.net.DatagramPacket;
4
5 public class ServerRecieve extends Thread{
6
7 //Receiving datagram packets
8 private String PASSTEXT;
9
10 @Override
11 public void run(){
12 recieve();
13 }
14
15 //Recieve from client
16 private void recieve(){
17 try{
18 while(true){
19
20 byte dataRecieve[]= new byte[Short.MAX_VALUE];
21 DatagramPacket dpRecieve = new DatagramPacket(dataRecieve,dataRecieve.length,Server.iaServer,1234);
22 Server.dsServer.receive(dpRecieve);
23
24 if(dpRecieve.getData() == null || dpRecieve.getData().equals("")){
25 Thread.sleep(300);
26 continue;
27 }
28 dataRecieve = dpRecieve.getData();
29
30 //when client first run,post IP to server.
31 String sTem = new String(dataRecieve).trim();
32 //client第一次启动,会发送一个握手协议,通知server加入client ip
33 if(!sTem.startsWith("up\n")){
34 this.PASSTEXT = sTem;
35 System.out.println("Server recieved: " + this.PASSTEXT);
36 ServerSend ss = new ServerSend(this.PASSTEXT.getBytes());
37 ss.start();
38 }
39 else{
40 sTem = sTem.substring(3);
41 Server.addClientIp(getIPByte(sTem));
42 }
43 }
44 }catch(Exception e){
45 e.printStackTrace();
46 }
47 }
48
49 //Get IP from string
50 private byte[] getIPByte(String IP){//}
51
52 }
ServerSend.java
1 package server;
2
3 import java.net.DatagramPacket;
4 import java.net.InetAddress;
5
6
7 public class ServerSend extends Thread{
8
9 //Sending datagram packets
10 private byte dataSend[];
11
12 public ServerSend(byte b[]){
13 this.dataSend = b;
14 }
15
16 @Override
17 public void run(){
18 send();
19 }
20
21 //Send to clients
22 private void send(){
23 DatagramPacket dpSend;
24 for(InetAddress ia : Server.iaClientArr){
25 try{
26 dpSend = new DatagramPacket(dataSend,dataSend.length,ia,5678);
27 Server.dsServer.send(dpSend);
28 System.out.println("Server sent: " + new String(dataSend));
29 }
30 catch(Exception e){
31 Server.delClientIp(ia);
32 }
33 }
34 }
35 }
下一篇:
用java.net包和JavaFX制作简单的C/S聊天室(二):Client端的部分代码