我正在观看客户端服务器java教程,其中导师正在开发一个聊天应用程序。在包上有两个文件夹。在一个文件夹中,有登录ui(代码),客户端连接和主类。在另一个文件夹中有服务器连接,服务器客户端连接(在此类中包括以唯一方式识别客户端,导师使用一个客户端的随机生成号)和主类。
当我运行应用程序UI时,但是在控制台服务器和客户端输出中没有显示。这是我的客户代码。
public class Client extends javax.swing.JFrame {
private String name,address;
private int port;
private DatagramSocket socket;
private InetAddress ip;
private Thread send;
public Client()
{
}
public Client(String name,String address, int port) {
this.name=name;
this.address=address;
this.port=port;
initComponents();
boolean connect = openConnection(address);
if(!connect){
System.err.println("Connection failed!");
console("Connection failed!");
}
console("Attempting to connection to"+ address+":"+port+",user:"+name);
String connection = "/c/"+name;
send(connection.getBytes());
}
private boolean openConnection(String address){
try{
socket =new DatagramSocket();
ip = InetAddress.getByName(address);
}catch(UnknownHostException e){
e.printStackTrace();
return false;
}catch(SocketException e){
e.printStackTrace();
return false;
}
return true;
}
//receive packets part //
private String receive(){
byte[]data=new byte[1024];
DatagramPacket packet = new DatagramPacket(data,data.length);
try{
socket.receive(packet);
}catch(IOException e){
e.printStackTrace();
}
String message =new String(packet.getData());
return message;
}
//send data
private void send(final byte[] data){
send = new Thread("send"){
public void run(){
DatagramPacket packet = new DatagramPacket (data,data.length,ip,port);
try{
socket.send(packet);
}catch(IOException e){
e.printStackTrace();
}
}
};
send.start();
}
private void send(String message){
if(message.equals(""))
return;
message= name+":"+message;
console(message);
send(message.getBytes());
txtMessage.setText("");
}
服务器类(包含在sperate文件夹中)
public class Server implements Runnable {
private List<ServerClient> clients = new ArrayList<ServerClient>();
private DatagramSocket socket;
private int port;
private boolean running = false;
private Thread run, manage, send, receive;
public Server(int port) {
this.port = port;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
return;
}
run = new Thread(this, "Server");
run.start();
}
public void run() {
running = true;
System.out.println("Server started on port " + port);
manageClients();
receive();
}
private void manageClients() {
manage = new Thread("Manage") {
public void run() {
while (running) {
// Managing
}
}
};
manage.start();
}
private void receive() {
receive = new Thread("Receive") {
public void run() {
while (running) {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
String string = new String(packet.getData());
System.out.println(string);
}
}
};
receive.start();
}
private void process(DatagramPacket packet){
String string = new String(packet.getData());
if(string.startsWith("/c/")){
UUID id= UUID.randomUUID();
System.out.println(id.toString());
clients.add(new ServerClient(string.substring(3,string.length()),packet.getAddress(),packet.getPort(),50));
System.out.println(string.substring(3,string.length()));
}else{
System.out.println(string);
}
}
在教程中,教师在没有main方法的情况下运行此服务器代码。但它不会在netbeans中运行。
在Server类包main方法
中public class ServerMain {
private int port;
private Server server;
public ServerMain(int port){
this.port=port;
server = new Server(port);
}
public static void main(String[] args){
int port;
if(args.length!=1){
System.out.println("Usage: java-jar MyChat.jar[port]");
return;
}
port=Integer.parseInt(args[0]);
new ServerMain(port);
}
在客户端聊天包主要方法中。
public class YakaaChat {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}